cdiff

package module
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Jan 14, 2020 License: Apache-2.0 Imports: 5 Imported by: 5

README

cdiff

GoDoc

cdiff generates line-by-line diff or word-by-word diff (like github) and formats with color.

screenshot

Usage

import (
    "github.com/gookit/color"
)

func main() {
	diff := cdiff.Diff(string(oldDoc), string(newDoc), cdiff.WordByWord)
	color.Print(diff.Unified(oldDocPath, newDocPath, 3, cdiff.GooKitColorTheme))
}

Reference

func Diff(oldText, newText string, diffType DiffType) Result

It returns diff information from oldText/newText. diffType should be WordByWord or LineByLine.

Result.Unified(oldDocPath, newDocPath string, keepLines int, theme map[Tag]string) string

It returns string representation of unified format.

keepLines is like diff -U n. No changed lines count around diffs.

theme is a style text to create string representation. There is GooKitColorTheme and HTMLTheme. GooKitColorTheme has tags of github.com/gookit/color.

Result.Format(theme map[Tag]string) string

It doesn't omit unchanged line blocks instead of Unified().

Result.String() sring

It is similar to Format() but it desn't have extra text like theme.

License

Apache 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var GooKitColorTag = map[Tag]string{
	OpenDeletedLine:          "",
	CloseDeletedLine:         "\n",
	OpenDeletedModified:      "<fg=black;bg=red;>",
	CloseDeletedModified:     "</>",
	OpenDeletedNotModified:   "<red>",
	CloseDeletedNotModified:  "</>",
	OpenInsertedLine:         "",
	CloseInsertedLine:        "\n",
	OpenInsertedModified:     "<fg=black;bg=green;>",
	CloseInsertedModified:    "</>",
	OpenInsertedNotModified:  "<green>",
	CloseInsertedNotModified: "</>",
	OpenKeepLine:             "",
	CloseKeepLine:            "\n",
	OpenSection:              "<cyan>",
	CloseSection:             "</>\n",
	OpenHeader:               "",
	CloseHeader:              "\n",
}

GooKitColorTag is a theme for Result.Format() method for coloring console

GooKitColorTheme is a theme for Result.Format() method for coloring console

View Source
var HTMLTag = map[Tag]string{
	OpenDeletedLine:          `<div style="background-color: #ffecec;">`,
	CloseDeletedLine:         `</div>`,
	OpenDeletedModified:      `<span style="background-color: #f8cbcb;">`,
	CloseDeletedModified:     `</span>`,
	OpenDeletedNotModified:   "",
	CloseDeletedNotModified:  "",
	OpenInsertedLine:         `<div style="background-color: #eaffea;">`,
	CloseInsertedLine:        `</div>`,
	OpenInsertedModified:     `<span style="background-color: #a6f3a6;">`,
	CloseInsertedModified:    `</span>`,
	OpenInsertedNotModified:  "",
	CloseInsertedNotModified: "",
	OpenKeepLine:             `<div style="background-color: #ffffff;">`,
	CloseKeepLine:            "</div>",
}

HTMLTheme is a theme for Result.Format() method for generating HTML

Functions

This section is empty.

Types

type DiffType

type DiffType int

DiffType is an option of Diff()

const (
	// LineByLine returns simple result
	LineByLine DiffType = iota + 1
	// WordByWord returns complex result that includes word by word diffs in line by line diffs
	WordByWord
)

type Fragment

type Fragment struct {
	Text    string
	Changed bool
}

Fragment contains the smallest text fragment of diff

type Line

type Line struct {
	Ope           Ope
	NewLineNumber int
	OldLineNumber int
	Fragments     []Fragment
}

Line represents a line

func (Line) String

func (d Line) String() string

type Ope

type Ope int8

Ope defines the operation of a diff item.

const (
	// Delete item represents a delete diff.
	Delete Ope = -1
	// Insert item represents an insert diff.
	Insert Ope = 1
	// Keep item represents an equal diff.
	Keep Ope = 0
)

func (Ope) String

func (o Ope) String() string

type Result

type Result struct {
	Lines []Line
}

Result contains diff result

func Diff

func Diff(oldText, newText string, diffType DiffType) Result

Diff calcs diff of text

func (Result) Format

func (r Result) Format(theme map[Tag]string) string

Format returns formatted text

func (Result) String

func (r Result) String() string

func (Result) UnifiedWithGooKitColor added in v0.1.1

func (r Result) UnifiedWithGooKitColor(oldTitle, newTitle string, l int, theme map[Tag]color.Style) string

func (Result) UnifiedWithTag added in v0.1.1

func (r Result) UnifiedWithTag(oldTitle, newTitle string, l int, theme map[Tag]string) string

Unified returns unified formatWithTag diff text

type Tag

type Tag int

Tag is for formatting text

const (
	OpenDeletedLine Tag = iota + 1
	CloseDeletedLine
	OpenDeletedModified
	CloseDeletedModified
	OpenDeletedNotModified
	CloseDeletedNotModified
	OpenInsertedLine
	CloseInsertedLine
	OpenInsertedModified
	CloseInsertedModified
	OpenInsertedNotModified
	CloseInsertedNotModified
	OpenKeepLine
	CloseKeepLine
	OpenSection
	CloseSection
	OpenHeader
	CloseHeader
)

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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