This repository has been archived by the owner on Jan 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
ui.go
260 lines (236 loc) · 5.83 KB
/
ui.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
package main
import (
"io/ioutil"
"os"
"os/exec"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)
type Lanes struct {
content *Content
lanes []*tview.List
active int
pages *tview.Pages
app *tview.Application
inselect bool
add *ModalInput
edit *ModalInput
}
func NewLanes(content *Content, app *tview.Application) *Lanes {
l := &Lanes{content, make([]*tview.List, content.GetNumLanes()), 0, tview.NewPages(), app, false, NewModalInput(), NewModalInput()}
flex := tview.NewFlex()
for i := 0; i < l.content.GetNumLanes(); i++ {
l.lanes[i] = tview.NewList()
l.lanes[i].ShowSecondaryText(false).SetBorder(true)
l.lanes[i].SetTitle(l.content.GetLaneTitle(i))
l.lanes[i].SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyUp:
if l.inselect {
l.up()
return nil
}
case tcell.KeyDown:
if l.inselect {
l.down()
return nil
}
case tcell.KeyLeft:
if l.inselect {
l.left()
} else {
l.decActive()
}
return nil
case tcell.KeyRight:
if l.inselect {
l.right()
} else {
l.incActive()
}
return nil
}
switch event.Rune() {
case 'q':
l.pages.ShowPage("quit")
case 'd':
l.pages.ShowPage("delete")
case 'a':
l.add.SetValue("")
l.pages.ShowPage("add")
case 'e':
if item := l.currentItem(); item != nil {
l.edit.SetValue(item.Title)
l.pages.ShowPage("edit")
}
case 'n':
app.Suspend(l.editNote)
}
return event
})
l.lanes[i].SetSelectedFunc(func(w int, x string, y string, z rune) {
l.selected()
})
l.lanes[i].SetDoneFunc(func() {
// Cancel select on Done (escape)
if l.inselect {
l.selected()
}
})
for _, item := range l.content.GetLaneItems(i) {
l.lanes[i].AddItem(item.Title, "", 0, nil)
}
flex.AddItem(l.lanes[i], 0, 1, i == 0)
}
l.pages.AddPage("lanes", flex, true, true)
quit := tview.NewModal().
SetText("Do you want to quit the application?").
AddButtons([]string{"Quit", "Cancel"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Quit" {
app.Stop()
}
l.pages.HidePage("quit")
})
l.pages.AddPage("quit", quit, false, false)
delete := tview.NewModal().
SetText("Are you sure?").
AddButtons([]string{"Yes", "No"}).
SetDoneFunc(func(buttonIndex int, buttonLabel string) {
if buttonLabel == "Yes" {
item := l.lanes[l.active].GetCurrentItem()
l.content.DelItem(l.active, item)
l.redraw(l.active, item)
}
l.pages.HidePage("delete")
l.setActive()
})
l.pages.AddPage("delete", delete, false, false)
l.add.SetDoneFunc(func(text string, success bool) {
if success {
item := l.lanes[l.active].GetCurrentItem()
l.content.AddItem(l.active, item, text)
l.redraw(l.active, item)
}
l.pages.HidePage("add")
l.setActive()
})
l.pages.AddPage("add", l.add, false, false)
l.edit.SetDoneFunc(func(text string, success bool) {
if success {
item := l.lanes[l.active].GetCurrentItem()
itemVal := l.currentItem()
itemVal.Title = text
l.redraw(l.active, item)
}
l.pages.HidePage("edit")
l.setActive()
})
l.pages.AddPage("edit", l.edit, false, false)
return l
}
func (l *Lanes) selected() {
if l.inselect {
l.lanes[l.active].SetSelectedBackgroundColor(tcell.ColorWhite)
} else {
l.lanes[l.active].SetSelectedBackgroundColor(tcell.ColorGreen)
}
l.inselect = !l.inselect
}
func (l *Lanes) redraw(lane, active int) {
l.lanes[lane].Clear()
for _, item := range l.content.GetLaneItems(lane) {
l.lanes[lane].AddItem(item.Title, "", 0, nil)
}
num := l.lanes[lane].GetItemCount()
if num > 0 {
l.lanes[lane].SetCurrentItem(normPos(active, num))
}
}
func (l *Lanes) up() {
currentPos := l.lanes[l.active].GetCurrentItem()
newPos := normPos(currentPos-1, l.lanes[l.active].GetItemCount())
l.content.MoveItem(l.active, currentPos, l.active, newPos)
l.redraw(l.active, newPos)
}
func (l *Lanes) down() {
currentPos := l.lanes[l.active].GetCurrentItem()
newPos := normPos(currentPos+1, l.lanes[l.active].GetItemCount())
l.content.MoveItem(l.active, currentPos, l.active, newPos)
l.redraw(l.active, newPos)
}
func (l *Lanes) left() {
currentPos := l.lanes[l.active].GetCurrentItem()
newLane := normPos(l.active-1, len(l.lanes))
newPos := l.lanes[newLane].GetCurrentItem()
l.content.MoveItem(l.active, currentPos, newLane, newPos)
l.redraw(l.active, currentPos)
l.redraw(newLane, newPos)
l.selected()
l.decActive()
l.selected()
}
func (l *Lanes) right() {
currentPos := l.lanes[l.active].GetCurrentItem()
newLane := normPos(l.active+1, len(l.lanes))
newPos := l.lanes[newLane].GetCurrentItem()
l.content.MoveItem(l.active, currentPos, newLane, newPos)
l.redraw(l.active, currentPos)
l.redraw(newLane, newPos)
l.selected()
l.incActive()
l.selected()
}
func (l *Lanes) decActive() {
l.active--
l.setActive()
}
func (l *Lanes) incActive() {
l.active++
l.setActive()
}
func normPos(pos, length int) int {
for pos < 0 {
pos += length
}
pos %= length
return pos
}
func (l *Lanes) setActive() {
l.active = normPos(l.active, len(l.lanes))
l.app.SetFocus(l.lanes[l.active])
}
func (l *Lanes) currentItem() *Item {
pos := l.lanes[l.active].GetCurrentItem()
content := l.content.GetLaneItems(l.active)
if pos < 0 || pos >= len(content) {
return nil
}
return &content[pos]
}
func (l *Lanes) editNote() {
item := l.currentItem()
if item != nil {
tmp, err := ioutil.TempFile("", "toukan")
if err == nil {
name := tmp.Name()
defer os.Remove(name)
tmp.Write([]byte(item.Note))
tmp.Close()
cmd := exec.Command("vim", name)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err = cmd.Run()
if err == nil {
note_raw, err := ioutil.ReadFile(name)
if err == nil {
item.Note = string(note_raw)
}
}
}
}
}
func (l *Lanes) GetUi() *tview.Pages {
return l.pages
}