vpac / src / table.v
123 lines · 115 sloc · 2.94 KB · 3609dfbd82867dc1034a443f8f9ced529ddbbedd
Raw
1module main
2
3import encoding.utf8
4import encoding.utf8.east_asian
5
6// vpac's own table printer, ported from divilib.fmt: rows of cells, every
7// column padded to its widest value, two spaces apart, like pkgin's listings.
8// the widths are the printed widths, so styled values (bold names, colored
9// versions) line up with the color sequences counted as zero
10pub struct Cell {
11 value string
12}
13
14pub struct Row {
15pub mut:
16 cols []Cell
17}
18
19// cell makes one column of a row
20fn cell(value string) Cell {
21 return Cell{
22 value: value
23 }
24}
25
26fn row(cols ...string) Row {
27 mut out := []Cell{}
28 for c in cols {
29 out << cell(c)
30 }
31 return Row{
32 cols: out
33 }
34}
35
36// kv is a key/value row, the key in bold
37fn kv(key string, value string) Row {
38 return Row{
39 cols: [cell(name_style(key)), cell(value)]
40 }
41}
42
43// visible_width is the printed width of a string in terminal cells: ANSI
44// escape sequences count as nothing, and a wide character (CJK, fullwidth
45// forms) as two, by its Unicode East Asian Width. ambiguous ones count as
46// one, the way terminals outside East Asian locales draw them. the \x1b form
47// and the \e033 form divilib used are both handled
48fn visible_width(input string) int {
49 mut result := 0
50 mut i := 0
51 for i < input.len {
52 // the length of the sequence marker, and by extension the whole
53 // sequence, is searched to the terminating byte in the ASCII range.
54 // the bytes of a multibyte character are never in that range
55 mut seq := 0
56 if input[i] == 0x1b && i + 1 < input.len && input[i + 1] == `[` {
57 seq = 2
58 } else if input[i] == `\\` && i + 4 < input.len && input[i + 1] == `0`
59 && input[i + 2] == `3` && input[i + 3] == `3` && input[i + 4] == `[` {
60 seq = 5
61 }
62 if seq > 0 {
63 i += seq
64 for i < input.len && !(input[i] >= 0x40 && input[i] <= 0x7e) {
65 i++
66 }
67 if i < input.len {
68 i++
69 }
70 continue
71 }
72 if input[i] < 0x80 {
73 result++
74 i++
75 continue
76 }
77 result += match east_asian.east_asian_width_property_at(input, i) {
78 .full, .wide { 2 }
79 else { 1 }
80 }
81 // a broken sequence advances by one byte, so the loop always ends
82 _, n := utf8.decode_rune_at(input, i)
83 i += n
84 }
85 return result
86}
87
88// table_text renders the rows: columns padded to their widest value, the
89// first line of every row starting with indent and the columns separated by
90// sep. the last column is padded too, and the trailing spaces are trimmed
91// straight away
92fn table_text(rows []Row, indent string, sep string) string {
93 if rows.len == 0 {
94 return ''
95 }
96 mut col_count := 0
97 for r in rows {
98 if r.cols.len > col_count {
99 col_count = r.cols.len
100 }
101 }
102 mut widths := []int{len: col_count, init: 0}
103 for r in rows {
104 for i, c in r.cols {
105 w := visible_width(c.value)
106 if widths[i] < w {
107 widths[i] = w
108 }
109 }
110 }
111 mut out := []string{}
112 for r in rows {
113 mut line := indent
114 for i, c in r.cols {
115 line += c.value
116 if i < r.cols.len - 1 {
117 line += ' '.repeat(widths[i] - visible_width(c.value)) + sep
118 }
119 }
120 out << line
121 }
122 return out.join('\n')
123}
124