-
Notifications
You must be signed in to change notification settings - Fork 33
/
tui.go
136 lines (116 loc) · 2.87 KB
/
tui.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
package cmd
import (
"fmt"
"io"
"github.com/charmbracelet/bubbles/list"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/j178/leetgo/leetcode"
)
var (
titleStyle = lipgloss.NewStyle().MarginLeft(2)
itemStyle = lipgloss.NewStyle().PaddingLeft(4)
selectedItemStyle = lipgloss.NewStyle().PaddingLeft(2).Foreground(lipgloss.Color("170"))
paginationStyle = list.DefaultStyles().PaginationStyle.PaddingLeft(4)
helpStyle = lipgloss.NewStyle().PaddingLeft(4).PaddingBottom(1)
// textStyle = lipgloss.NewStyle().Margin(1, 0, 2, 4)
)
type rowDelegate struct{}
func (d rowDelegate) Height() int {
return 1
}
func (d rowDelegate) Spacing() int {
return 0
}
func (d rowDelegate) Update(msg tea.Msg, m *list.Model) tea.Cmd {
return nil
}
func (d rowDelegate) Render(w io.Writer, m list.Model, index int, listItem list.Item) {
i, ok := listItem.(*item)
if !ok {
return
}
q := (*leetcode.QuestionData)(i)
// TODO improve display
str := q.GetTitle()
if index == m.Index() {
str = selectedItemStyle.Render("> " + str)
} else {
str = itemStyle.Render(str)
}
_, _ = fmt.Fprint(w, str)
}
type qsMsg []*leetcode.QuestionData
type item leetcode.QuestionData
func (i *item) FilterValue() string {
return (*leetcode.QuestionData)(i).GetTitle()
}
type tui struct {
filter leetcode.QuestionFilter
client leetcode.Client
idx int // nolint: unused
total int
hasMore bool
list *list.Model
selected *leetcode.QuestionData
}
func newTuiModel(filter leetcode.QuestionFilter, c leetcode.Client) *tui {
l := list.New(nil, rowDelegate{}, 60, 60)
l.Title = "Select a question"
l.SetShowStatusBar(true)
l.SetShowTitle(true)
l.Styles.Title = titleStyle
l.Styles.PaginationStyle = paginationStyle
l.Styles.HelpStyle = helpStyle
// TODO Implement a progressive loading list
return &tui{
filter: filter,
client: c,
list: &l,
}
}
func (m *tui) Selected() *leetcode.QuestionData {
return m.selected
}
func (m *tui) Init() tea.Cmd {
return func() tea.Msg {
qs, err := m.client.GetQuestionsByFilter(m.filter, 100, 0)
if err != nil {
return nil
}
m.total = qs.Total
m.hasMore = qs.HasMore
return qsMsg(qs.Questions)
}
}
func (m *tui) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
if m.list.SelectedItem() != nil {
m.selected = (*leetcode.QuestionData)(m.list.SelectedItem().(*item))
return m, tea.Quit
}
}
case tea.WindowSizeMsg:
if m.list == nil {
return m, nil
}
m.list.SetSize(msg.Width, msg.Height)
return m, nil
case qsMsg:
items := make([]list.Item, len(msg))
for i, q := range msg {
items[i] = (*item)(q)
}
m.list.SetItems(items)
return m, nil
}
lst, cmd := m.list.Update(msg)
m.list = &lst
return m, cmd
}
func (m *tui) View() string {
return "\n" + m.list.View()
}