diff

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2023 License: BSD-2-Clause Imports: 6 Imported by: 3

Documentation

Overview

Package diff proposes a set of functions to compute differences between two strings. It offers standard implementation of (lcs based diff)[from https://en.m.wikipedia.org/wiki/Longest_common_subsequence_problem] as well as the (patience diff)[http://alfedenzo.livejournal.com/170301.html].

On top of these algorythms, some home-brewed approach is supposed to provide more readable diff outputs. Main intend is to allow refining diff outputs by deeper looking into differences, for instance between almost similar lines, look at differences by words the, runes.

`diff` package supports personalizing highlighters to pretty-print diff results.

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultHighlighter is the highlither used by default by PrettyPrint if
	// no specific highlighters are supplied. You can ovverride it as needed.
	// It defaults to WithoutMissingContent.
	DefaultHighlighter = WithoutMissingContent

	// WithoutMissingContent highlights differences by hidding any missing part
	// on left or right.
	WithoutMissingContent = Highlighter{
		Same:      func(dL, dR, dT string) (string, string, string) { return dL, dR, dT },
		Deleted:   func(dL, dR, dT string) (string, string, string) { return dL, "", dT },
		Inserted:  func(dL, dR, dT string) (string, string, string) { return "", dR, dT },
		Different: func(dL, dR, dT string) (string, string, string) { return dL, dR, dT },
	}

	// WithColor highlights differences in colors: inserted are in red, deleted
	// are in blue, missing content are cossed-out.
	WithColor = Highlighter{
		Same: func(dL, dR, dT string) (string, string, string) { return dL, dR, dT },
		Deleted: func(dL, dR, dT string) (string, string, string) {
			return ansi.Blue(dL), ansi.CrossedOut(dR), ansi.BlueBG(ansi.White(dT))
		},
		Inserted: func(dL, dR, dT string) (string, string, string) {
			return ansi.CrossedOut(dL), ansi.Red(dR), ansi.RedBG(ansi.White(dT))
		},
		Different: func(dL, dR, dT string) (string, string, string) {
			return ansi.Red(dL), ansi.Red(dR), ansi.RedBG(ansi.White(dT))
		},
	}

	// WithNonPrintable ensures that non easily spotable differences are showed
	// by aliasing non visible runes with visible equivalent ones.
	WithNonPrintable = Highlighter{
		Same: func(dL, dR, dT string) (string, string, string) {
			return showNonPrintable(dL), showNonPrintable(dR), dT
		},
		Deleted: func(dL, dR, dT string) (string, string, string) {
			return showNonPrintable(dL), showNonPrintable(dR), dT
		},
		Inserted: func(dL, dR, dT string) (string, string, string) {
			return showNonPrintable(dL), showNonPrintable(dR), dT
		},
		Different: func(dL, dR, dT string) (string, string, string) {
			return showNonPrintable(dL), showNonPrintable(dR), dT
		},
	}

	// WithSoftTabs replaces any tabs ('\t') by four consecutives spaces so
	// that it does not voids any further text formatting (like showing diff in
	// columns)
	WithSoftTabs = Highlighter{
		Same:      func(dL, dR, dT string) (string, string, string) { return expandTabs(dL), expandTabs(dR), dT },
		Deleted:   func(dL, dR, dT string) (string, string, string) { return expandTabs(dL), expandTabs(dR), dT },
		Inserted:  func(dL, dR, dT string) (string, string, string) { return expandTabs(dL), expandTabs(dR), dT },
		Different: func(dL, dR, dT string) (string, string, string) { return expandTabs(dL), expandTabs(dR), dT },
	}
)

Functions

func ByLines

func ByLines(s string) []string

ByLines splits a string by lines (limited by '\n').

func ByRunes

func ByRunes(s string) []string

ByRunes splits a string by runes.

func ByWords

func ByWords(s string) (split []string)

ByWords splits a string by words (group of letters).

Types

type Delta

type Delta interface {
	// Type is the kind of difference for Delta
	Type() Type
	// Value is the Delta's actual text
	Value() string
	// PrettyPrint returns for each diff a formatted view of differences resp.
	// for left string, right string, difference's type and marker.
	PrettyPrint(...Highlighter) ([]string, []string, []string, string)
	// contains filtered or unexported methods
}

Delta represents an atomic piece of diff

type Highlighter

type Highlighter struct {
	Same, Deleted, Inserted, Different func(string, string, string) (string, string, string)
}

Highlighter represents a set of functions to decorate a Diff when pretty printing it.

type Result

type Result []Delta

Result gathers any diff results

func LCS

func LCS(l, r string, tokenizers ...Tokenizer) Result

LCS computes the differences between l and r strings using the LCS algorithm.

func Patience

func Patience(l, r string, tokenizers ...Tokenizer) Result

Patience computes the differences between l and r strings using the Patience algorithm.

func VanillaLCS

func VanillaLCS(l, r []string) Result

VanillaLCS computes the reserences between two strings using the LCS algortithm from https://en.m.wikipedia.org/wiki/Longest_common_subsequence_problem

func VanillaPatience

func VanillaPatience(l, r []string) (res Result)

VanillaPatience implements the patience diff algorithm (from http://alfedenzo.livejournal.com/170301.html)

func (Result) GoString

func (r Result) GoString() (s string)

GoString represents a diff's Result in an easy to read format.

func (Result) PrettyPrint

func (r Result) PrettyPrint(h ...Highlighter) (dL []string, dR []string, dT []string, dM string)

PrettyPrint translates a difference's result into a human (or machine) readable text. It outputs a representation of the differences for the first reference string, for the second one as well as a representation of the type of difference.

Output format depends on the selected Highlighter(s) if any.

func (Result) PrintSideBySide

func (r Result) PrintSideBySide(h ...Highlighter) string

PrintSideBySide prints result in a human readable format showing side by side the left string, the right string and the differences between both.

Output format depends on the selected Highlighter(s) if any. WithSoftTabs highlighter is automatically applied to prevent voiding the output and doesn't need to be specified again.

func (Result) Type

func (r Result) Type() Type

Type returns the difference's Type of Result

func (Result) Value

func (r Result) Value() string

Value returns the difference's text of Result

type Tokenizer

type Tokenizer func(string) []string

Tokenizer is a function that transform a string into a bunch of tokens

type Type

type Type int

Type represents the differences's types that can be encountered

const (
	// IsUnknown when status is not known (usuallt initialisation state)
	IsUnknown Type = iota
	// IsSame when strings are the same
	IsSame
	// IsDeleted when a string has been deleted
	IsDeleted
	// IsInserted when a string has been inserted
	IsInserted
	// IsDifferent when two sets of strings are differents
	IsDifferent
)

func (Type) String

func (typ Type) String() string

String represents a difference's Type in an easy to understand format.

Jump to

Keyboard shortcuts

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