forked from charmbracelet/kancli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
284 lines (251 loc) · 5.98 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
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
package main
import (
"log"
"github.com/charmbracelet/bubbles/list"
"github.com/charmbracelet/bubbles/textarea"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
type status int
const divisor = 4
const (
todo status = iota
inProgress
done
)
/* MODEL MANAGEMENT */
var models []tea.Model
const (
model status = iota
form
)
/* STYLING */
var (
columnStyle = lipgloss.NewStyle().
Padding(1, 2).
Border(lipgloss.HiddenBorder())
focusedStyle = lipgloss.NewStyle().
Padding(1, 2).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62"))
)
/* CUSTOM ITEM */
type Task struct {
status status
title string
description string
}
func NewTask(status status, title, description string) Task {
return Task{status: status, title: title, description: description}
}
func (t *Task) Next() {
if t.status == done {
t.status = todo
} else {
t.status++
}
}
// implement the list.Item interface
func (t Task) FilterValue() string {
return t.title
}
func (t Task) Title() string {
return t.title
}
func (t Task) Description() string {
return t.description
}
/* MAIN MODEL */
type Model struct {
loaded bool
focused status
lists []list.Model
quitting bool
}
func (m *Model) MoveToNext() tea.Msg {
selectedItem := m.lists[m.focused].SelectedItem()
if selectedItem != nil {
selectedTask := selectedItem.(Task)
m.lists[selectedTask.status].RemoveItem(m.lists[m.focused].Index())
selectedTask.Next()
m.lists[selectedTask.status].InsertItem(len(m.lists[selectedTask.status].Items())-1, list.Item(selectedTask))
}
return nil
}
func (m *Model) DeleteCurrent() tea.Msg {
if len(m.lists[m.focused].VisibleItems()) > 0 {
selectedTask := m.lists[m.focused].SelectedItem().(Task)
m.lists[selectedTask.status].RemoveItem(m.lists[m.focused].Index())
}
return nil
}
func (m *Model) Next() {
if m.focused == done {
m.focused = todo
} else {
m.focused++
}
}
func (m *Model) Prev() {
if m.focused == todo {
m.focused = done
} else {
m.focused--
}
}
func (m *Model) initLists(width, height int) {
defaultList := list.New([]list.Item{}, list.NewDefaultDelegate(), width/divisor, height/2)
defaultList.SetShowHelp(false)
m.lists = []list.Model{defaultList, defaultList, defaultList}
// Init To Do
m.lists[todo].Title = "To Do"
m.lists[todo].SetItems([]list.Item{
Task{status: todo, title: "buy milk", description: "strawberry milk"},
Task{status: todo, title: "eat sushi", description: "negitoro roll, miso soup, rice"},
Task{status: todo, title: "fold laundry", description: "or wear wrinkly t-shirts"},
})
// Init in progress
m.lists[inProgress].Title = "In Progress"
m.lists[inProgress].SetItems([]list.Item{
Task{status: inProgress, title: "write code", description: "don't worry, it's Go"},
})
// Init done
m.lists[done].Title = "Done"
m.lists[done].SetItems([]list.Item{
Task{status: done, title: "stay cool", description: "as a cucumber"},
})
}
func (m Model) Init() tea.Cmd {
return nil
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
if !m.loaded {
columnStyle.Width(msg.Width / divisor)
focusedStyle.Width(msg.Width / divisor)
columnStyle.Height(msg.Height - divisor)
focusedStyle.Height(msg.Height - divisor)
m.initLists(msg.Width, msg.Height)
m.loaded = true
}
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
m.quitting = true
return m, tea.Quit
case "left", "h":
m.Prev()
case "right", "l":
m.Next()
case "enter":
return m, m.MoveToNext
case "n":
models[model] = m // save the state of the current model
models[form] = NewForm(m.focused)
return models[form].Update(nil)
case "d":
return m, m.DeleteCurrent
}
case Task:
task := msg
return m, m.lists[task.status].InsertItem(len(m.lists[task.status].Items()), task)
}
var cmd tea.Cmd
m.lists[m.focused], cmd = m.lists[m.focused].Update(msg)
return m, cmd
}
func (m Model) View() string {
if m.quitting {
return ""
}
if m.loaded {
todoView := m.lists[todo].View()
inProgView := m.lists[inProgress].View()
doneView := m.lists[done].View()
switch m.focused {
case inProgress:
return lipgloss.JoinHorizontal(
lipgloss.Left,
columnStyle.Render(todoView),
focusedStyle.Render(inProgView),
columnStyle.Render(doneView),
)
case done:
return lipgloss.JoinHorizontal(
lipgloss.Left,
columnStyle.Render(todoView),
columnStyle.Render(inProgView),
focusedStyle.Render(doneView),
)
default:
return lipgloss.JoinHorizontal(
lipgloss.Left,
focusedStyle.Render(todoView),
columnStyle.Render(inProgView),
columnStyle.Render(doneView),
)
}
} else {
return "loading..."
}
}
/* FORM MODEL */
type Form struct {
focused status
title textinput.Model
description textarea.Model
}
func NewForm(focused status) *Form {
form := Form{focused: focused}
form.title = textinput.New()
form.title.Focus()
form.description = textarea.New()
return &form
}
func (m Form) CreateTask() tea.Msg {
return Task{
m.focused, m.title.Value(), m.description.Value(),
}
}
func (m Form) Init() tea.Cmd {
return nil
}
func (m Form) 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":
return m, tea.Quit
case "enter":
if m.title.Focused() {
m.title.Blur()
m.description.Focus()
return m, textarea.Blink
} else {
models[form] = m
return models[model], m.CreateTask
}
}
}
if m.title.Focused() {
m.title, cmd = m.title.Update(msg)
return m, cmd
} else {
m.description, cmd = m.description.Update(msg)
return m, cmd
}
}
func (m Form) View() string {
return lipgloss.JoinVertical(lipgloss.Left, m.title.View(), m.description.View())
}
func main() {
models = []tea.Model{&Model{}, NewForm(todo)}
m := models[model]
p := tea.NewProgram(m)
if err := p.Start(); err != nil {
log.Fatal(err)
}
}