forked from wtfutil/wtf
/
widget.go
276 lines (225 loc) · 5.84 KB
/
widget.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
package todo
import (
"fmt"
"io/ioutil"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/rivo/tview"
"github.com/senorprogrammer/wtf/wtf"
"gopkg.in/yaml.v2"
)
// Config is a pointer to the global config object
var Config *config.Config
const HelpText = `
Keyboard commands for Todo:
/: Show/hide this help window
j: Select the next item in the list
k: Select the previous item in the list
n: Create a new list item
o: Open the todo file in the operating system
arrow down: Select the next item in the list
arrow up: Select the previous item in the list
ctrl-d: Delete the selected item
esc: Unselect the todo list
return: Edit selected item
space: Check the selected item on or off
`
type Widget struct {
wtf.TextWidget
app *tview.Application
filePath string
list *List
pages *tview.Pages
}
func NewWidget(app *tview.Application, pages *tview.Pages) *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" 📝 Todo ", "todo", true),
app: app,
filePath: Config.UString("wtf.mods.todo.filename"),
list: &List{selected: -1},
pages: pages,
}
widget.init()
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
widget.UpdateRefreshedAt()
widget.load()
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
// edit opens a modal dialog that permits editing the text of the currently-selected item
func (widget *Widget) editItem() {
if widget.list.Selected() == nil {
return
}
form := widget.modalForm("Edit:", widget.list.Selected().Text)
saveFctn := func() {
text := form.GetFormItem(0).(*tview.InputField).GetText()
widget.list.Update(text)
widget.persist()
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
widget.display()
}
widget.addButtons(form, saveFctn)
widget.modalFocus(form)
}
func (widget *Widget) init() {
_, err := wtf.CreateFile(widget.filePath)
if err != nil {
panic(err)
}
}
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch string(event.Rune()) {
case " ":
// Check/uncheck selected item
widget.list.Toggle()
widget.persist()
widget.display()
return nil
case "/":
widget.showHelp()
return nil
case "j":
// Select the next item down
widget.list.Next()
widget.display()
return nil
case "k":
// Select the next item up
widget.list.Prev()
widget.display()
return nil
case "n":
// Add a new item
widget.newItem()
return nil
case "o":
// Open the file
wtf.OpenFile(widget.filePath)
return nil
}
switch event.Key() {
case tcell.KeyCtrlD:
// Delete the selected item
widget.list.Delete()
widget.persist()
widget.display()
return nil
case tcell.KeyCtrlJ:
// Move selected item down in the list
widget.list.Demote()
widget.persist()
widget.display()
return nil
case tcell.KeyCtrlK:
// Move selected item up in the list
widget.list.Promote()
widget.persist()
widget.display()
return nil
case tcell.KeyDown:
// Select the next item down
widget.list.Next()
widget.display()
return nil
case tcell.KeyEnter:
widget.editItem()
return nil
case tcell.KeyEsc:
// Unselect the current row
widget.list.Unselect()
widget.display()
return event
case tcell.KeyUp:
// Select the next item up
widget.list.Prev()
widget.display()
return nil
default:
// Pass it along
return event
}
}
// Loads the todo list from Yaml file
func (widget *Widget) load() {
confDir, _ := wtf.ConfigDir()
filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath)
fileData, _ := wtf.ReadFileBytes(filePath)
yaml.Unmarshal(fileData, &widget.list)
}
func (widget *Widget) newItem() {
form := widget.modalForm("New:", "")
saveFctn := func() {
text := form.GetFormItem(0).(*tview.InputField).GetText()
widget.list.Add(text)
widget.persist()
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
widget.display()
}
widget.addButtons(form, saveFctn)
widget.modalFocus(form)
}
// persist writes the todo list to Yaml file
func (widget *Widget) persist() {
confDir, _ := wtf.ConfigDir()
filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath)
fileData, _ := yaml.Marshal(&widget.list)
err := ioutil.WriteFile(filePath, fileData, 0644)
if err != nil {
panic(err)
}
}
func (widget *Widget) showHelp() {
closeFunc := func() {
widget.pages.RemovePage("help")
widget.app.SetFocus(widget.View)
}
modal := wtf.NewBillboardModal(HelpText, closeFunc)
widget.pages.AddPage("help", modal, false, true)
widget.app.SetFocus(modal)
}
/* -------------------- Modal Form -------------------- */
func (widget *Widget) addButtons(form *tview.Form, saveFctn func()) {
widget.addSaveButton(form, saveFctn)
widget.addCancelButton(form)
}
func (widget *Widget) addCancelButton(form *tview.Form) {
cancelFn := func() {
widget.pages.RemovePage("modal")
widget.app.SetFocus(widget.View)
widget.display()
}
form.AddButton("Cancel", cancelFn)
form.SetCancelFunc(cancelFn)
}
func (widget *Widget) addSaveButton(form *tview.Form, fctn func()) {
form.AddButton("Save", fctn)
}
func (widget *Widget) modalFocus(form *tview.Form) {
frame := widget.modalFrame(form)
widget.pages.AddPage("modal", frame, false, true)
widget.app.SetFocus(frame)
}
func (widget *Widget) modalForm(lbl, text string) *tview.Form {
form := tview.NewForm().
SetButtonsAlign(tview.AlignCenter).
SetButtonTextColor(tview.Styles.PrimaryTextColor)
form.AddInputField(lbl, text, 60, nil, nil)
return form
}
func (widget *Widget) modalFrame(form *tview.Form) *tview.Frame {
_, _, w, h := widget.View.GetInnerRect()
frame := tview.NewFrame(form).SetBorders(0, 0, 0, 0, 0, 0)
frame.SetBorder(true)
frame.SetRect(w+20, h+2, 80, 7)
return frame
}