Show the pending changes as a colored diff with line numbers
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+67
-16
@@ -6,10 +6,54 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
// DiffRow is a line of a diff: Op is ' ' (unchanged, near a change), '-'
|
||||
// (removed), '+' (added) or '.' (lines left out). Old and New are the line
|
||||
// numbers in the two texts (0 where the line is not in that text).
|
||||
type DiffRow struct {
|
||||
Op byte
|
||||
Old, New int
|
||||
Text string
|
||||
}
|
||||
|
||||
// Class names the row for the page: add, del, ctx or gap.
|
||||
func (r DiffRow) Class() string {
|
||||
switch r.Op {
|
||||
case '+':
|
||||
return "add"
|
||||
case '-':
|
||||
return "del"
|
||||
case '.':
|
||||
return "gap"
|
||||
}
|
||||
return "ctx"
|
||||
}
|
||||
|
||||
// Mark is the sign before the text.
|
||||
func (r DiffRow) Mark() string {
|
||||
if r.Op == '+' || r.Op == '-' {
|
||||
return string(r.Op)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// diffLines is a line diff of two texts in the style of diff -u without the
|
||||
// hunk headers: " " unchanged lines near a change, "-" removed, "+" added.
|
||||
// It returns "" when the texts are equal.
|
||||
func diffLines(a, b string) string {
|
||||
var out strings.Builder
|
||||
for _, r := range diffRows(a, b) {
|
||||
if r.Op == '.' {
|
||||
out.WriteString("…\n")
|
||||
continue
|
||||
}
|
||||
out.WriteString(string(r.Op) + " " + r.Text + "\n")
|
||||
}
|
||||
return out.String()
|
||||
}
|
||||
|
||||
// diffRows is the diff of two texts with two lines of context around each
|
||||
// change; nil when they are equal.
|
||||
func diffRows(a, b string) []DiffRow {
|
||||
x, y := strings.Split(a, "\n"), strings.Split(b, "\n")
|
||||
// Longest common subsequence table.
|
||||
lcs := make([][]int, len(x)+1)
|
||||
@@ -25,23 +69,19 @@ func diffLines(a, b string) string {
|
||||
}
|
||||
}
|
||||
}
|
||||
type line struct {
|
||||
op byte
|
||||
text string
|
||||
}
|
||||
var all []line
|
||||
var all []DiffRow
|
||||
i, j := 0, 0
|
||||
for i < len(x) || j < len(y) {
|
||||
switch {
|
||||
case i < len(x) && j < len(y) && x[i] == y[j]:
|
||||
all = append(all, line{' ', x[i]})
|
||||
all = append(all, DiffRow{' ', i + 1, j + 1, x[i]})
|
||||
i++
|
||||
j++
|
||||
case i < len(x) && (j == len(y) || lcs[i+1][j] >= lcs[i][j+1]):
|
||||
all = append(all, line{'-', x[i]})
|
||||
all = append(all, DiffRow{'-', i + 1, 0, x[i]})
|
||||
i++
|
||||
default:
|
||||
all = append(all, line{'+', y[j]})
|
||||
all = append(all, DiffRow{'+', 0, j + 1, y[j]})
|
||||
j++
|
||||
}
|
||||
}
|
||||
@@ -50,7 +90,7 @@ func diffLines(a, b string) string {
|
||||
keep := make([]bool, len(all))
|
||||
changed := false
|
||||
for k, l := range all {
|
||||
if l.op != ' ' {
|
||||
if l.Op != ' ' {
|
||||
changed = true
|
||||
for d := -ctx; d <= ctx; d++ {
|
||||
if k+d >= 0 && k+d < len(all) {
|
||||
@@ -60,9 +100,9 @@ func diffLines(a, b string) string {
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
return ""
|
||||
return nil
|
||||
}
|
||||
var out strings.Builder
|
||||
var rows []DiffRow
|
||||
skipped := false
|
||||
for k, l := range all {
|
||||
if !keep[k] {
|
||||
@@ -70,12 +110,12 @@ func diffLines(a, b string) string {
|
||||
continue
|
||||
}
|
||||
if skipped {
|
||||
out.WriteString("…\n")
|
||||
rows = append(rows, DiffRow{Op: '.'})
|
||||
skipped = false
|
||||
}
|
||||
out.WriteString(string(l.op) + " " + l.text + "\n")
|
||||
rows = append(rows, l)
|
||||
}
|
||||
return out.String()
|
||||
return rows
|
||||
}
|
||||
|
||||
// pretty re-indents JSON with sorted keys, so two spellings of the same
|
||||
@@ -91,10 +131,21 @@ func pretty(data []byte) string {
|
||||
|
||||
// pendingChanges is the diff from the confirmed settings to the saved ones.
|
||||
func (s System) pendingChanges() string {
|
||||
return diffLines(s.pendingTexts())
|
||||
}
|
||||
|
||||
// pendingRows is the same as rows, for the page.
|
||||
func (s System) pendingRows() []DiffRow {
|
||||
return diffRows(s.pendingTexts())
|
||||
}
|
||||
|
||||
// pendingTexts are the confirmed settings and the saved ones, normalized; two
|
||||
// empty texts (nothing to compare) if either cannot be read.
|
||||
func (s System) pendingTexts() (string, string) {
|
||||
cur, err1 := os.ReadFile(s.settingsPath())
|
||||
conf, err2 := os.ReadFile(s.confirmedPath())
|
||||
if err1 != nil || err2 != nil {
|
||||
return ""
|
||||
return "", ""
|
||||
}
|
||||
return diffLines(pretty(conf), pretty(cur))
|
||||
return pretty(conf), pretty(cur)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user