diff

package
v3.0.0-...-a35e849 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: MIT Imports: 7 Imported by: 1

Documentation

Index

Examples

Constants

View Source
const DefaultTimeout = time.Second

Variables

View Source
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

func New(opts ...Opt) *Differ

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

func (d *Differ) Diff(actual, expected any) string

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

type Opt func(Differ) Differ

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

func Actual(opts ...Opt) Opt

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

func Expected(opts ...Opt) Opt

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

func WithFormat(format string) Opt

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

func WithSprinter(s Sprinter) Opt

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

func WithTimeout(timeout time.Duration) Opt

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 Sprinter

type Sprinter interface {
	Sprint(...any) string
}

Sprinter is any type which can print a string.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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