-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.go
More file actions
82 lines (69 loc) · 1.73 KB
/
table.go
File metadata and controls
82 lines (69 loc) · 1.73 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
package main
import (
"strings"
"github.com/charmbracelet/bubbles/table"
"github.com/charmbracelet/lipgloss"
)
const maxColWidth = 22
func RenderTable(t *table.Model) string {
style := lipgloss.NewStyle().BorderStyle(lipgloss.RoundedBorder())
if t.Focused() {
style = style.BorderForeground(lipgloss.Color(Theme().primary))
} else {
style = style.BorderForeground(lipgloss.Color("240"))
}
return style.Render(t.View())
}
func tableStyle() table.Styles {
return table.Styles{
Selected: lipgloss.NewStyle().Bold(true).Background(Theme().primary).Foreground(lipgloss.Color("#FFFFFF")),
Header: lipgloss.NewStyle().Bold(true).Padding(0, 1),
Cell: lipgloss.NewStyle().Padding(0, 1),
}
}
func toTable(results FeedbackResults) ([]table.Column, []table.Row) {
if len(results) == 0 {
return []table.Column{}, []table.Row{}
}
r := results[0]
cols := r.Columns()
rows := make([]table.Row, 0)
columns := make([]table.Column, len(cols))
widths := make([]int, len(cols))
for _, r := range results {
r := r.ToRow()
for k, s := range r {
ls := len(s)
if ls > maxColWidth {
r[k] = s[:(maxColWidth-1)] + "…"
widths[k] = maxColWidth
} else if ls > widths[k] {
widths[k] = ls
}
}
rows = append(rows, r)
}
w := 4
for k, c := range cols {
title := strings.ToUpper(c)
if len(title) > widths[k] {
widths[k] = len(title)
}
columns[k] = table.Column{
Title: title, Width: widths[k],
}
w += widths[k] + 1
}
// fmt.Println(w)
return columns, rows
}
func NewTable(results FeedbackResults) table.Model {
columns, rows := toTable(results)
return table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithWidth(80),
table.WithHeight(10),
table.WithStyles(tableStyle()),
)
}