-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.go
More file actions
296 lines (252 loc) · 6.37 KB
/
ui.go
File metadata and controls
296 lines (252 loc) · 6.37 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
296
package main
import (
"fmt"
"sort"
"strings"
"time"
"github.com/charmbracelet/bubbles/table"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type CmdSelector struct {
cmds []string
cursor int
selected string
quit bool
rerun bool
}
func NewCmdSelector(cmds []string) *CmdSelector {
return &CmdSelector{
cmds: cmds,
cursor: 0,
selected: "",
quit: false,
rerun: false,
}
}
func (m *CmdSelector) Init() tea.Cmd {
return nil
}
func (m *CmdSelector) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
m.quit = true
return m, tea.Quit
case "up", "k":
if m.cursor > 0 {
m.cursor--
} else {
m.cursor = len(m.cmds) - 1
}
case "down", "j":
if m.cursor < len(m.cmds)-1 {
m.cursor++
} else {
m.cursor = 0
}
case "r":
m.rerun = true
return m, tea.Quit
case "enter", " ":
m.selected = m.cmds[m.cursor]
return m, tea.Quit
}
}
return m, nil
}
// Colors
var (
MutedGray = lipgloss.Color("#A1A1AA")
MutedPurpleBlue = lipgloss.Color("#5A3FC0")
NeuralGrey = lipgloss.Color("#BDBDBD")
SlateBlue = lipgloss.Color("#64748B")
SoftGreen = lipgloss.Color("#6FCF97")
WarmOrange = lipgloss.Color("#F4A261")
White = lipgloss.Color("#FFFFFF")
)
// Styles
var (
TitleStyle = lipgloss.NewStyle()
ItemStyle = lipgloss.NewStyle().Padding(0, 1)
SelectedItemStyle = lipgloss.NewStyle().Foreground(White).Background(SlateBlue).Padding(0, 1)
CheckedStyle = lipgloss.NewStyle().Foreground(SoftGreen)
UncheckedStyle = lipgloss.NewStyle().Foreground(NeuralGrey)
HelpStyle = lipgloss.NewStyle().Foreground(MutedGray)
KeyStyle = lipgloss.NewStyle().Foreground(WarmOrange).Bold(true)
TableHeaderStyle = lipgloss.NewStyle().Foreground(SoftGreen).Bold(true)
)
// keybindings
var (
NavigateKey1 = KeyStyle.Render("↑/↓")
NavigateKey2 = KeyStyle.Render("k/j")
ProceedKey = KeyStyle.Render("Enter")
RerunKey = KeyStyle.Render("r")
DeleteKey1 = KeyStyle.Render("Backspace")
DeleteKey2 = KeyStyle.Render("d")
ExitKey1 = KeyStyle.Render("Ctrl+c")
ExitKey2 = KeyStyle.Render("q")
)
// words
var (
Use = HelpStyle.Render("Use")
Press = HelpStyle.Render("Press")
Or = HelpStyle.Render("or")
ToNavigate = HelpStyle.Render("to navigate")
ToProceed = HelpStyle.Render("to proceed")
ToExit = HelpStyle.Render("to exit")
ToDelete = HelpStyle.Render("to delete entry")
ToRerun = HelpStyle.Render("to rerun")
)
// help messages
var (
Navigate = fmt.Sprintf(" %s %s %s %s %s\n", Use, NavigateKey1, Or, NavigateKey2, ToNavigate)
Proceed = fmt.Sprintf(" %s %s %s\n", Press, ProceedKey, ToProceed)
Rerun = fmt.Sprintf(" %s %s %s\n", Press, RerunKey, ToRerun)
Delete = fmt.Sprintf(" %s %s %s %s %s\n", Press, DeleteKey1, Or, DeleteKey2, ToDelete)
Exit = fmt.Sprintf(" %s %s %s %s %s\n", Press, ExitKey1, Or, ExitKey2, ToExit)
)
func (m *CmdSelector) View() string {
s := "\nChoose a command:\n"
for i, choice := range m.cmds {
cursor := " "
style := ItemStyle
if i == m.cursor {
cursor = ">"
style = SelectedItemStyle
}
s += fmt.Sprintf("%s %s\n", cursor, style.Render(choice))
}
return s + "\n\n" + Navigate + Rerun + Proceed + Exit
}
func SelectCmd(cmds []CmdEntry) (string, error) {
maxCmdLength := 0
for _, entry := range cmds {
if len(entry.Cmd) > maxCmdLength {
maxCmdLength = len(entry.Cmd)
}
}
commentedCmds := make([]string, len(cmds))
for i, entry := range cmds {
if entry.Comment != "" {
padding := strings.Repeat(" ", maxCmdLength-len(entry.Cmd)+2)
commentedCmds[i] = fmt.Sprintf("%s%s# %s", entry.Cmd, padding, entry.Comment)
} else {
commentedCmds[i] = entry.Cmd
}
}
model := NewCmdSelector(commentedCmds)
p := tea.NewProgram(model)
_, err := p.Run()
if err != nil {
return "", err
}
if model.quit {
return "", QuitError{}
}
if model.rerun {
return "", RerunError{}
}
return cmds[model.cursor].Cmd, nil
}
type Table struct {
table table.Model
quit bool
ogTotal float64
}
func (m Table) Init() tea.Cmd {
return nil
}
func (m Table) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
m.quit = true
return m, tea.Quit
case "backspace", "d":
selectedRow := m.table.SelectedRow()
if selectedRow[0] == "TOTAL" {
return m, nil
}
date := selectedRow[0]
if err := DeleteCostEntry(Today(date)); err != nil {
return m, nil
}
costs, err := GetCosts()
if err != nil {
return m, nil
}
newModel := NewTableModel(costs)
newModel.ogTotal = m.ogTotal
newModel.table.SetCursor(0)
rows := newModel.table.Rows()
for i, row := range rows {
if row[0] == "TOTAL" {
rows[i] = table.Row{"TOTAL", fmt.Sprintf("%.5f", m.ogTotal)}
break
}
}
newModel.table.SetRows(rows)
m.table = newModel.table
m.ogTotal = newModel.ogTotal
return m, nil
}
}
m.table, cmd = m.table.Update(msg)
return m, cmd
}
func (m Table) View() string {
return m.table.View() +
strings.Repeat("\n", 3) +
Navigate + Delete + Exit
}
func NewTableModel(costs Costs) Table {
columns := []table.Column{
{Title: "Date", Width: 15},
{Title: "Cost ($)", Width: 15},
}
rows := []table.Row{}
var totalCost float64
thisRepoIndex := 0
today := time.Now().Format("2006-01-02")
for date, cost := range costs {
rows = append(rows, table.Row{string(date), fmt.Sprintf("%.5f", cost)})
totalCost += float64(cost)
if string(date) == today {
thisRepoIndex = len(rows) - 1
} else {
thisRepoIndex++
}
}
rows = append(rows, table.Row{"TOTAL", fmt.Sprintf("%.5f", totalCost)})
sort.Slice(rows, func(i, j int) bool {
return rows[i][0] < rows[j][0]
})
t := table.New(
table.WithColumns(columns),
table.WithRows(rows),
table.WithFocused(true),
table.WithHeight(len(rows)+1),
)
t.SetCursor(thisRepoIndex)
s := table.DefaultStyles()
s.Header = TableHeaderStyle
s.Selected = SelectedItemStyle.Padding(0, 0)
t.SetStyles(s)
return Table{table: t, quit: false, ogTotal: totalCost}
}
func CostTableModel(costs Costs) error {
model := NewTableModel(costs)
p := tea.NewProgram(model)
_, err := p.Run()
if err != nil {
return err
}
if model.quit {
return QuitError{}
}
return nil
}