-
Notifications
You must be signed in to change notification settings - Fork 0
/
update.go
executable file
·125 lines (114 loc) · 2.44 KB
/
update.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
package main
import tea "github.com/charmbracelet/bubbletea"
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
switch m.state {
case 1:
// List
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "n":
if m.cursor < len(m.TaskList)-1 {
currentTask := m.TaskList[m.cursor]
replacedTask := m.TaskList[m.cursor+1]
m.TaskList[m.cursor] = replacedTask
m.TaskList[m.cursor+1] = currentTask
m.cursor++
}
case "m":
if m.cursor > 0 {
currentTask := m.TaskList[m.cursor]
replacedTask := m.TaskList[m.cursor-1]
m.TaskList[m.cursor] = replacedTask
m.TaskList[m.cursor-1] = currentTask
m.cursor--
}
case "j", "down":
if m.cursor < len(m.TaskList)-1 {
m.cursor++
}
case "k", "up":
if m.cursor > 0 {
m.cursor--
}
case "G", "pgdown":
m.cursor = len(m.TaskList) - 1
case "g", "pgup":
m.cursor = 0
case " ":
m.TaskList[m.cursor].IsSelected = !m.TaskList[m.cursor].IsSelected
case "q":
Save(&m)
return m, tea.Quit
case "a":
m.state = 2
case "d":
if len(m.TaskList) > 0 {
m.TaskList = append(m.TaskList[:m.cursor], m.TaskList[m.cursor+1:]...)
if m.cursor > len(m.TaskList)-1 {
m.cursor = len(m.TaskList) - 1
}
}
case "0":
m.TaskList[m.cursor].Rating = 0
case "1":
if m.TaskList[m.cursor].Rating == 1 {
m.TaskList[m.cursor].Rating = 0
} else {
m.TaskList[m.cursor].Rating = 1
}
case "2":
if m.TaskList[m.cursor].Rating == 2 {
m.TaskList[m.cursor].Rating = 0
} else {
m.TaskList[m.cursor].Rating = 2
}
}
case errMsg:
m.err = msg
return m, nil
}
case 2:
// add new element
m.textInput, cmd = m.textInput.Update(msg)
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "enter":
if m.textInput.Value() > "" {
addNew(&m, task{m.textInput.Value(), false, 0})
m.textInput.Reset()
Save(&m)
m.state = 1
}
case "esc":
m.state = 1
m.textInput.Reset()
}
case errMsg:
m.err = msg
return m, nil
}
}
// HACK
// ALL states
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctr+c":
Save(&m)
return m, tea.Quit
}
case errMsg:
m.err = msg
return m, nil
}
m.termHeight = 88
return m, cmd
}
func addNew(m *model, newTask task) {
m.TaskList = append(m.TaskList, newTask)
m.cursor = 0
m.state = 1
}