-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtable.go
More file actions
295 lines (264 loc) · 6.76 KB
/
table.go
File metadata and controls
295 lines (264 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package tui
import (
"bytes"
"fmt"
"os"
"strings"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
"golang.org/x/term"
)
var (
styleBase = lipgloss.NewStyle().
Align(lipgloss.Left)
borderNone = table.Border{
Top: "",
Bottom: "─",
Left: "",
Right: "",
TopLeft: "",
TopRight: "",
BottomLeft: "",
BottomRight: "",
TopJunction: "",
BottomJunction: " ",
LeftJunction: "",
RightJunction: "",
InnerJunction: " ",
InnerDivider: " ",
}
)
const defaultTermWidth = 120
func getTerminalWidth() int {
w, _, err := term.GetSize(int(os.Stdout.Fd()))
if err != nil || w <= 0 {
return defaultTermWidth
}
return w
}
func NewTable(columns []table.Column, isStatic bool) *TableModel {
var newTable = table.Model{}
termWidth := getTerminalWidth()
if isStatic {
newTable = table.New(columns).WithFooterVisibility(false).
Border(borderNone).WithBaseStyle(styleBase).
WithTargetWidth(termWidth)
} else {
newTable = table.New(columns).Filtered(true).
Border(borderNone).Focused(true).WithPageSize(10).WithBaseStyle(styleBase).
WithTargetWidth(termWidth)
}
keyMap := table.DefaultKeyMap()
keyMap.RowSelectToggle = key.NewBinding(
key.WithKeys("enter"),
key.WithHelp("enter", "select row"),
)
newTable = newTable.WithKeyMap(keyMap)
newTable = newTable.HighlightStyle(SelectStyle)
t := &TableModel{
table: newTable,
Columns: columns,
Buffer: new(bytes.Buffer),
rowsPerPage: 10,
isStatic: isStatic,
}
return t
}
type TableModel struct {
table table.Model
Columns []table.Column
Rows []table.Row
AllRows []table.Row
*bytes.Buffer
currentPage int
totalPages int
rowsPerPage int
isStatic bool
filtered bool
handle func()
Title string
selected table.Row
highlightRows []int
searchString string
highlightStyle lipgloss.Style
}
func (t *TableModel) UpdatePagination() {
t.totalPages = (len(t.Rows) + t.rowsPerPage - 1) / t.rowsPerPage
if t.currentPage > t.totalPages {
t.currentPage = t.totalPages
}
if t.currentPage < 1 {
t.currentPage = 1
}
}
func (t *TableModel) Init() tea.Cmd { return nil }
func (t *TableModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
t.table = t.table.WithTargetWidth(msg.Width)
return t, nil
case tea.KeyMsg:
switch msg.Type {
case tea.KeyCtrlC, tea.KeyEsc, tea.KeyCtrlQ:
if len(t.highlightRows) > 0 {
t.CleanHighlight()
t.SetRows(t.AllRows)
return t, nil
}
return t, tea.Quit
case tea.KeyEnter:
t.selected = t.GetHighlightedRow()
if t.handle == nil {
return t, tea.Quit
}
t.handleSelectedRow()
return t, tea.Quit
}
}
t.table, cmd = t.table.Update(msg)
return t, tea.Batch(cmd)
}
func (t *TableModel) View() string {
if t.isStatic {
t.table.WithPageSize(len(t.Rows))
return fmt.Sprintf("%s\n", t.Title) + "\n" + t.table.View() + "\n"
}
return fmt.Sprintf("%s\n", t.Title) + "\n" + t.table.View() + "\n"
}
func (t *TableModel) SetRows(rows []table.Row) {
t.Rows = rows
t.autoFitColumns(rows)
t.table = t.table.WithRows(rows)
}
// autoFitColumns calculates optimal column widths based on actual cell content,
// then replaces columns with fixed-width columns sized to fit.
// Long content is capped per-column and left to multiline word-wrap.
func (t *TableModel) autoFitColumns(rows []table.Row) {
numCols := len(t.Columns)
if numCols == 0 || len(rows) == 0 {
return
}
termWidth := getTerminalWidth()
const padding = 1
const minWidth = 6
// InnerDivider is 1 char between columns
dividerOverhead := numCols - 1
availableWidth := termWidth - dividerOverhead
// Per-column max: adaptive based on column count
// Allow up to 2x average width, but at least 20% of terminal
maxColWidth := max(termWidth/numCols*2, termWidth*20/100)
// Calculate optimal width for each column: max(header, max_cell_content) + padding
widths := make([]int, numCols)
capped := make([]bool, numCols)
for i, col := range t.Columns {
widths[i] = lipgloss.Width(col.Title()) + padding
key := col.Key()
for _, row := range rows {
if capped[i] {
break
}
val, ok := row.Data[key]
if !ok {
continue
}
s := fmt.Sprint(val)
// Handle multiline cells: use the widest line
for _, line := range strings.Split(s, "\n") {
if w := lipgloss.Width(line) + padding; w > widths[i] {
widths[i] = w
}
}
// Early termination: cap reached, no need to scan more rows
if widths[i] >= maxColWidth {
widths[i] = maxColWidth
capped[i] = true
}
}
}
total := 0
for _, w := range widths {
total += w
}
if total > availableWidth {
// Proportionally compress, respecting a minimum width
ratio := float64(availableWidth) / float64(total)
newTotal := 0
for i := range widths {
widths[i] = max(int(float64(widths[i])*ratio), minWidth)
newTotal += widths[i]
}
// Distribute leftover space to the columns that lost the most
remaining := availableWidth - newTotal
for remaining > 0 {
bestIdx, bestLoss := 0, 0
for i := range widths {
orig := int(float64(widths[i]) / ratio)
loss := orig - widths[i]
if loss > bestLoss {
bestLoss = loss
bestIdx = i
}
}
if bestLoss == 0 {
break
}
widths[bestIdx]++
remaining--
}
}
// Rebuild columns with calculated widths
newCols := make([]table.Column, numCols)
for i, col := range t.Columns {
newCols[i] = table.NewColumn(col.Key(), col.Title(), widths[i])
if col.Filterable() {
newCols[i] = newCols[i].WithFiltered(true)
}
}
t.Columns = newCols
t.table = t.table.WithColumns(newCols).WithTargetWidth(termWidth)
}
func (t *TableModel) handleSelectedRow() {
t.handle()
}
func (t *TableModel) SetHandle(handle func()) {
t.handle = handle
}
func (t *TableModel) CleanHighlight() {
t.highlightRows = []int{}
t.searchString = ""
}
func (t *TableModel) SetMultiline() {
t.table = t.table.WithMultiline(true)
}
func (t *TableModel) GetSelectedRow() table.Row {
selectedRow := t.selected
return selectedRow
}
func (t *TableModel) GetHighlightedRow() table.Row {
selectedRow := t.table.HighlightedRow()
return selectedRow
}
func (t *TableModel) pageView(s string) string {
style := lipgloss.NewStyle().Inline(true).Align(lipgloss.Left)
return style.Render(s)
}
func (t *TableModel) SetAscSort(s string) {
t.table = t.table.SortByAsc(s)
}
func (t *TableModel) SetDescSort(s string) {
t.table = t.table.SortByDesc(s)
}
func (t *TableModel) Run() error {
p := tea.NewProgram(t)
_, err := p.Run()
if err != nil {
return err
}
fmt.Printf(HelpStyle("<Press enter to exit>\n"))
os.Stdin.Write([]byte("\n"))
ClearLines(1)
return nil
}