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)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestDiffRows(t *testing.T) {
|
||||
if diffRows("a\nb", "a\nb") != nil {
|
||||
t.Fatal("equal texts have rows")
|
||||
}
|
||||
rows := diffRows("1\n2\n3\n4\n5\n6\n7\n8\n9\n10\n11", "1\n2\n3\n4\n5\n6\n7\n8\n9\n10\neleven\n12")
|
||||
var got []string
|
||||
for _, r := range rows {
|
||||
got = append(got, r.Class()+r.Mark())
|
||||
}
|
||||
want := []string{"gap", "ctx", "ctx", "del-", "add+", "add+"}
|
||||
if len(got) != len(want) {
|
||||
t.Fatalf("rows %v", got)
|
||||
}
|
||||
for i := range want {
|
||||
if got[i] != want[i] {
|
||||
t.Fatalf("rows %v, want %v", got, want)
|
||||
}
|
||||
}
|
||||
// The numbers are those of each side.
|
||||
del, add := rows[3], rows[4]
|
||||
if del.Old != 11 || del.New != 0 || del.Text != "11" || add.Old != 0 || add.New != 11 || add.Text != "eleven" {
|
||||
t.Fatalf("%+v %+v", del, add)
|
||||
}
|
||||
if rows[5].New != 12 {
|
||||
t.Fatalf("%+v", rows[5])
|
||||
}
|
||||
// The text form is what it always was.
|
||||
if d := diffLines("a\nb", "a\nc"); d != " a\n- b\n+ c\n" {
|
||||
t.Fatalf("%q", d)
|
||||
}
|
||||
}
|
||||
@@ -1,10 +1,12 @@
|
||||
{{define "content"}}
|
||||
{{with .Data}}
|
||||
{{if .Diff}}
|
||||
<p>The saved settings differ from the confirmed (running) ones: <code>-</code> lines go away, <code>+</code> lines are new.
|
||||
{{if .Rows}}
|
||||
<p>The saved settings differ from the confirmed (running) ones: red lines go away, green lines are new.
|
||||
Nothing is applied until you press Apply.</p>
|
||||
{{with .TokenEdit}}<p class="error">{{.}}</p>{{end}}
|
||||
<pre class="log">{{.Diff}}</pre>
|
||||
<div class="diff" role="table" aria-label="Changes">
|
||||
{{range .Rows}}{{if eq .Class "gap"}}<div class="drow gap" role="row"><span class="gaptext">…</span></div>{{else}}<div class="drow {{.Class}}" role="row"><span class="ln" role="cell">{{if .Old}}{{.Old}}{{end}}</span><span class="ln" role="cell">{{if .New}}{{.New}}{{end}}</span><span class="mk" role="cell">{{.Mark}}</span><code role="cell">{{.Text}}</code></div>{{end}}{{end}}
|
||||
</div>
|
||||
<div class="actions">
|
||||
<form method="post" action="/apply"><input type="hidden" name="csrf" value="{{$.CSRF}}"><button>Apply</button></form>
|
||||
<form method="post" action="/discard"><input type="hidden" name="csrf" value="{{$.CSRF}}"><button class="secondary">Discard changes</button></form>
|
||||
|
||||
@@ -60,6 +60,6 @@ func (a *app) tokenRevoke(w http.ResponseWriter, r *http.Request, _ session) {
|
||||
// ones, for a person to review before applying.
|
||||
func (a *app) changesPage(w http.ResponseWriter, r *http.Request, sess session) {
|
||||
a.render(w, "changes", page{Title: "Changes", CSRF: sess.csrf, LoggedIn: true, Data: map[string]any{
|
||||
"Diff": a.sys.pendingChanges(), "Unapplied": a.sys.Status().Unapplied, "TokenEdit": a.sys.Status().TokenEdit,
|
||||
"Rows": a.sys.pendingRows(), "Unapplied": a.sys.Status().Unapplied, "TokenEdit": a.sys.Status().TokenEdit,
|
||||
}})
|
||||
}
|
||||
|
||||
@@ -158,7 +158,7 @@ type upgradeData struct {
|
||||
Run UpgradeRun
|
||||
Title string // what the running step does
|
||||
Updating bool // router-update is running
|
||||
Pending bool // a system waits for confirmation
|
||||
Pending bool // a system waits for confirmation
|
||||
}
|
||||
|
||||
// stableChannels are the stable NixOS releases the page offers (the branch
|
||||
|
||||
Reference in New Issue
Block a user