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