/termdiff

Small utility for printing user-friendly text diffs

Primary LanguageGoMIT LicenseMIT

Go Report Card GitHub Actions Go Reference codecov# termdiff

import "github.com/princjef/termdiff"

Package termdiff is a small utility designed to make it easy to print terminal-friendly diffs.

Index

func Fprint

func Fprint(w io.Writer, name string, diffs []Diff, opts ...Option)

Fprint writes a set of diffs for the given named entity to the given writer using the default configuration for printing. Configurations can be overridden using the various [Option] functions.

func Print

func Print(name string, diffs []Diff, opts ...Option)

Print writes a set of diffs for the given named entity to [os.Stdout] using the default configuration for printing. Configurations can be overridden using the various [Option] functions.

func Sprint

func Sprint(name string, diffs []Diff, opts ...Option) string

Sprint converts a set of diffs into a string that can be sent to a terminal or other output using the default configuration for printing. Configurations can be overridden using the various [Option] functions.

type Diff

Diff holds a single diff with a type and text.

type Diff struct {
    Type DiffType
    Text string
}
func DiffsFromDiffMatchPatch(diffs []diffmatchpatch.Diff) []Diff

DiffsFromDiffMatchPatch converts a set of [diffmatchpatch.Diff] diffs into their equivalent in this package to enable easy interoperability.

DiffType categorizes the kind of operation associated with a [Diff].

type DiffType int

The valid types of diffs.

const (
    InsertDiffType DiffType = iota + 1
    EqualDiffType
    DeleteDiffType
)

Formatter applies formatting/pretty printing to a piece of text.

type Formatter func(text string) string

type Option

Option defines the contract for options to configure a [Printer] or a call to any of the functions like [Print], [Fprint] and [Sprint].

type Option func(p *Printer)
func WithAfterText(text string) Option

WithAfterText customizes the text that appears in the diff header indicating what the logical "after" state in the diff means.

func WithBeforeText(text string) Option

WithBeforeText customizes the text that appears in the diff header indicating what the logical "before" state in the diff means.

func WithBuffer(buffer int) Option

WithBuffer customizes the number of lines with no insertions or deletions that will be printed both above and below each set of lines with diffs.

func WithDeleteLineFormatter(f Formatter) Option

WithDeleteLineFormatter customizes the text formatter/pretty printer used to format portions of a line with deleted text that are not themselves changed.

func WithDeleteTextFormatter(f Formatter) Option

WithDeleteTextFormatter customizes the text formatter/pretty printer used to format portions of a line where text has been deleted.

func WithEqualFormatter(f Formatter) Option

WithEqualFormatter customizes the text formatter/pretty printer used to format any text that is not associated with insertion or deletion.

func WithInsertLineFormatter(f Formatter) Option

WithInsertLineFormatter customizes the text formatter/pretty printer used to format portions of a line with inserted text that are not themselves changed.

func WithInsertTextFormatter(f Formatter) Option

WithInsertTextFormatter customizes the text formatter/pretty printer used to format portions of a line where text has been inserted.

func WithNameFormatter(f Formatter) Option

WithNameFormatter customizes the text formatter/pretty printer used to format the name of the text being diffed at the top of the overall diff.

type Printer

Printer handles creating text-based diffs with several customization options. Use [NewPrinter] to create a printer that is pre-filled with the default options.

type Printer struct {
    // contains filtered or unexported fields
}
func NewPrinter(opts ...Option) Printer

NewPrinter creates a new [Printer], optionally customized with the given options. This is primarily useful for scenarios where multiple diffs need to be printed with the same options. Consider using the top-level [Print], [Fprint] and [Sprint] functions instead if you don't need to re-use customizations.

func (Printer) Fprint

func (p Printer) Fprint(w io.Writer, name string, diffs []Diff, opts ...Option)

Fprint writes a set of diffs for the given named entity to the given writer. Options can be specified to override behaviors in the [Printer].

func (Printer) Print

func (p Printer) Print(name string, diffs []Diff, opts ...Option)

Print writes a set of diffs for the given named entity to [os.Stdout]. Options can be specified to override behaviors in the [Printer].

func (Printer) Sprint

func (p Printer) Sprint(name string, diffs []Diff, opts ...Option) string

Sprint converts a set of diffs into a string that can be sent to a terminal or any other place. Options can be specified to override behaviors in the [Printer].

Generated by gomarkdoc