-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtd.go
145 lines (116 loc) · 2.78 KB
/
td.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
package main
import (
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/yassernasc/td/models"
"github.com/yassernasc/td/state"
"github.com/yassernasc/td/ui"
)
type model struct {
cursor int
editMode bool
exiting bool
input textinput.Model
insertMode bool
todos []models.Todo
}
func initialModel() model {
todos := state.Load()
var initialInsertMode bool
if len(todos) == 0 {
initialInsertMode = true
}
return model{
input: state.CreatePrompt(),
todos: todos,
insertMode: initialInsertMode,
}
}
func (m model) Init() tea.Cmd {
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
if m.insertMode {
m.input, _ = m.input.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
state.AddNewTodo(&m.todos, m.input.Value())
m.input.Reset()
// cursor shows up when starts with no item and then one is added
m.cursor = -1
case "esc":
state.CursorFocusOnLast(&m.cursor, m.todos)
state.ExitPrompt(&m.input, &m.insertMode)
// nothing to show, exit
if len(m.todos) == 0 {
return m, tea.Quit
}
}
}
} else if m.editMode {
m.input, _ = m.input.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
state.EditTodo(&m.todos[m.cursor], m.input.Value())
state.ExitPrompt(&m.input, &m.editMode)
case "esc":
state.ExitPrompt(&m.input, &m.editMode)
}
}
} else {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "esc", "q", "ctrl+c":
m.exiting = true
return m, tea.Quit
case "up", "down":
state.UpdateCursor(&m.cursor, len(m.todos), msg.String())
case "shift+up", "ctrl+k": // added ctrl keybinding 'cause vhs limitations
state.ShiftUp(&m.cursor, &m.todos)
case "shift+down", "ctrl+j":
state.ShiftDown(&m.cursor, &m.todos)
case "enter", " ":
state.ToogleTodo(&m.todos[m.cursor])
case "backspace", "d", "r", "x":
state.MarkTodo(&m.todos[m.cursor])
case "c":
state.CleanTodos(&m.todos)
state.EnsureCursorIsVisible(&m.cursor, m.todos)
if len(m.todos) == 0 {
m.insertMode = true
}
case "a", "i":
m.cursor = -1
m.insertMode = true
case "e":
m.editMode = true
state.SetPromptByTodo(&m.input, m.todos[m.cursor])
}
}
}
return m, nil
}
func (m model) View() string {
if m.exiting {
pending := state.FilterPendingTodos(m.todos)
state.Save(pending)
return ui.Pending(pending)
}
todos := ui.Todos(m.todos, m.cursor)
prompt := m.input.View()
if m.editMode {
todos[m.cursor] = prompt
}
if m.insertMode {
todos = append(todos, prompt)
}
return ui.List(todos)
}
func main() {
tea.NewProgram(initialModel()).Run()
}