Name the element a change is in and let the diff show the lines around it
Each group of changes gets a bar with its place in the settings (like router.firewall.rules[3], and the rule's zones) and buttons to show more lines above and below, or all that are hidden. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+190
-26
@@ -2,17 +2,27 @@ package main
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"regexp"
|
||||
"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).
|
||||
// DiffRow is a line of a diff: Op is ' ' (unchanged), '-' (removed), '+'
|
||||
// (added) or '.' (a bar above a group of lines: see diffView). 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
|
||||
Hidden bool // unchanged and far from a change: shown when the page is asked to
|
||||
|
||||
// Of a bar: where the group below it is in the settings (Path, and Label
|
||||
// naming the element), how many hidden lines follow it (N) and which way
|
||||
// there is more to show: Up (above the next change), Down (below the last).
|
||||
Path, Label string
|
||||
N int
|
||||
Up, Down bool
|
||||
}
|
||||
|
||||
// Class names the row for the page: add, del, ctx or gap.
|
||||
@@ -40,20 +50,29 @@ func (r DiffRow) Mark() string {
|
||||
// hunk headers: " " unchanged lines near a change, "-" removed, "+" added.
|
||||
// It returns "" when the texts are equal.
|
||||
func diffLines(a, b string) string {
|
||||
all, keep := diffAll(a, b)
|
||||
var out strings.Builder
|
||||
for _, r := range diffRows(a, b) {
|
||||
if r.Op == '.' {
|
||||
out.WriteString("…\n")
|
||||
skipped := false
|
||||
for k, l := range all {
|
||||
if !keep[k] {
|
||||
skipped = true
|
||||
continue
|
||||
}
|
||||
out.WriteString(string(r.Op) + " " + r.Text + "\n")
|
||||
if skipped {
|
||||
out.WriteString("…\n")
|
||||
skipped = false
|
||||
}
|
||||
out.WriteString(string(l.Op) + " " + l.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 {
|
||||
// diffContext is how many unchanged lines are shown around a change.
|
||||
const diffContext = 2
|
||||
|
||||
// diffAll is every line of the two texts in diff order, and which of them are
|
||||
// a change or near one; nil when the texts are equal.
|
||||
func diffAll(a, b string) ([]DiffRow, []bool) {
|
||||
x, y := strings.Split(a, "\n"), strings.Split(b, "\n")
|
||||
// Longest common subsequence table.
|
||||
lcs := make([][]int, len(x)+1)
|
||||
@@ -74,25 +93,23 @@ func diffRows(a, b string) []DiffRow {
|
||||
for i < len(x) || j < len(y) {
|
||||
switch {
|
||||
case i < len(x) && j < len(y) && x[i] == y[j]:
|
||||
all = append(all, DiffRow{' ', i + 1, j + 1, x[i]})
|
||||
all = append(all, DiffRow{Op: ' ', Old: i + 1, New: j + 1, Text: x[i]})
|
||||
i++
|
||||
j++
|
||||
case i < len(x) && (j == len(y) || lcs[i+1][j] >= lcs[i][j+1]):
|
||||
all = append(all, DiffRow{'-', i + 1, 0, x[i]})
|
||||
all = append(all, DiffRow{Op: '-', Old: i + 1, Text: x[i]})
|
||||
i++
|
||||
default:
|
||||
all = append(all, DiffRow{'+', 0, j + 1, y[j]})
|
||||
all = append(all, DiffRow{Op: '+', New: j + 1, Text: y[j]})
|
||||
j++
|
||||
}
|
||||
}
|
||||
// Keep two lines of context around changes.
|
||||
const ctx = 2
|
||||
keep := make([]bool, len(all))
|
||||
changed := false
|
||||
for k, l := range all {
|
||||
if l.Op != ' ' {
|
||||
changed = true
|
||||
for d := -ctx; d <= ctx; d++ {
|
||||
for d := -diffContext; d <= diffContext; d++ {
|
||||
if k+d >= 0 && k+d < len(all) {
|
||||
keep[k+d] = true
|
||||
}
|
||||
@@ -100,24 +117,171 @@ func diffRows(a, b string) []DiffRow {
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
return nil, nil
|
||||
}
|
||||
return all, keep
|
||||
}
|
||||
|
||||
// diffView is the diff for the page: all lines, the ones far from a change
|
||||
// marked Hidden, and a bar before each group of changed lines (and after the
|
||||
// last) that names where in the settings the group is and offers the lines
|
||||
// hidden next to it. nil when the texts are equal.
|
||||
func diffView(a, b string) []DiffRow {
|
||||
all, keep := diffAll(a, b)
|
||||
if all == nil {
|
||||
return nil
|
||||
}
|
||||
oldPaths, newPaths := jsonPaths(a), jsonPaths(b)
|
||||
var oldDoc, newDoc any
|
||||
json.Unmarshal([]byte(a), &oldDoc)
|
||||
json.Unmarshal([]byte(b), &newDoc)
|
||||
// where is the settings path of a changed line, and its element's name.
|
||||
where := func(l DiffRow) (string, string) {
|
||||
if l.Op == '+' && l.New-1 < len(newPaths) {
|
||||
p := newPaths[l.New-1]
|
||||
return pathName(p), pathLabel(newDoc, p)
|
||||
}
|
||||
if l.Op == '-' && l.Old-1 < len(oldPaths) {
|
||||
p := oldPaths[l.Old-1]
|
||||
return pathName(p), pathLabel(oldDoc, p)
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
var rows []DiffRow
|
||||
skipped := false
|
||||
for k, l := range all {
|
||||
if !keep[k] {
|
||||
skipped = true
|
||||
for k := 0; k < len(all); {
|
||||
start := k
|
||||
hidden := !keep[k]
|
||||
for k < len(all) && keep[k] == !hidden {
|
||||
k++
|
||||
}
|
||||
if hidden {
|
||||
bar := DiffRow{Op: '.', N: k - start, Down: start > 0, Up: k < len(all)}
|
||||
if k < len(all) {
|
||||
bar.Path, bar.Label = firstChange(all[k:], keep[k:], where)
|
||||
}
|
||||
rows = append(rows, bar)
|
||||
for _, l := range all[start:k] {
|
||||
l.Hidden = true
|
||||
rows = append(rows, l)
|
||||
}
|
||||
continue
|
||||
}
|
||||
if skipped {
|
||||
rows = append(rows, DiffRow{Op: '.'})
|
||||
skipped = false
|
||||
if start == 0 {
|
||||
bar := DiffRow{Op: '.'}
|
||||
bar.Path, bar.Label = firstChange(all[start:k], keep[start:k], where)
|
||||
rows = append(rows, bar)
|
||||
}
|
||||
rows = append(rows, l)
|
||||
rows = append(rows, all[start:k]...)
|
||||
}
|
||||
return rows
|
||||
}
|
||||
|
||||
// firstChange is where the first changed line of the lines that are kept is.
|
||||
func firstChange(rows []DiffRow, keep []bool, where func(DiffRow) (string, string)) (string, string) {
|
||||
for i, l := range rows {
|
||||
if !keep[i] {
|
||||
break
|
||||
}
|
||||
if l.Op != ' ' {
|
||||
return where(l)
|
||||
}
|
||||
}
|
||||
return "", ""
|
||||
}
|
||||
|
||||
// pathSeg is a step into the settings: a key, or an index into an array.
|
||||
type pathSeg struct {
|
||||
key string
|
||||
index int
|
||||
array bool // index is meant
|
||||
}
|
||||
|
||||
var jsonKey = regexp.MustCompile(`^"((?:[^"\\]|\\.)*)":`)
|
||||
|
||||
// jsonPaths is, for each line of indented JSON, the path of the object or
|
||||
// array the line is in (a line that opens one is in its parent).
|
||||
func jsonPaths(text string) [][]pathSeg {
|
||||
type frame struct {
|
||||
seg pathSeg
|
||||
array bool // the frame is an array
|
||||
next int // index of the next element
|
||||
}
|
||||
var stack []frame
|
||||
lines := strings.Split(text, "\n")
|
||||
paths := make([][]pathSeg, len(lines))
|
||||
for i, line := range lines {
|
||||
t := strings.TrimSpace(line)
|
||||
for _, f := range stack {
|
||||
if f.seg != (pathSeg{}) {
|
||||
paths[i] = append(paths[i], f.seg)
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case strings.HasPrefix(t, "}") || strings.HasPrefix(t, "]"):
|
||||
if len(stack) > 0 {
|
||||
stack = stack[:len(stack)-1]
|
||||
}
|
||||
case strings.HasSuffix(t, "{") || strings.HasSuffix(t, "["):
|
||||
var seg pathSeg
|
||||
if m := jsonKey.FindStringSubmatch(t); m != nil {
|
||||
seg = pathSeg{key: m[1]}
|
||||
} else if n := len(stack); n > 0 && stack[n-1].array {
|
||||
seg = pathSeg{index: stack[n-1].next, array: true}
|
||||
stack[n-1].next++
|
||||
}
|
||||
stack = append(stack, frame{seg: seg, array: strings.HasSuffix(t, "[")})
|
||||
default:
|
||||
if n := len(stack); n > 0 && stack[n-1].array {
|
||||
stack[n-1].next++
|
||||
}
|
||||
}
|
||||
}
|
||||
return paths
|
||||
}
|
||||
|
||||
// pathName writes a path like router.firewall.rules[3].
|
||||
func pathName(p []pathSeg) string {
|
||||
var b strings.Builder
|
||||
for _, s := range p {
|
||||
if s.array {
|
||||
fmt.Fprintf(&b, "[%d]", s.index)
|
||||
continue
|
||||
}
|
||||
if b.Len() > 0 {
|
||||
b.WriteByte('.')
|
||||
}
|
||||
b.WriteString(s.key)
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// pathLabel names the element at a path when it says what it is: a rule or a
|
||||
// forward has a from and a to ("lan → family").
|
||||
func pathLabel(doc any, p []pathSeg) string {
|
||||
v := doc
|
||||
for _, s := range p {
|
||||
switch c := v.(type) {
|
||||
case map[string]any:
|
||||
v = c[s.key]
|
||||
case []any:
|
||||
if !s.array || s.index >= len(c) {
|
||||
return ""
|
||||
}
|
||||
v = c[s.index]
|
||||
default:
|
||||
return ""
|
||||
}
|
||||
}
|
||||
if m, ok := v.(map[string]any); ok {
|
||||
from, _ := m["from"].(string)
|
||||
to, _ := m["to"].(string)
|
||||
if from != "" && to != "" {
|
||||
return from + " → " + to
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// pretty re-indents JSON with sorted keys, so two spellings of the same
|
||||
// settings compare equal.
|
||||
func pretty(data []byte) string {
|
||||
@@ -134,9 +298,9 @@ func (s System) pendingChanges() string {
|
||||
return diffLines(s.pendingTexts())
|
||||
}
|
||||
|
||||
// pendingRows is the same as rows, for the page.
|
||||
// pendingRows is the same for the page (see diffView).
|
||||
func (s System) pendingRows() []DiffRow {
|
||||
return diffRows(s.pendingTexts())
|
||||
return diffView(s.pendingTexts())
|
||||
}
|
||||
|
||||
// pendingTexts are the confirmed settings and the saved ones, normalized; two
|
||||
|
||||
Reference in New Issue
Block a user