Debian Codegraph @

lib/ui/table.go

raw
1 package ui
2
3 import (
4 "math"
5 "regexp"
6
7 "git.sr.ht/~rjarry/aerc/config"
8 "github.com/mattn/go-runewidth"
9 "go.rockorager.dev/vaxis"
10 )
11
12 type Table struct {
13 Columns []Column
14 Rows []Row
15 Height int
16 // Optional callback that allows customizing the default drawing routine
17 // of table rows. If true is returned, the default routine is skipped.
18 CustomDraw func(t *Table, row int, c *Context) bool
19 // Optional callback that allows returning a custom style for the row.
20 GetRowStyle func(t *Table, row int) vaxis.Style
21
22 // true if at least one column has WIDTH_FIT
23 autoFitWidths bool
24 // if false, widths need to be computed before drawing
25 widthsComputed bool
26 }
27
28 type Column struct {
29 Offset int
30 Width int
31 Def *config.ColumnDef
32 Separator string
33 }
34
35 type Row struct {
36 Cells []string
37 Priv any
38 }
39
40 func NewTable(
41 height int,
42 columnDefs []*config.ColumnDef, separator string,
43 customDraw func(*Table, int, *Context) bool,
44 getRowStyle func(*Table, int) vaxis.Style,
45 ) Table {
46 if customDraw == nil {
47 customDraw = func(*Table, int, *Context) bool { return false }
48 }
49 if getRowStyle == nil {
50 getRowStyle = func(*Table, int) vaxis.Style {
51 return vaxis.Style{}
52 }
53 }
54 columns := make([]Column, len(columnDefs))
55 autoFitWidths := false
56 for c, col := range columnDefs {
57 if col.Flags.Has(config.WIDTH_FIT) {
58 autoFitWidths = true
59 }
60 columns[c] = Column{Def: col}
61 if c != len(columns)-1 {
62 // set separator for all columns except the last one
63 columns[c].Separator = separator
64 }
65 }
66 return Table{
67 Columns: columns,
68 Height: height,
69 CustomDraw: customDraw,
70 GetRowStyle: getRowStyle,
71 autoFitWidths: autoFitWidths,
72 }
73 }
74
75 // add a row to the table, returns true when the table is full
76 func (t *Table) AddRow(cells []string, priv any) bool {
77 if len(cells) != len(t.Columns) {
78 panic("invalid number of cells")
79 }
80 if len(t.Rows) >= t.Height {
81 return true
82 }
83 t.Rows = append(t.Rows, Row{Cells: cells, Priv: priv})
84 if t.autoFitWidths {
85 t.widthsComputed = false
86 }
87 return len(t.Rows) >= t.Height
88 }
89
90 func (t *Table) computeWidths(width int) {
91 contentMaxWidths := make([]int, len(t.Columns))
92 if t.autoFitWidths {
93 for _, row := range t.Rows {
94 for c := range t.Columns {
95 buf := StyledString(row.Cells[c])
96 if buf.Len() > contentMaxWidths[c] {
97 contentMaxWidths[c] = buf.Len()
98 }
99 }
100 }
101 }
102
103 nonFixed := width
104 autoWidthCount := 0
105 for c := range t.Columns {
106 col := &t.Columns[c]
107 switch {
108 case col.Def.Flags.Has(config.WIDTH_FIT):
109 col.Width = contentMaxWidths[c]
110 // compensate for exact width columns
111 col.Width += runewidth.StringWidth(col.Separator)
112 case col.Def.Flags.Has(config.WIDTH_EXACT):
113 col.Width = int(math.Round(col.Def.Width))
114 // compensate for exact width columns
115 col.Width += runewidth.StringWidth(col.Separator)
116 case col.Def.Flags.Has(config.WIDTH_AUTO):
117 col.Width = 0
118 autoWidthCount += 1
119 case col.Def.Flags.Has(config.WIDTH_FRACTION):
120 col.Width = int(math.Round(float64(width) * col.Def.Width))
121 }
122 nonFixed -= col.Width
123 }
124
125 autoWidth := 0
126 if autoWidthCount > 0 && nonFixed > 0 {
127 autoWidth = nonFixed / autoWidthCount
128 if autoWidth == 0 {
129 autoWidth = 1
130 }
131 }
132
133 offset := 0
134 remain := width
135 for c := range t.Columns {
136 col := &t.Columns[c]
137 if col.Def.Flags.Has(config.WIDTH_AUTO) && autoWidth > 0 {
138 col.Width = autoWidth
139 if nonFixed >= 2*autoWidth {
140 nonFixed -= autoWidth
141 }
142 }
143 if remain == 0 {
144 // column is outside of screen
145 col.Width = -1
146 } else if col.Width > remain {
147 // limit width to avoid overflow
148 col.Width = remain
149 }
150 remain -= col.Width
151 col.Offset = offset
152 offset += col.Width
153 // reserve room for separator
154 col.Width -= runewidth.StringWidth(col.Separator)
155 }
156 }
157
158 var metaCharsRegexp = regexp.MustCompile(`[\t\r\f\n\v]`)
159
160 func (col *Column) alignCell(cell string) string {
161 cell = metaCharsRegexp.ReplaceAllString(cell, " ")
162 buf := StyledString(cell)
163 width := buf.Len()
164
165 switch {
166 case col.Def.Flags.Has(config.ALIGN_LEFT):
167 if width < col.Width {
168 PadRight(buf, col.Width)
169 cell = buf.Encode()
170 } else if width > col.Width {
171 Truncate(buf, col.Width)
172 cell = buf.Encode()
173 }
174 case col.Def.Flags.Has(config.ALIGN_CENTER):
175 if width < col.Width {
176 pad := col.Width - width
177 PadLeft(buf, col.Width-(pad/2))
178 PadRight(buf, col.Width)
179 cell = buf.Encode()
180 } else if width > col.Width {
181 Truncate(buf, col.Width)
182 cell = buf.Encode()
183 }
184 case col.Def.Flags.Has(config.ALIGN_RIGHT):
185 if width < col.Width {
186 PadLeft(buf, col.Width)
187 cell = buf.Encode()
188 } else if width > col.Width {
189 TruncateHead(buf, col.Width)
190 cell = buf.Encode()
191 }
192 }
193
194 return cell
195 }
196
197 func (t *Table) Draw(ctx *Context) {
198 if !t.widthsComputed {
199 t.computeWidths(ctx.Width())
200 t.widthsComputed = true
201 }
202 for r, row := range t.Rows {
203 if t.CustomDraw(t, r, ctx) {
204 continue
205 }
206 for c, col := range t.Columns {
207 if col.Width == -1 {
208 // column overflows screen width
209 continue
210 }
211 cell := col.alignCell(row.Cells[c])
212 style := t.GetRowStyle(t, r)
213
214 buf := StyledString(cell)
215 ApplyAttrs(buf, style)
216 cell = buf.Encode()
217 ctx.Printf(col.Offset, r, style, "%s%s", cell, col.Separator)
218 }
219 }
220 }