Documentation ¶
Index ¶
Examples ¶
Constants ¶
const DefaultTimeout = time.Second
Variables ¶
var DefaultStrDiffs = []StringDiffAlgorithm{str.NewCharDiff()}
Functions ¶
This section is empty.
Types ¶
type Differ ¶
type Differ struct {
// contains filtered or unexported fields
}
Differ is a type that can diff values. It keeps its own diffing style.
func New ¶
New creates a Differ, using the passed in opts to manipulate its diffing behavior and output.
By default, we wrap mismatched text in angle brackets and separate them with "!=". Example:
matching text >actual!=expected< more matching text
opts will be applied to the text in the order they are passed in, so you can do things like color a value and then wrap the colored text up in custom formatting.
See the examples on the different Opt types for more detail.
func (*Differ) Diff ¶
Diff takes two values and returns a string showing a diff of them.
Example ¶
package main import ( "fmt" "github.com/poy/onpar/v3/diff" ) func main() { fmt.Println(diff.New().Diff("some string with foo in it", "some string with bar in it")) }
Output: some string with >foo!=bar< in it
type Opt ¶
Opt is an option type that can be passed to New.
Most of the time, you'll want to use at least one of Actual or Expected, to differentiate the two in your output.
func Actual ¶
Actual returns an Opt that only applies formatting to the actual value. Non-formatting options (e.g. different diffing algorithms) will have no effect.
Example ¶
package main import ( "fmt" "github.com/poy/onpar/v3/diff" ) func main() { styles := []diff.Opt{ diff.Actual( // styles passed to this will only apply to actual values diff.WithFormat("(%s|"), ), diff.Expected( // styles passed to this will only apply to expected values diff.WithFormat("%s)"), ), } fmt.Println(diff.New(styles...).Diff("some string with foo in it", "some string with bar in it")) }
Output: some string with (foo|bar) in it
func Expected ¶
Expected returns an Opt that only applies formatting to the expected value. Non-formatting options (e.g. different diffing algorithms) will have no effect.
func WithFormat ¶
WithFormat returns an Opt that wraps up differences using a format string. The format should contain one '%s' to add the difference string in.
Example ¶
package main import ( "fmt" "github.com/poy/onpar/v3/diff" ) func main() { style := diff.WithFormat("LOOK-->%s<--!!!") fmt.Println(diff.New(style).Diff("some string with foo in it", "some string with bar in it")) }
Output: some string with LOOK-->foobar<--!!! in it
func WithSprinter ¶
WithSprinter returns an Opt that wraps up differences using a Sprinter.
Example (Color) ¶
package main import ( "fmt" "github.com/fatih/color" "github.com/poy/onpar/v3/diff" ) func main() { // WithSprinter is provided for integration with any type // that has an `Sprint(...any) string` method. Here // we use github.com/fatih/color. styles := []diff.Opt{ diff.Actual(diff.WithSprinter(color.New(color.CrossedOut, color.FgRed))), diff.Expected(diff.WithSprinter(color.New(color.FgYellow))), } fmt.Println(diff.New(styles...).Diff("some string with foo in it", "some string with bar in it")) }
Output:
func WithStringAlgos ¶
func WithStringAlgos(algos ...StringDiffAlgorithm) Opt
WithStringAlgos picks the algorithms that will be used to diff strings. We will always use a "dumb" algorithm for the base case that simply returns the full string as either equal or different, but extra algorithms can be provided to get more useful diffs for larger strings. For example, StringCharDiff gets diffs of characters (good for catching misspellings), and StringLineDiff gets diffs of lines (good for large multiline output).
The default is DefaultStrDiffs.
If called without any arguments, only the "dumb" algorithm will be used.
func WithTimeout ¶
WithTimeout sets a timeout for diffing. Normally, diffs will be refined until the "cost" of the diff is as low as possible. If diffing is taking too long, the best diff that has been loaded will be returned.
The default is DefaultTimeout.
If no diff at all has been generated yet, we will still wait until the first diff is generated before returning, but no refinement will be done.
type StringDiffAlgorithm ¶
type StringDiffAlgorithm interface { // Diffs takes a context.Context to know when to stop, returning a channel // of diffs. Each new diff returned on this channel should have a lower // cost than the previous one. // // If a higher cost diff is returned after a lower cost diff, it will be // discarded. // // Once ctx.Done() is closed, diffs will not be read off of the returned // channel - it's up to the algorithm to perform select statements to avoid // deadlocking. Diffs(ctx context.Context, actual, expected []rune) <-chan str.Diff }
StringDiffAlgorithm is a type which can generate diffs between two strings.