~bigbes/sr-ht-spec

ref: 8219ede1804c144de2a2f2e42f3f476bff4b622a sr-ht-spec/prosediff/cmd/prosediff/main.go -rw-r--r-- 1.5 KiB
8219ede1 — Eugene Blikh feat(web,service): the owner mints and revokes agent tokens in a browser 13 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// Command prosediff prints the prose diff between two markdown files.
//
// It exists so the differ can be judged the way a reviewer would judge it —
// by reading its output on real documents — without a web layer.
//
//	prosediff old.md new.md
//	prosediff -stats -width 100 old.md new.md
package main

import (
	"flag"
	"fmt"
	"os"

	"sourcecraft.dev/bigbes/sr-ht-spec/prosediff"
)

func main() {
	var (
		width     = flag.Int("width", 92, "wrap prose at this column; 0 disables wrapping")
		context   = flag.Int("context", 0, "unchanged blocks to show around each change")
		showEqual = flag.Bool("all", false, "print unchanged blocks too")
		statsOnly = flag.Bool("stats", false, "print only the summary line")
	)
	flag.Parse()
	if flag.NArg() != 2 {
		fmt.Fprintln(os.Stderr, "usage: prosediff [flags] <old.md> <new.md>")
		flag.PrintDefaults()
		os.Exit(2)
	}

	oldSrc, err := os.ReadFile(flag.Arg(0))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}
	newSrc, err := os.ReadFile(flag.Arg(1))
	if err != nil {
		fmt.Fprintln(os.Stderr, err)
		os.Exit(1)
	}

	d := prosediff.Compare(oldSrc, newSrc)
	if !*statsOnly {
		fmt.Print(prosediff.RenderText(d, prosediff.RenderOptions{
			Width:     *width,
			Context:   *context,
			ShowEqual: *showEqual,
		}))
	}
	s := d.Stats
	fmt.Printf("\n%d blocks: %d equal, %d modified, %d added, %d removed, %d moved (+%d/-%d words)\n",
		len(d.Changes), s.BlocksEqual, s.BlocksModified, s.BlocksInserted,
		s.BlocksDeleted, s.BlocksMoved, s.WordsInserted, s.WordsDeleted)
}