-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.go
68 lines (55 loc) · 1.48 KB
/
list.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
package presentation
import (
"slices"
"strings"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/gabrielseibel1/fungo/apply"
"github.com/gabrielseibel1/godo/types"
)
type ListModel struct {
list list.Model
style lipgloss.Style
}
func NewListModel(style lipgloss.Style) ListModel {
m := ListModel{
list: list.New(make([]list.Item, 0), list.NewDefaultDelegate(), 0, 0),
style: style,
}
m.list.SetShowTitle(false)
return m
}
func (m ListModel) Init() tea.Cmd {
return nil
}
func (m ListModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.WindowSizeMsg:
h, v := m.style.GetFrameSize()
m.list.SetSize(msg.Width-h, msg.Height-v)
m.list, cmd = m.list.Update(msg)
return m, cmd
case itemsMsg:
slices.SortFunc(msg, func(a, b types.Actionable) int {
return strings.Compare(string(a.Identify()), string(b.Identify()))
})
items := apply.ToSlice(msg, func(a types.Actionable) list.Item { return UIItem{a} })
return m, m.list.SetItems(items)
}
m.list, cmd = m.list.Update(msg)
return m, cmd
}
func (m ListModel) View() string {
return m.style.Render(m.list.View())
}
func (m ListModel) Items() []UIItem {
return apply.ToSlice(m.list.Items(), func(i list.Item) UIItem { return i.(UIItem) })
}
func (m ListModel) Selected() types.Actionable {
if sel := m.list.SelectedItem(); sel != nil {
return sel.(UIItem).Actionable
}
return nil
}