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"
|
"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
|
// 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.
|
// hunk headers: " " unchanged lines near a change, "-" removed, "+" added.
|
||||||
// It returns "" when the texts are equal.
|
// It returns "" when the texts are equal.
|
||||||
func diffLines(a, b string) string {
|
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")
|
x, y := strings.Split(a, "\n"), strings.Split(b, "\n")
|
||||||
// Longest common subsequence table.
|
// Longest common subsequence table.
|
||||||
lcs := make([][]int, len(x)+1)
|
lcs := make([][]int, len(x)+1)
|
||||||
@@ -25,23 +69,19 @@ func diffLines(a, b string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
type line struct {
|
var all []DiffRow
|
||||||
op byte
|
|
||||||
text string
|
|
||||||
}
|
|
||||||
var all []line
|
|
||||||
i, j := 0, 0
|
i, j := 0, 0
|
||||||
for i < len(x) || j < len(y) {
|
for i < len(x) || j < len(y) {
|
||||||
switch {
|
switch {
|
||||||
case i < len(x) && j < len(y) && x[i] == y[j]:
|
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++
|
i++
|
||||||
j++
|
j++
|
||||||
case i < len(x) && (j == len(y) || lcs[i+1][j] >= lcs[i][j+1]):
|
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++
|
i++
|
||||||
default:
|
default:
|
||||||
all = append(all, line{'+', y[j]})
|
all = append(all, DiffRow{'+', 0, j + 1, y[j]})
|
||||||
j++
|
j++
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -50,7 +90,7 @@ func diffLines(a, b string) string {
|
|||||||
keep := make([]bool, len(all))
|
keep := make([]bool, len(all))
|
||||||
changed := false
|
changed := false
|
||||||
for k, l := range all {
|
for k, l := range all {
|
||||||
if l.op != ' ' {
|
if l.Op != ' ' {
|
||||||
changed = true
|
changed = true
|
||||||
for d := -ctx; d <= ctx; d++ {
|
for d := -ctx; d <= ctx; d++ {
|
||||||
if k+d >= 0 && k+d < len(all) {
|
if k+d >= 0 && k+d < len(all) {
|
||||||
@@ -60,9 +100,9 @@ func diffLines(a, b string) string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if !changed {
|
if !changed {
|
||||||
return ""
|
return nil
|
||||||
}
|
}
|
||||||
var out strings.Builder
|
var rows []DiffRow
|
||||||
skipped := false
|
skipped := false
|
||||||
for k, l := range all {
|
for k, l := range all {
|
||||||
if !keep[k] {
|
if !keep[k] {
|
||||||
@@ -70,12 +110,12 @@ func diffLines(a, b string) string {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if skipped {
|
if skipped {
|
||||||
out.WriteString("…\n")
|
rows = append(rows, DiffRow{Op: '.'})
|
||||||
skipped = false
|
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
|
// 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.
|
// pendingChanges is the diff from the confirmed settings to the saved ones.
|
||||||
func (s System) pendingChanges() string {
|
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())
|
cur, err1 := os.ReadFile(s.settingsPath())
|
||||||
conf, err2 := os.ReadFile(s.confirmedPath())
|
conf, err2 := os.ReadFile(s.confirmedPath())
|
||||||
if err1 != nil || err2 != nil {
|
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"}}
|
{{define "content"}}
|
||||||
{{with .Data}}
|
{{with .Data}}
|
||||||
{{if .Diff}}
|
{{if .Rows}}
|
||||||
<p>The saved settings differ from the confirmed (running) ones: <code>-</code> lines go away, <code>+</code> lines are new.
|
<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>
|
Nothing is applied until you press Apply.</p>
|
||||||
{{with .TokenEdit}}<p class="error">{{.}}</p>{{end}}
|
{{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">
|
<div class="actions">
|
||||||
<form method="post" action="/apply"><input type="hidden" name="csrf" value="{{$.CSRF}}"><button>Apply</button></form>
|
<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>
|
<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.
|
// ones, for a person to review before applying.
|
||||||
func (a *app) changesPage(w http.ResponseWriter, r *http.Request, sess session) {
|
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{
|
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,
|
||||||
}})
|
}})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user