diff

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2024 License: Apache-2.0 Imports: 8 Imported by: 5

README

godoc codecov Go Report Card

diff - unified diff utilities

diff is a Go package for computing the differences between two strings, can interact with the result to selectively pick groups of changes and generate unified diff output.

Installation

> go get github.com/go-corelibs/diff@latest

Examples

Diff

Get a unified diff between two strings.

original := `This is the first line
This is the second line`
modified := strings.Replace(original, "the first", "one", 1)
delta := diff.New("filename.txt", original, modified)
if unified, err := delta.Unified(); err != nil {
    panic(err)
} else {
    fmt.Println(unified)
}

With the output being:

--- a/filename.txt
+++ b/filename.txt
@@ -1,2 +1,2 @@
-This is the first line
+This is one line
 This is the second line
\ No newline at end of file

Renderer

Sometimes we like to render unified diffs for users in ways beyond plain text, such as HTML or the Go-Curses Tango markup format.

// using the unified variable from the Diff example
output := diff.HTMLRenderer.RenderDiff(unified)
fmt.Println(output)

Produces the following:

<ul style="list-style-type:none;margin:0;padding:0;">
<li style="color:#eeeeee;background-color:#770000;">--- a/filename.txt</li>
<li style="color:#ffffff;background-color:#007700;">+++ b/filename.txt</li>
<li style="font-style:italic;opacity:0.77;">@@ -1,2 +1,2 @@</li>
<li style="color:#eeeeee;background-color:#770000;">-This is <span style="background-color:#440000;opacity:0.77;text-decoration:line-through;">th</span>e<span style="background-color:#440000;opacity:0.77;text-decoration:line-through;"> first</span> line</li>
<li style="color:#ffffff;background-color:#007700;">+This is <span style="background-color:#004400;font-weight:bold;">on</span>e line</li>
<li style="opacity:0.77;"> This is the second line</li>
<li style="font-style:italic;opacity:0.77;">\ No newline at end of file</li>
</ul>

Go-CoreLibs

Go-CoreLibs is a repository of shared code between the Go-Curses and Go-Enjin projects.

License

Copyright 2023 The Go-CoreLibs Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use file except in compliance with the License.
You may obtain a copy of the license at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AddRemTags

type AddRemTags struct {
	Add MarkupTag
	Rem MarkupTag
}

type CRender

type CRender struct {
	File    MarkupTag
	Normal  MarkupTag
	Comment MarkupTag
	Line    AddRemTags
	Text    AddRemTags
}

CRender implements the RenderBuilder and Renderer interfaces

func (*CRender) Clone

func (r *CRender) Clone() RenderBuilder

func (*CRender) Make

func (r *CRender) Make() Renderer

func (*CRender) RenderDiff

func (r *CRender) RenderDiff(unified string) (markup string)

func (*CRender) RenderLine

func (r *CRender) RenderLine(a, b string) (ma, mb string)

func (*CRender) SetComment

func (r *CRender) SetComment(open, close string) RenderBuilder

func (*CRender) SetFile

func (r *CRender) SetFile(open, close string) RenderBuilder

func (*CRender) SetLineAdded

func (r *CRender) SetLineAdded(open, close string) RenderBuilder

func (*CRender) SetLineRemoved

func (r *CRender) SetLineRemoved(open, close string) RenderBuilder

func (*CRender) SetNormal

func (r *CRender) SetNormal(open, close string) RenderBuilder

func (*CRender) SetTextAdded

func (r *CRender) SetTextAdded(open, close string) RenderBuilder

func (*CRender) SetTextRemoved

func (r *CRender) SetTextRemoved(open, close string) RenderBuilder

type Diff

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

func New

func New(path, source, changed string) (delta *Diff)

New constructs a new Diff instance with the given source and changed strings computed into a set of "edits" which can be selectively included in the Diff.UnifiedEdits and Diff.ModifiedEdits outputs

func (*Diff) EditGroup

func (d *Diff) EditGroup(index int) (unified string)

EditGroup returns the unified diff of the edit group at the given index

func (*Diff) EditGroupsLen

func (d *Diff) EditGroupsLen() (count int)

EditGroupsLen returns the count of edit groups present

func (*Diff) GetEdit added in v1.1.0

func (d *Diff) GetEdit(index int) (text string, ok bool)

GetEdit returns the change text for the index given

func (*Diff) KeepAll

func (d *Diff) KeepAll()

KeepAll flags all edits to be included in the UnifiedEdits and ModifiedEdits output

func (*Diff) KeepEdit

func (d *Diff) KeepEdit(index int) (ok bool)

KeepEdit flags a particular edit to be included in the UnifiedEdits() and ModifiedEdits() output

func (*Diff) KeepGroup

func (d *Diff) KeepGroup(index int)

KeepGroup flags the given group index for including in the UnifiedEdits and ModifiedEdits outputs

func (*Diff) KeepLen

func (d *Diff) KeepLen() (count int)

KeepLen returns the total number of edits flagged to be included in the UnifiedEdits and ModifiedEdits output

func (*Diff) Len

func (d *Diff) Len() (length int)

Len returns the total number of edits (regardless of keep/skip state)

func (*Diff) ModifiedEdits

func (d *Diff) ModifiedEdits() (modified string, err error)

