-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcategorize.go
44 lines (36 loc) · 1002 Bytes
/
categorize.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
package main
import (
"sort"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
lm "github.com/icco/lunchmoney"
)
func newCategorizeTransactionForm(categories []*lm.Category) *huh.Form {
sort.Slice(categories, func(i, j int) bool {
return categories[i].Name < categories[j].Name
})
opts := make([]huh.Option[int64], len(categories))
for i, c := range categories {
opts[i] = huh.NewOption(c.Name, c.ID)
}
return huh.NewForm(huh.NewGroup(
huh.NewSelect[int64]().
Title("New category").
Description("Select a new category for the transaction").
Options(opts...).
Key("category"),
))
}
func updateCategorizeTransaction(msg tea.Msg, m *model) (tea.Model, tea.Cmd) {
form, cmd := m.categoryForm.Update(msg)
if f, ok := form.(*huh.Form); ok {
m.categoryForm = f
}
if m.categoryForm.State == huh.StateCompleted {
m.sessionState = transactions
}
return m, cmd
}
func categorizeTransactionView(m model) string {
return m.categoryForm.View()
}