gunk-dev/the-valley

style.go

sourcecaaa5bee5e11main's published tip history
3.4 KiB
1// valley style: review's page, styled for a terminal. review pages a
2// plain buffer, which the editor opens and notes are read from, and shows
3// a rendering of it (reviewpage.go's page): the diff pager's, then
4// style's. style comes last so that the pager never reads its escapes,
5// and it changes no line's place, so the editor opens the buffer on the
6// line the view shows.
7//
8// It styles what compare writes (compare.go) and leaves every other line
9// as the pager rendered it: a comparison's title in bold, with "edited"
10// in yellow and "unchanged" or "same file changes" in green, the lines
11// under the title with hashes dim, and each section heading in cyan. The
12// lines under the title end at the first section heading or diff. It
13// uses only the basic attributes and the eight standard colours, which
14// read on dark and light terminals alike.
15//
16// It styles only for a terminal, and never when NO_COLOR is set to
17// anything but the empty string: when its output is a terminal, or with
18// --paged, when the caller has checked that the page goes to one.
19
20package main
21
22import (
23 "bufio"
24 "io"
25 "os"
26 "regexp"
27 "strings"
28)
29
30const styleUsage = "usage: valley style [--paged] < page"
31
32var (
33 styleTitle = regexp.MustCompile(`^\S+ v[0-9]+ at [0-9a-f]+: (rebased|recommitted|edited) since `)
34 styleHeading = regexp.MustCompile(`^── .+ ──$`)
35 styleWord = regexp.MustCompile(`\b(edited|unchanged|same file changes)\b`)
36 styleHash = regexp.MustCompile(`\b[0-9a-f]{7,64}\b`)
37 styleEscape = regexp.MustCompile("\x1b\\[[0-9;]*m")
38)
39
40func runStyle(args []string, stdin io.Reader, stdout io.Writer) error {
41 paged := false
42 for _, a := range args {
43 if a != "--paged" {
44 return failure(styleUsage)
45 }
46 paged = true
47 }
48 on := terminalOf(stdout).bold || paged && os.Getenv("NO_COLOR") == ""
49 w := bufio.NewWriter(stdout)
50 if !on {
51 if _, err := io.Copy(w, stdin); err != nil {
52 return err
53 }
54 return w.Flush()
55 }
56 r := bufio.NewReader(stdin)
57 header := false
58 for {
59 line, err := r.ReadString('\n')
60 if line != "" {
61 body, end := strings.CutSuffix(line, "\n")
62 w.WriteString(styledLine(body, &header))
63 if end {
64 w.WriteString("\n")
65 }
66 }
67 if err == io.EOF {
68 return w.Flush()
69 }
70 if err != nil {
71 return err
72 }
73 }
74}
75
76// styledLine is one line of the page styled, given whether it is under a
77// comparison's title, which it updates.
78func styledLine(line string, header *bool) string {
79 plain := styleEscape.ReplaceAllString(line, "")
80 switch {
81 case styleHeading.MatchString(plain):
82 *header = false
83 return sgrSpan("36", "39", plain)
84 case styleTitle.MatchString(plain):
85 *header = true
86 return sgrSpan("1", "22", styleWord.ReplaceAllStringFunc(plain, colourWord))
87 case !*header:
88 return line
89 case strings.HasPrefix(plain, "diff --git "):
90 *header = false
91 return line
92 }
93 if !strings.HasPrefix(plain, " ") {
94 plain = styleWord.ReplaceAllStringFunc(plain, colourWord)
95 }
96 return styleHash.ReplaceAllStringFunc(plain, func(h string) string {
97 if !strings.ContainsAny(h, "0123456789") {
98 return h
99 }
100 return sgrSpan("2", "22", h)
101 })
102}
103
104func colourWord(w string) string {
105 if w == "edited" {
106 return sgrSpan("33", "39", w)
107 }
108 return sgrSpan("32", "39", w)
109}
110
111// sgrSpan is s between the SGR codes that turn an attribute on and off, the
112// off code ending only that attribute, so that s can sit inside another.
113func sgrSpan(on, off, s string) string {
114 return "\x1b[" + on + "m" + s + "\x1b[" + off + "m"
115}