-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathmain.go
202 lines (168 loc) · 4.92 KB
/
main.go
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
// This file contains a full demo of most available features, for both testing
// and for reference
package main
import (
"fmt"
"log"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/evertras/bubble-table/table"
)
const (
columnKeyID = "id"
columnKeyName = "name"
columnKeyDescription = "description"
columnKeyCount = "count"
)
var (
customBorder = table.Border{
Top: "─",
Left: "│",
Right: "│",
Bottom: "─",
TopRight: "╮",
TopLeft: "╭",
BottomRight: "╯",
BottomLeft: "╰",
TopJunction: "╥",
LeftJunction: "├",
RightJunction: "┤",
BottomJunction: "╨",
InnerJunction: "╫",
InnerDivider: "║",
}
)
type Model struct {
tableModel table.Model
}
func NewModel() Model {
columns := []table.Column{
table.NewColumn(columnKeyID, "ID", 5).WithStyle(
lipgloss.NewStyle().
Faint(true).
Foreground(lipgloss.Color("#88f")).
Align(lipgloss.Center)),
table.NewColumn(columnKeyName, "Name", 10),
table.NewColumn(columnKeyDescription, "Description", 30),
table.NewColumn(columnKeyCount, "#", 5),
}
rows := []table.Row{
table.NewRow(table.RowData{
columnKeyID: "abc",
// Missing name
columnKeyDescription: "The first table entry, ever",
columnKeyCount: 4,
}),
table.NewRow(table.RowData{
columnKeyID: "123",
columnKeyName: "Oh no",
columnKeyDescription: "Super bold!",
columnKeyCount: 17,
}).WithStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("9")).Bold(true)),
table.NewRow(table.RowData{
columnKeyID: "def",
// Apply a style to this cell
columnKeyName: table.NewStyledCell("Styled", lipgloss.NewStyle().Foreground(lipgloss.Color("#8ff"))),
columnKeyDescription: "This is a really, really, really long description that will get cut off",
columnKeyCount: table.NewStyledCell(0, lipgloss.NewStyle().Faint(true)),
}),
table.NewRow(table.RowData{
columnKeyID: "spg",
columnKeyName: "Page 2",
columnKeyDescription: "Second page",
columnKeyCount: 2,
}),
table.NewRow(table.RowData{
columnKeyID: "spg2",
columnKeyName: "Page 2.1",
columnKeyDescription: "Second page again",
columnKeyCount: 4,
}),
}
// Start with the default key map and change it slightly, just for demoing
keys := table.DefaultKeyMap()
keys.RowDown.SetKeys("j", "down", "s")
keys.RowUp.SetKeys("k", "up", "w")
model := Model{
// Throw features in... the point is not to look good, it's just reference!
tableModel: table.New(columns).
WithRows(rows).
HeaderStyle(lipgloss.NewStyle().Foreground(lipgloss.Color("10")).Bold(true)).
SelectableRows(true).
Focused(true).
Border(customBorder).
WithKeyMap(keys).
WithStaticFooter("Footer!").
WithPageSize(3).
WithSelectedText(" ", "✓").
WithBaseStyle(
lipgloss.NewStyle().
BorderForeground(lipgloss.Color("#a38")).
Foreground(lipgloss.Color("#a7a")).
Align(lipgloss.Left),
).
SortByAsc(columnKeyID).
WithMissingDataIndicatorStyled(table.StyledCell{
Style: lipgloss.NewStyle().Foreground(lipgloss.Color("#faa")),
Data: "<ない>",
}),
}
model.updateFooter()
return model
}
func (m Model) Init() tea.Cmd {
return nil
}
func (m *Model) updateFooter() {
highlightedRow := m.tableModel.HighlightedRow()
footerText := fmt.Sprintf(
"Pg. %d/%d - Currently looking at ID: %s",
m.tableModel.CurrentPage(),
m.tableModel.MaxPages(),
highlightedRow.Data[columnKeyID],
)
m.tableModel = m.tableModel.WithStaticFooter(footerText)
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var (
cmd tea.Cmd
cmds []tea.Cmd
)
m.tableModel, cmd = m.tableModel.Update(msg)
cmds = append(cmds, cmd)
// We control the footer text, so make sure to update it
m.updateFooter()
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "esc", "q":
cmds = append(cmds, tea.Quit)
case "i":
m.tableModel = m.tableModel.WithHeaderVisibility(!m.tableModel.GetHeaderVisibility())
}
}
return m, tea.Batch(cmds...)
}
func (m Model) View() string {
body := strings.Builder{}
body.WriteString("A (chaotic) table demo with all features enabled!\n")
body.WriteString("Press left/right or page up/down to move pages\n")
body.WriteString("Press 'i' to toggle the header visibility\n")
body.WriteString("Press space/enter to select a row, q or ctrl+c to quit\n")
selectedIDs := []string{}
for _, row := range m.tableModel.SelectedRows() {
// Slightly dangerous type assumption but fine for demo
selectedIDs = append(selectedIDs, row.Data[columnKeyID].(string))
}
body.WriteString(fmt.Sprintf("SelectedIDs: %s\n", strings.Join(selectedIDs, ", ")))
body.WriteString(m.tableModel.View())
body.WriteString("\n")
return body.String()
}
func main() {
p := tea.NewProgram(NewModel())
if err := p.Start(); err != nil {
log.Fatal(err)
}
}