Documentation
¶
Overview ¶
Package diff says what changed between two texts, line by line.
It is here rather than beside the thing that draws a diff for the same reason github.com/Tangerg/oolong/core/fuzzy is: what changed is a fact about two strings and has nothing to do with a terminal. Something that only knew how to draw a diff would leave every caller to work out what the diff was, and there is one answer.
Lines and not characters. A terminal shows a diff a line at a time, a reader reads it a line at a time, and the line is the unit every tool that produces one already speaks in.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Hunk ¶
type Hunk struct {
Lines Script
// Old and New are where the hunk begins in each text, counting from one.
Old, New int
}
Hunk is a run of a script worth showing: what changed, and a few lines either side of it so a reader can see where they are.
type Kind ¶
type Kind uint8
Kind is what happened to a line.
What happened to a line. Context is the zero value, because a line that was left alone is what most lines are.
type Line ¶
type Line struct {
Kind Kind
Text string
// Old and New are the line's number in each text, counting from one, and zero
// where the line is not in that text. A reader looking for what to open and where
// needs the number in the text that still exists; a reader reading a change needs
// both.
Old, New int
}
Line is one line of a diff.
type Script ¶
type Script []Line
Script is a change to a text, as the lines that make it.
It reads top to bottom as the change itself: every line of both texts appears once, in an order in which the removals and the additions of the same passage sit together.
func Between ¶
Between is what changed between two texts.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/Tangerg/oolong/core/diff"
)
func main() {
before := strings.Split("a\nb\nc", "\n")
after := strings.Split("a\nB\nc", "\n")
fmt.Print(diff.Between(before, after))
}
Output: a -b +B c
func (Script) Hunks ¶
Hunks is the changed parts of the script with context lines around them, and everything else left out.
A file that changed in two places is two hundred lines of which six matter, and a view that shows all two hundred is a view nobody reads. Overlapping runs of context are one hunk rather than two, because a gap of one unchanged line is not a gap worth drawing a break across.
A context of zero is the changed lines alone. A script with nothing changed in it has no hunks at all, which is how "these are the same" is said.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/Tangerg/oolong/core/diff"
)
func main() {
// A file that changed in one place is a hundred lines of which three matter, and
// hunks are what is left once the rest is dropped.
before := strings.Split("1\n2\n3\n4\n5\n6\n7", "\n")
after := strings.Split("1\n2\n3\nX\n5\n6\n7", "\n")
for _, hunk := range diff.Between(before, after).Hunks(1) {
fmt.Printf("@@ -%d +%d @@\n%s", hunk.Old, hunk.New, hunk)
}
}
Output: @@ -3 +3 @@ 3 -4 +X 5
func (Script) Same ¶
Same reports whether the two texts were the same, which is a script with nothing changed in it.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/Tangerg/oolong/core/diff"
)
func main() {
same := strings.Split("a\nb", "\n")
fmt.Println(diff.Between(same, same).Same())
}
Output: true