diff

package
v0.24.0 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2022 License: MIT Imports: 7 Imported by: 0

README

Overview

A simple lib to diff two string with pretty output.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ThemeDefault = func(t Type) gop.Style {
	switch t {
	case AddSymbol, AddWords:
		return gop.BgGreen
	case DelSymbol, DelWords:
		return gop.BgRed
	case ChunkStart:
		return gop.BgMagenta
	}
	return gop.None
}

ThemeDefault colors for Sprint

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

ThemeNone colors for Sprint

Functions

func ChunkWords added in v0.23.2

func ChunkWords(lines []*TokenLine)

ChunkWords with words

func Diff

func Diff(x, y string) string

Diff x and y into a human readable string.

func Format

func Format(ts []*Token, theme func(Type) gop.Style) string

Format tokens into a human readable string

func TokenizeLine

func TokenizeLine(x, y string) ([]*Token, []*Token)

TokenizeLine two different lines

Types

type Char

type Char rune

Char is a rune

func (Char) Hash

func (c Char) Hash() []byte

Hash interface

type Comparable

type Comparable interface {
	// Hash for comparison
	Hash() []byte
}

Comparable interface

func LCS

func LCS(x, y []Comparable) []Comparable

LCS extends the standard lcs algorithm with dynamic programming and recursive common-lines reduction. The base algorithm we use is here: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem#LCS_function_defined. TODO: implement Patience Diff http://alfedenzo.livejournal.com/170301.html

type Line

type Line struct {
	// contains filtered or unexported fields
}

Line of a string for fast comparison.

func NewLine

func NewLine(b []byte) *Line

NewLine from bytes

func (*Line) Hash

func (c *Line) Hash() []byte

Hash interface

type String

type String []Comparable

String of Char list

func NewString

func NewString(s string) String

NewString from string

func (String) String

func (s String) String() string

String interface

type Text

type Text []Comparable

Text of Char list

func NewText

func NewText(s string) Text

NewText from string. It will split the s via newlines.

func (Text) String

func (s Text) String() string

String interface

type Theme added in v0.24.0

type Theme func(t Type) gop.Style

Theme for diff

type Token

type Token struct {
	Type    Type
	Literal string
}

Token presents a symbol in diff layout

func SpreadTokenLines added in v0.23.2

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

SpreadTokenLines to tokens

func Tokenize added in v0.24.0

func Tokenize(x, y string) []*Token

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

func TokenizeText

func TokenizeText(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