ModifiedEdits returns the source content modified by only kept edits

func (*Diff) SetEdit added in v1.1.0

func (d *Diff) SetEdit(index int, text string) (ok bool)

SetEdit updates the change text for the index given

func (*Diff) SkipAll

func (d *Diff) SkipAll()

SkipAll flags all edits to be excluded in the UnifiedEdits() and ModifiedEdits() output

func (*Diff) SkipEdit

func (d *Diff) SkipEdit(index int) (ok bool)

SkipEdit flags a particular edit to be excluded in the UnifiedEdits() output

func (*Diff) SkipGroup

func (d *Diff) SkipGroup(index int)

SkipGroup flags the given group index for exclusion from the UnifiedEdits and ModifiedEdits outputs

func (*Diff) Unified

func (d *Diff) Unified() (unified string, err error)

Unified returns the source content modified by all edits

func (*Diff) UnifiedEdit

func (d *Diff) UnifiedEdit(index int) (unified string)

UnifiedEdit returns the unified diff output for just the given edit

func (*Diff) UnifiedEdits

func (d *Diff) UnifiedEdits() (unified string)

UnifiedEdits returns the unified diff output for all kept edits

type MarkupTag

type MarkupTag struct {
	Open  string
	Close string
}

type RenderBuilder

type RenderBuilder interface {
	// SetFile specifies the markup wrapping the entire unified diff
	SetFile(open, close string) RenderBuilder
	// SetNormal specifies the markup wrapping each normal line of unified diff
	SetNormal(open, close string) RenderBuilder
	// SetComment specifies the markup wrapping comment lines (starting with a backslash `\` or a hash `#`)
	SetComment(open, close string) RenderBuilder
	// SetLineAdded specifies the markup wrapping lines that were added
	SetLineAdded(open, close string) RenderBuilder
	// SetTextAdded specifies the markup wrapping additions within a line
	SetTextAdded(open, close string) RenderBuilder
	// SetLineRemoved specifies the markup wrapping lines that were removed
	SetLineRemoved(open, close string) RenderBuilder
	// SetTextRemoved specifies the markup wrapping removals within a line
	SetTextRemoved(open, close string) RenderBuilder
	// Make returns the built Renderer instance
	Make() Renderer
}

RenderBuilder is the buildable interface for constructing new Renderer instances

func NewRenderer

func NewRenderer() (tb RenderBuilder)

NewRenderer returns a new RenderBuilder instance

type Renderer

type Renderer interface {
	// RenderLine compares to single-line strings and highlights the differences
	// with the CRender.Text markup strings
	RenderLine(a, b string) (ma, mb string)
	// RenderDiff parses a unified diff string and highlights the interesting
	// details using the CRender.Line, CRender.Comment and CRender.Normal
	// markup strings
	RenderDiff(unified string) (markup string)
	// Clone returns a new RenderBuilder instance configured exactly the same
	// as the one the Clone method is called upon
	Clone() RenderBuilder
}

Renderer is the interface used to actual render unified diff content

var (
	// TangoRender is a Renderer preset for the go-curses Tango markup
	// format used in the ctk.Label and other widgets
	TangoRender Renderer = &CRender{
		Line: AddRemTags{
			Add: MarkupTag{
				Open:  `<span foreground="#ffffff" background="#007700">`,
				Close: `</span>`,
			},
			Rem: MarkupTag{
				Open:  `<span foreground="#eeeeee" background="#770000">`,
				Close: `</span>`,
			},
		},
		Text: AddRemTags{
			Add: MarkupTag{
				Open:  `<span background="#004400" weight="bold">`,
				Close: `</span>`,
			},
			Rem: MarkupTag{
				Open:  `<span background="#440000" weight="dim" strikethrough="true">`,
				Close: `</span>`,
			},
		},
		Normal: MarkupTag{
			Open:  `<span weight="dim">`,
			Close: `</span>`,
		},
		Comment: MarkupTag{
			Open:  `<span style="italic" weight="dim">`,
			Close: `</span>`,
		},
	}

	// HTMLRender is a Renderer preset for browser presentation
	HTMLRender Renderer = &CRender{
		File: MarkupTag{
			Open:  `<ul style="list-style-type:none;margin:0;padding:0;">` + "\n",
			Close: `</ul>`,
		},
		Normal: MarkupTag{
			Open:  `<li style="opacity:0.77;">`,
			Close: `</li>`,
		},
		Comment: MarkupTag{
			Open:  `<li style="font-style:italic;opacity:0.77;">`,
			Close: `</li>`,
		},
		Line: AddRemTags{
			Add: MarkupTag{
				Open:  `<li style="color:#ffffff;background-color:#007700;">`,
				Close: `</li>`,
			},
			Rem: MarkupTag{
				Open:  `<li style="color:#eeeeee;background-color:#770000;">`,
				Close: `</li>`,
			},
		},
		Text: AddRemTags{
			Add: MarkupTag{
				Open:  `<span style="background-color:#004400;font-weight:bold;">`,
				Close: `</span>`,
			},
			Rem: MarkupTag{
				Open:  `<span style="background-color:#440000;opacity:0.77;text-decoration:line-through;">`,
				Close: `</span>`,
			},
		},
	}
)

Jump to

Keyboard shortcuts

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