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 ¶
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 ¶
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 Patience ¶
Patience computes the differences between l and r strings using the Patience algorithm.
func VanillaLCS ¶
VanillaLCS computes the reserences between two strings using the LCS algortithm from https://en.m.wikipedia.org/wiki/Longest_common_subsequence_problem
func VanillaPatience ¶
VanillaPatience implements the patience diff algorithm (from http://alfedenzo.livejournal.com/170301.html)
func (Result) PrettyPrint ¶
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.
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 )