| @@ -0,0 +1,191 @@ |
| |
1 | package control |
| |
2 | |
| |
3 | import ( |
| |
4 | "io" |
| |
5 | "strconv" |
| |
6 | "strings" |
| |
7 | ) |
| |
8 | |
| |
9 | type cellKind int |
| |
10 | |
| |
11 | const ( |
| |
12 | kindText cellKind = iota |
| |
13 | kindFlex |
| |
14 | kindRef |
| |
15 | kindState |
| |
16 | kindAge |
| |
17 | kindNum |
| |
18 | ) |
| |
19 | |
| |
20 | // cell is one column of a table row. The kind decides colour, time |
| |
21 | // format, and whether the column may be clipped to fit the terminal. |
| |
22 | type cell struct { |
| |
23 | kind cellKind |
| |
24 | s string |
| |
25 | } |
| |
26 | |
| |
27 | func cRef(s string) cell { return cell{kindRef, s} } |
| |
28 | func cState(s string) cell { return cell{kindState, s} } |
| |
29 | func cText(s string) cell { return cell{kindText, s} } |
| |
30 | func cFlex(s string) cell { return cell{kindFlex, s} } |
| |
31 | func cAge(ts string) cell { return cell{kindAge, ts} } |
| |
32 | func cNum(n int64) cell { return cell{kindNum, strconv.FormatInt(n, 10)} } |
| |
33 | |
| |
34 | // table is a list command's rows. Plain, each row is written as it |
| |
35 | // comes, tab-separated with no header. At a terminal rows are held |
| |
36 | // until flush, then written under a header, padded, and fitted to the |
| |
37 | // width. |
| |
38 | type table struct { |
| |
39 | term Term |
| |
40 | w io.Writer |
| |
41 | header []string |
| |
42 | rows [][]cell |
| |
43 | } |
| |
44 | |
| |
45 | func (c *Ctx) table(w io.Writer, header ...string) *table { |
| |
46 | return &table{term: c.Term, w: w, header: header} |
| |
47 | } |
| |
48 | |
| |
49 | func (t *table) row(cs ...cell) { |
| |
50 | if t.term.Cols == 0 { |
| |
51 | parts := make([]string, len(cs)) |
| |
52 | for i, c := range cs { |
| |
53 | if c.kind == kindAge { |
| |
54 | parts[i] = stamp(c.s) |
| |
55 | } else { |
| |
56 | parts[i] = c.s |
| |
57 | } |
| |
58 | } |
| |
59 | io.WriteString(t.w, strings.Join(parts, "\t")+"\n") |
| |
60 | return |
| |
61 | } |
| |
62 | now := termNow() |
| |
63 | for i := range cs { |
| |
64 | if cs[i].kind == kindAge { |
| |
65 | cs[i].s = relAge(cs[i].s, now) |
| |
66 | } |
| |
67 | } |
| |
68 | t.rows = append(t.rows, cs) |
| |
69 | } |
| |
70 | |
| |
71 | func (t *table) flush() { |
| |
72 | if t.term.Cols == 0 || len(t.rows) == 0 { |
| |
73 | return |
| |
74 | } |
| |
75 | n := len(t.header) |
| |
76 | widths := make([]int, n) |
| |
77 | for i, h := range t.header { |
| |
78 | widths[i] = cells(h) |
| |
79 | } |
| |
80 | for _, r := range t.rows { |
| |
81 | for i := 0; i < n && i < len(r); i++ { |
| |
82 | widths[i] = max(widths[i], cells(r[i].s)) |
| |
83 | } |
| |
84 | } |
| |
85 | t.fit(widths) |
| |
86 | |
| |
87 | var b strings.Builder |
| |
88 | line := make([]string, n) |
| |
89 | for i, h := range t.header { |
| |
90 | line[i] = h |
| |
91 | } |
| |
92 | b.WriteString(t.term.paint(sgrDim, t.join(line, widths)) + "\n") |
| |
93 | for _, r := range t.rows { |
| |
94 | for i := 0; i < n; i++ { |
| |
95 | s := "" |
| |
96 | if i < len(r) { |
| |
97 | s = clip(r[i].s, widths[i]) |
| |
98 | } |
| |
99 | line[i] = s |
| |
100 | } |
| |
101 | b.WriteString(t.joinRow(r, line, widths) + "\n") |
| |
102 | } |
| |
103 | io.WriteString(t.w, b.String()) |
| |
104 | } |
| |
105 | |
| |
106 | // fit shrinks columns until a row fits the terminal: the flexible |
| |
107 | // column first, down to 8 cells, then the other text columns from the |
| |
108 | // right, down to 8 each. |
| |
109 | func (t *table) fit(widths []int) { |
| |
110 | total := func() int { |
| |
111 | s := 2 * (len(widths) - 1) |
| |
112 | for _, w := range widths { |
| |
113 | s += w |
| |
114 | } |
| |
115 | return s |
| |
116 | } |
| |
117 | kinds := make([]cellKind, len(widths)) |
| |
118 | if len(t.rows) > 0 { |
| |
119 | for i := range widths { |
| |
120 | if i < len(t.rows[0]) { |
| |
121 | kinds[i] = t.rows[0][i].kind |
| |
122 | } |
| |
123 | } |
| |
124 | } |
| |
125 | shrink := func(i int) { |
| |
126 | if over := total() - t.term.Cols; over > 0 && widths[i] > 8 { |
| |
127 | widths[i] = max(8, widths[i]-over) |
| |
128 | } |
| |
129 | } |
| |
130 | for i, k := range kinds { |
| |
131 | if k == kindFlex { |
| |
132 | shrink(i) |
| |
133 | } |
| |
134 | } |
| |
135 | for i := len(kinds) - 1; i >= 0; i-- { |
| |
136 | if kinds[i] == kindText { |
| |
137 | shrink(i) |
| |
138 | } |
| |
139 | } |
| |
140 | } |
| |
141 | |
| |
142 | // join pads every column but the last and separates them by two spaces. |
| |
143 | func (t *table) join(line []string, widths []int) string { |
| |
144 | var b strings.Builder |
| |
145 | for i, s := range line { |
| |
146 | if i > 0 { |
| |
147 | b.WriteString(" ") |
| |
148 | } |
| |
149 | if i == len(line)-1 { |
| |
150 | b.WriteString(s) |
| |
151 | } else { |
| |
152 | b.WriteString(pad(s, widths[i])) |
| |
153 | } |
| |
154 | } |
| |
155 | return b.String() |
| |
156 | } |
| |
157 | |
| |
158 | // joinRow is join with state cells coloured after padding, so the |
| |
159 | // SGR bytes never count against the width. |
| |
160 | func (t *table) joinRow(r []cell, line []string, widths []int) string { |
| |
161 | var b strings.Builder |
| |
162 | for i, s := range line { |
| |
163 | if i > 0 { |
| |
164 | b.WriteString(" ") |
| |
165 | } |
| |
166 | padding := "" |
| |
167 | if i < len(line)-1 { |
| |
168 | padding = strings.Repeat(" ", max(0, widths[i]-cells(s))) |
| |
169 | } |
| |
170 | if i < len(r) && r[i].kind == kindState { |
| |
171 | s = t.term.paint(stateColor(s), s) |
| |
172 | } |
| |
173 | b.WriteString(s + padding) |
| |
174 | } |
| |
175 | return b.String() |
| |
176 | } |
| |
177 | |
| |
178 | // stripSGR removes SGR sequences, for tests and width checks. |
| |
179 | func stripSGR(s string) string { |
| |
180 | var b strings.Builder |
| |
181 | for i := 0; i < len(s); i++ { |
| |
182 | if s[i] == 0x1b { |
| |
183 | if j := strings.IndexByte(s[i:], 'm'); j >= 0 { |
| |
184 | i += j |
| |
185 | continue |
| |
186 | } |
| |
187 | } |
| |
188 | b.WriteByte(s[i]) |
| |
189 | } |
| |
190 | return b.String() |
| |
191 | } |