gunk-dev/the-valley

terminal.go

source40b311d8f0demain's published tip history
2.7 KiB
1package main
2
3import (
4 "io"
5 "os"
6 "strings"
7 "unicode"
8)
9
10// terminal is what valley pending's and valley plan's text is laid out
11// for: a width in columns, whether text is styled, with names in bold and
12// plan's glyphs in colour, and whether a line can be redrawn in place.
13type terminal struct {
14 width int
15 bold bool
16 live bool
17}
18
19// terminalOf is the terminal w writes to. Its width is the terminal's
20// own, or 80 columns when w is no terminal or the terminal does not know
21// its width. Text is styled on a terminal, unless NO_COLOR is set to
22// anything but the empty string, and a terminal is live either way.
23func terminalOf(w io.Writer) terminal {
24 t := terminal{width: 80}
25 f, ok := w.(*os.File)
26 if !ok {
27 return t
28 }
29 cols, isTerminal := windowColumns(f)
30 if !isTerminal {
31 return t
32 }
33 if cols > 0 {
34 t.width = cols
35 }
36 t.live = true
37 t.bold = os.Getenv("NO_COLOR") == ""
38 return t
39}
40
41// strong is s in bold, on a terminal that shows it.
42func (t terminal) strong(s string) string {
43 if !t.bold {
44 return s
45 }
46 return "\x1b[1m" + s + "\x1b[0m"
47}
48
49// tint is s in the colour an SGR code names, on a terminal that shows
50// it. An empty code leaves s as it is.
51func (t terminal) tint(code, s string) string {
52 if !t.bold || code == "" {
53 return s
54 }
55 return "\x1b[" + code + "m" + s + "\x1b[0m"
56}
57
58// fit is s cut to the columns a line has left after lead.
59func (t terminal) fit(s, lead string) string {
60 return fit(s, t.width-columns(lead))
61}
62
63// fit is s cut to at most n columns, ending in … where it was cut.
64func fit(s string, n int) string {
65 if columns(s) <= n {
66 return s
67 }
68 var b strings.Builder
69 used := 0
70 for _, r := range s {
71 if used+runeColumns(r) > n-1 {
72 break
73 }
74 b.WriteRune(r)
75 used += runeColumns(r)
76 }
77 return strings.TrimRight(b.String(), " ") + "…"
78}
79
80// columns is how many columns a terminal gives s.
81func columns(s string) int {
82 n := 0
83 for _, r := range s {
84 n += runeColumns(r)
85 }
86 return n
87}
88
89// runeColumns is how many columns a terminal gives r: none for a mark that
90// combines with the character before it, two for a wide character, and
91// one for any other.
92func runeColumns(r rune) int {
93 if unicode.In(r, unicode.Mn, unicode.Me, unicode.Cf) {
94 return 0
95 }
96 for _, w := range wideRunes {
97 if r >= w[0] && r <= w[1] {
98 return 2
99 }
100 }
101 return 1
102}
103
104// wideRunes are the main blocks of East Asian wide and fullwidth
105// characters, and of emoji, which a terminal draws two columns wide.
106var wideRunes = [][2]rune{
107 {0x1100, 0x115f}, {0x2e80, 0x303e}, {0x3041, 0x33ff}, {0x3400, 0x4dbf},
108 {0x4e00, 0x9fff}, {0xa000, 0xa4cf}, {0xac00, 0xd7a3}, {0xf900, 0xfaff},
109 {0xfe30, 0xfe4f}, {0xff00, 0xff60}, {0xffe0, 0xffe6}, {0x1f300, 0x1f64f},
110 {0x1f900, 0x1f9ff}, {0x20000, 0x3fffd},
111}