-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
280 lines (240 loc) · 6.07 KB
/
main.go
File metadata and controls
280 lines (240 loc) · 6.07 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
package main
import (
"fmt"
"os"
"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)
focusedStyle = lipgloss.NewStyle().
Padding(1, 2).
Border(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("62"))
)
// Task definition
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 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
}
// New main model
func New() *Model {
return &Model{}
}
// Move task to the next list
func (m *Model) MoveToNext() tea.Msg {
selectedItem := m.lists[m.focused].SelectedItem()
if selectedItem == nil {
return nil
}
selectedTask := selectedItem.(Task)
// Remove from current list
m.lists[m.focused].RemoveItem(m.lists[m.focused].Index())
// Advance status
selectedTask.Next()
// Insert into new list at the end
targetList := m.lists[selectedTask.status]
targetList.InsertItem(len(targetList.Items()), list.Item(selectedTask))
// Switch focus to the new list
m.focused = selectedTask.status
return nil
}
// Delete selected task
func (m *Model) DeleteTask() tea.Msg {
if selectedItem := m.lists[m.focused].SelectedItem(); selectedItem != nil {
m.lists[m.focused].RemoveItem(m.lists[m.focused].Index())
}
return nil
}
// Change focus
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--
}
}
// Initialize lists (empty) and disable the default help panel
func (m *Model) initLists(width, height int) {
listWidth := width / divisor
listHeight := height / 2 // start smaller, adjust dynamically
m.lists = []list.Model{
list.New([]list.Item{}, list.NewDefaultDelegate(), listWidth, listHeight),
list.New([]list.Item{}, list.NewDefaultDelegate(), listWidth, listHeight),
list.New([]list.Item{}, list.NewDefaultDelegate(), listWidth, listHeight),
}
for i := range m.lists {
m.lists[i].SetShowHelp(false) // REMOVE the default help/menu
}
m.lists[todo].Title = "To Do"
m.lists[inProgress].Title = "In Progress"
m.lists[done].Title = "Done"
}
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 / 2) // dynamic smaller height
focusedStyle.Height(msg.Height / 2) // dynamic smaller height
m.initLists(msg.Width, msg.Height)
m.loaded = true
}
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
m.quitting = true
return m, tea.Quit
case "left", "h":
m.Prev()
case "right", "l":
m.Next()
case "enter":
return m, m.MoveToNext
case "d":
return m, m.DeleteTask
case "n":
models[model] = m // save current
models[form] = NewForm(m.focused)
return models[form].Update(nil)
}
}
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),
)
}
}
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 NewTask(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) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c":
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
}
}
}
var cmd tea.Cmd
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{New(), NewForm(todo)}
m := models[model]
p := tea.NewProgram(m)
if err := p.Start(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}