Thanks to visit codestin.com
Credit goes to pkg.go.dev

diff

package
v0.43.0 Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2026 License: MIT Imports: 6 Imported by: 0

README

Overview

A simple lib to diff two string with pretty output. It also provide some low-level API to customize the output.

Documentation

Overview

Package diff ...

Index

Constants

This section is empty.

Variables

View Source
var SplitKey = contextSplitKey{}

SplitKey is the context key used to override the default word-splitting function consumed by TokenizeLine.

View Source
var ThemeDefault = func(t Type) []gop.Style {
	switch t {
	case AddSymbol:
		return []gop.Style{gop.Green}
	case DelSymbol:
		return []gop.Style{gop.Red}
	case AddWords:
		return []gop.Style{gop.Green}
	case DelWords:
		return []gop.Style{gop.Red}
	case ChunkStart:
		return []gop.Style{gop.Black, gop.BgMagenta}
	}
	return []gop.Style{gop.None}
}

ThemeDefault colors for Sprint

View Source
var ThemeNone = func(_ Type) []gop.Style {
	return []gop.Style{gop.None}
}

ThemeNone colors for Sprint

Functions

func Diff

func Diff(x, y string) string

Diff x and y into a human readable string.

func Format

func Format(ts []Token, theme Theme) string

Format tokens into a styled string using theme.

func Render added in v0.43.0

func Render(ts []Token) string

Render tokens by invoking Build on each one into a single string.

func Split added in v0.26.0

func Split(s string) []string

Split splits a line into words. Short lines are split per-rune; longer lines are split into alphabetic, digit, or single-character tokens.

func Text

func Text(t Token) string

Text renders a token into a string via its Build method.

func TokenizeLine

func TokenizeLine(ctx context.Context, x, y string) ([]Token, []Token)

TokenizeLine two different lines

func Words added in v0.26.0

func Words(ctx context.Context, lines []*TokenLine)

Words diff

Types

type AddGutter added in v0.43.0

type AddGutter struct{ N, Xl, Yl int }

AddGutter renders the added-line gutter: "<Xl+1 spaces><N pad Yl> +".

func (AddGutter) Build added in v0.43.0

func (g AddGutter) Build(sb *strings.Builder)

Build writes the gutter into sb.

func (AddGutter) Type added in v0.43.0

func (AddGutter) Type() Type

Type of the token.

type ConcatToken added in v0.43.0

type ConcatToken struct {
	T Type
	// contains filtered or unexported fields
}

ConcatToken holds a run of strSegs sharing one Type. Produced by mergeRuns so that merging is zero-copy: parts are concatenated only at Build time.

func (ConcatToken) Build added in v0.43.0

func (x ConcatToken) Build(sb *strings.Builder)

Build writes each part into sb in order.

func (ConcatToken) Type added in v0.43.0

func (x ConcatToken) Type() Type

Type of the token.

type DelGutter added in v0.43.0

type DelGutter struct{ N, Xl, Yl int }

DelGutter renders the deleted-line gutter: "<N pad Xl> <Yl+1 spaces>-".

func (DelGutter) Build added in v0.43.0

func (g DelGutter) Build(sb *strings.Builder)

Build writes the gutter into sb.

func (DelGutter) Type added in v0.43.0

func (DelGutter) Type() Type

Type of the token.

type SameGutter added in v0.43.0

type SameGutter struct{ A, B, Xl, Yl int }

SameGutter renders the unchanged-line gutter: "<A pad Xl> <B pad Yl> ".

func (SameGutter) Build added in v0.43.0

func (g SameGutter) Build(sb *strings.Builder)

Build writes the gutter into sb.

func (SameGutter) Type added in v0.43.0

func (SameGutter) Type() Type

Type of the token.

type SegToken added in v0.43.0

type SegToken struct {
	T Type
	// contains filtered or unexported fields
}

SegToken carries a single strSeg tagged with a Type.

func (SegToken) Build added in v0.43.0

func (x SegToken) Build(sb *strings.Builder)

Build writes the segment into sb.

func (SegToken) Type added in v0.43.0

func (x SegToken) Type() Type

Type of the token.

type Styled added in v0.43.0

type Styled struct {
	Inner  Token
	Styles []gop.Style
}

Styled wraps an inner token with ANSI styles applied at Build time.

func (Styled) Build added in v0.43.0

func (s Styled) Build(sb *strings.Builder)

Build renders the inner token and writes its styled form into sb.

func (Styled) Type added in v0.43.0

func (s Styled) Type() Type

Type delegates to the wrapped token's type.

type Theme added in v0.24.0

type Theme func(t Type) []gop.Style

Theme for diff

type Token

type Token interface {
	Type() Type
	Build(sb *strings.Builder)
}

Token is a lazily-rendered piece of diff output.

func SpreadTokenLines added in v0.23.2

func SpreadTokenLines(lines []*TokenLine) []Token

SpreadTokenLines to tokens

func Stylize added in v0.43.0

func Stylize(ts []Token, theme Theme) []Token

Stylize wraps each token in a Styled using theme.

func Tokenize added in v0.24.0

func Tokenize(ctx context.Context, x, y string) []Token

Tokenize x and y into diff tokens with diff words and narrow chunks.

func TokenizeText

func TokenizeText(ctx context.Context, x, y string) []Token

TokenizeText text block a and b into diff tokens.

type TokenLine added in v0.23.2

type TokenLine struct {
	Type   Type
	Tokens []Token
}

TokenLine of tokens

func Narrow added in v0.23.2

func Narrow(n int, lines []*TokenLine) []*TokenLine

Narrow the context around each diff section to n lines.

func ParseTokenLines added in v0.23.2

func ParseTokenLines(ts []Token) []*TokenLine

ParseTokenLines of tokens

type Type

type Type int

Type of token

const (
	// Newline type
	Newline Type = iota
	// Space type
	Space

	// ChunkStart type
	ChunkStart
	// ChunkEnd type
	ChunkEnd

	// SameSymbol type
	SameSymbol
	// SameLine type
	SameLine

	// AddSymbol type
	AddSymbol
	// AddLine type
	AddLine

	// DelSymbol typ
	DelSymbol
	// DelLine type
	DelLine

	// SameWords type
	SameWords
	// AddWords type
	AddWords
	// DelWords type
	DelWords

	// EmptyLine type
	EmptyLine
)

Jump to

Keyboard shortcuts

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