godiff

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 3, 2022 License: MIT Imports: 4 Imported by: 1

README

go-diff

A Go package that generates (colorful) diff between files.

Installation

go get -u codeberg.org/h7c/go-diff

and then in your .go files:

import godiff "codeberg.org/h7c/go-diff"

Usage

You have a basic example in examples that demonstrates the main usage of this package.

The usage is pretty straightforward:

func main() {
	data1, _ := os.ReadFile("samples/sample1.txt")
	data2, _ := os.ReadFile("samples/sample2.txt")

	f1 := godiff.NewFileFromBytes(data1)
	f2 := godiff.NewFileFromBytes(data2)

	if f1.IsDifferentFrom(f2) {
		godiff.ShowDiff(f1, f2, true)
	}
}

The output will look like:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Duis mattis eu erat eget vestibulum. Fusce scelerisque vel odio non tincidunt. Donec nec euismod nulla.
- Proin pulvinar dapibus elit ac consequat. Donec quis accumsan erat. Integer bibendum at dui sit amet pretium.
In vitae sem nunc. Morbi non nulla accumsan, tempus eros non, accumsan quam.
+ Proin pulvinar dapibus elit ac consequat. Donec quis accumsan erat. Integer bibendum at dui sit amet pretium.
Nullam scelerisque neque ut lorem hendrerit mattis. Vivamus ultrices quam sed quam volutpat fringilla.
- Proin eu elit vestibulum, accumsan est facilisis, consequat justo. Fusce non tempus dui, ac sagittis justo. 
+ Proin eu elit, accumsan est facilisis, consequat justo. Fusce non tempus dui, ac sagittis justo. 

You can configure almost everything. If you add the following line before the if condition in the example above:

godiff.RemovalSign = "(┛ಠ_ಠ)┛彡┻━┻"

You'll get the following output:

Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Duis mattis eu erat eget vestibulum. Fusce scelerisque vel odio non tincidunt. Donec nec euismod nulla.
(┛ಠ_ಠ)┛彡┻━┻ Proin pulvinar dapibus elit ac consequat. Donec quis accumsan erat. Integer bibendum at dui sit amet pretium.
In vitae sem nunc. Morbi non nulla accumsan, tempus eros non, accumsan quam.
+ Proin pulvinar dapibus elit ac consequat. Donec quis accumsan erat. Integer bibendum at dui sit amet pretium.
Nullam scelerisque neque ut lorem hendrerit mattis. Vivamus ultrices quam sed quam volutpat fringilla.
(┛ಠ_ಠ)┛彡┻━┻ Proin eu elit vestibulum, accumsan est facilisis, consequat justo. Fusce non tempus dui, ac sagittis justo. 
+ Proin eu elit, accumsan est facilisis, consequat justo. Fusce non tempus dui, ac sagittis justo.

All configurable variables are defined in the file variables.go. It is not so big, so do not hesitate to have a look.

Internals

License

MIT

Documentation

Overview

Package godiff provides primitives to return and/or show differences between two files.

Index

Constants

This section is empty.

Variables

View Source
var (
	// AdditionSign is the prefix that is added before an added line in the diff.
	// By default, it is a plus sign.
	AdditionSign = "+"
	// RemovalSign is the prefix that is added before a removed line in the diff.
	// By default, it is a minus sign.
	RemovalSign = "-"
	// UnchangedSign is the prefix that is added before an unchanged line in the diff.
	// By default, it is a tilde sign.
	UnchangedSign = "~"
	// ShowUnchangedSign indicates if we want to show the UnchangedSign in front of
	// unchanged lines or not.
	// By default, it is set to false.
	ShowUnchangedSign = false
	// SignLineSeparator is what's between the Addition or Removal sign and the
	// text. By default, it is a single whitespace.
	SignLineSeparator = " "
)

Functions

func GetDiff

func GetDiff(f1, f2 File, useColors bool) string

GetDiff returns the diff as a single string.

func ShowDiff

func ShowDiff(f1, f2 File, useColors bool)

ShowDiff shows the diff between f1 and f2, f1 being used as the reference file.

Types

type File

type File []byte

File represents a slice of bytes that will be diffed to another File.

func NewFileFromBytes

func NewFileFromBytes(b []byte) File

NewFileFromBytes converts a slice of bytes into a File.

func NewFileFromString

func NewFileFromString(s string) File

NewFileFromString converts a string into a File.

func (File) IsDifferentFrom

func (f1 File) IsDifferentFrom(f2 File) bool

IsDifferentFrom returns true if f2 is different from f1 false otherwise.

func (File) IsIdenticalTo

func (f1 File) IsIdenticalTo(f2 File) bool

IsIdenticalTo returns true is f2 is identical to f1, false otherwise.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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