diff

package
v0.27.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2022 License: MIT Imports: 9 Imported by: 0

README

Overview

A simple lib to diff two string with pretty output.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SplitKey = struct{}{}

SplitKey for context

View Source
var ThemeDefault = func(t Type) []gop.Style {
	switch t {
	case AddSymbol, AddWords:
		return []gop.Style{gop.BgGreen}
	case DelSymbol, DelWords:
		return []gop.Style{gop.BgRed}
	case ChunkStart:
		return []gop.Style{gop.BgMagenta}
	}
	return []gop.Style{gop.None}
}

ThemeDefault colors for Sprint

View Source
var ThemeNone = func(t Type) []gop.Style {
	return []gop.Style{gop.None}
}

ThemeNone colors for Sprint

Functions

func Diff

func Diff(x, y string) string

Diff x and y into a human readable string.

func Format

func Format(ts []*Token, theme Theme) string

Format tokens into a human readable string

func Split added in v0.26.0

func Split(s string) []string

Split a line into words

func TokenizeLine

func TokenizeLine(ctx context.Context, x, y string) ([]*Token, []*Token)

TokenizeLine two different lines

func Words added in v0.26.0

func Words(ctx context.Context, lines []*TokenLine)

Words diff

Types

type Comparable

type Comparable interface {
	// Hash for fast comparison
	Hash() string
	// String returns the full content
	String() string
}

Comparable interface

type Comparables added in v0.25.0

type Comparables []Comparable

Comparables list

func NewText

func NewText(s string) Comparables

NewText from string. It will split the s via newlines.

func NewWords added in v0.26.0

func NewWords(words []string) Comparables

NewWords from string

func (Comparables) Common added in v0.25.0

func (x Comparables) Common(y Comparables) (left, right int)

Common returns the common prefix and suffix between x and y. This function scales down the problem via the first property: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem#First_property

func (Comparables) Histogram added in v0.25.0

func (x Comparables) Histogram() map[string]int

Histogram of each Comparable

func (Comparables) LCS added in v0.25.0

LCS extends the standard lcs algorithm with dynamic programming and recursive line-reduction. The base algorithm we use is here: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem#LCS_function_defined. TODO: implement Patience Diff http://alfedenzo.livejournal.com/170301.html

func (Comparables) Reduce added in v0.25.0

func (x Comparables) Reduce(y Comparables) Comparables

Reduce Comparables from x that doesn't exist in y

func (Comparables) String added in v0.25.0

func (x Comparables) String() string

String interface

type Line

type Line struct {
	// contains filtered or unexported fields
}

Line of a string for fast comparison.

func NewLine

func NewLine(b []byte) Line

NewLine from bytes

func (Line) Hash

func (c Line) Hash() string

Hash interface

func (Line) String added in v0.25.0

func (c Line) String() string

String interface

type Theme added in v0.24.0

type Theme func(t Type) []gop.Style

Theme for diff

type Token

type Token struct {
	Type    Type
	Literal string
}

Token presents a symbol in diff layout

func SpreadTokenLines added in v0.23.2

func SpreadTokenLines(lines []*TokenLine) []*Token

SpreadTokenLines to tokens

func Tokenize added in v0.24.0

func Tokenize(ctx context.Context, x, y string) []*Token

Tokenize x and y into diff tokens with diff words and narrow chunks.

func TokenizeText

func TokenizeText(ctx context.Context, x, y string) []*Token

TokenizeText text block a and b into diff tokens.

type TokenLine added in v0.23.2

type TokenLine struct {
	Type   Type
	Tokens []*Token
}

TokenLine of tokens

func Narrow added in v0.23.2

func Narrow(n int, lines []*TokenLine) []*TokenLine

Narrow the context around each diff section to n lines.

func ParseTokenLines added in v0.23.2

func ParseTokenLines(ts []*Token) []*TokenLine

ParseTokenLines of tokens

type Type

type Type int

Type of token

const (
	// Newline type
	Newline Type = iota
	// Space type
	Space

	// ChunkStart type
	ChunkStart
	// ChunkEnd type
	ChunkEnd

	// SameSymbol type
	SameSymbol
	// SameLine type
	SameLine

	// AddSymbol type
	AddSymbol
	// AddLine type
	AddLine

	// DelSymbol typ
	DelSymbol
	// DelLine type
	DelLine

	// SameWords type
	SameWords
	// AddWords type
	AddWords
	// DelWords type
	DelWords

	// EmptyLine type
	EmptyLine
)

type Word added in v0.26.0

type Word struct {
	// contains filtered or unexported fields
}

Word block

func (Word) Hash added in v0.26.0

func (w Word) Hash() string

Hash interface

func (Word) String added in v0.26.0

func (w Word) String() string

String interface

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL