Skip to content

Commit a9c5dc3

Browse files
WTF-427 Add KeyboardWidget and switch Todo widget to using it
1 parent b22a9ea commit a9c5dc3

5 files changed

Lines changed: 149 additions & 88 deletions

File tree

‎cfg/common_settings.go‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,11 @@ func NewCommonSettingsFromModule(name, defaultTitle string, moduleConfig *config
104104
common.Colors.Rows.Even = moduleConfig.UString("colors.rows.even", moduleConfig.UString("rows.even", "white"))
105105
common.Colors.Rows.Odd = moduleConfig.UString("colors.rows.even", moduleConfig.UString("rows.odd", "lightblue"))
106106

107-
common.Sigils.Checkbox.Checked = globalSettings.UString(sigilsPath+".Checkbox.Checked", "x")
108-
common.Sigils.Checkbox.Unchecked = globalSettings.UString(sigilsPath+".Checkbox.Unchecked", " ")
107+
common.Sigils.Checkbox.Checked = globalSettings.UString(sigilsPath+".checkbox.checked", "x")
108+
common.Sigils.Checkbox.Unchecked = globalSettings.UString(sigilsPath+".checkbox.unchecked", " ")
109109

110-
common.Sigils.Paging.Normal = globalSettings.UString(sigilsPath+".Paging.Normal", globalSettings.UString("wtf.paging.pageSigil", "*"))
111-
common.Sigils.Paging.Selected = globalSettings.UString(sigilsPath+".Paging.Select", globalSettings.UString("wtf.paging.selectedSigil", "_"))
110+
common.Sigils.Paging.Normal = globalSettings.UString(sigilsPath+".paging.normal", globalSettings.UString("wtf.paging.pageSigil", "*"))
111+
common.Sigils.Paging.Selected = globalSettings.UString(sigilsPath+".paging.select", globalSettings.UString("wtf.paging.selectedSigil", "_"))
112112

113113
return &common
114114
}

‎go.sum‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ google.golang.org/genproto v0.0.0-20181202183823-bd91e49a0898/go.mod h1:7Ep/1NZk
140140
google.golang.org/grpc v1.14.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
141141
google.golang.org/grpc v1.16.0/go.mod h1:0JHn/cJsOMiMfNA9+DeHDlAU7KAAB5GDlYFpa9MZMio=
142142
google.golang.org/grpc v1.17.0/go.mod h1:6QZJwpn2B+Zp71q/5VxRsJ6NXXVCE5NRUHRo+f3cWCs=
143+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
143144
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
144145
gopkg.in/yaml.v2 v2.2.1/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
145146
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=

‎modules/todo/keyboard.go‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package todo
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/gdamore/tcell"
7+
"github.com/wtfutil/wtf/cfg"
8+
"github.com/wtfutil/wtf/wtf"
9+
)
10+
11+
func (widget *Widget) initializeKeyboardControls() {
12+
widget.SetKeyboardChar(" ", widget.toggleChecked)
13+
widget.SetKeyboardChar("/", widget.ShowHelp)
14+
widget.SetKeyboardChar("j", widget.displayNext)
15+
widget.SetKeyboardChar("k", widget.displayPrev)
16+
widget.SetKeyboardChar("n", widget.newItem)
17+
widget.SetKeyboardChar("o", widget.openFile)
18+
19+
widget.SetKeyboardKey(tcell.KeyCtrlD, widget.deleteSelected)
20+
widget.SetKeyboardKey(tcell.KeyCtrlJ, widget.demoteSelected)
21+
widget.SetKeyboardKey(tcell.KeyCtrlK, widget.promoteSelected)
22+
widget.SetKeyboardKey(tcell.KeyDown, widget.displayNext)
23+
widget.SetKeyboardKey(tcell.KeyEnter, widget.editSelected)
24+
widget.SetKeyboardKey(tcell.KeyEsc, widget.unselect)
25+
widget.SetKeyboardKey(tcell.KeyUp, widget.displayPrev)
26+
}
27+
28+
func (widget *Widget) deleteSelected() {
29+
widget.list.Delete()
30+
widget.persist()
31+
widget.display()
32+
}
33+
34+
func (widget *Widget) demoteSelected() {
35+
widget.list.Demote()
36+
widget.persist()
37+
widget.display()
38+
}
39+
40+
func (widget *Widget) displayNext() {
41+
widget.list.Next()
42+
widget.display()
43+
}
44+
45+
func (widget *Widget) displayPrev() {
46+
widget.list.Prev()
47+
widget.display()
48+
}
49+
50+
func (widget *Widget) openFile() {
51+
confDir, _ := cfg.ConfigDir()
52+
wtf.OpenFile(fmt.Sprintf("%s/%s", confDir, widget.filePath))
53+
}
54+
55+
func (widget *Widget) promoteSelected() {
56+
widget.list.Promote()
57+
widget.persist()
58+
widget.display()
59+
}
60+
61+
func (widget *Widget) toggleChecked() {
62+
widget.list.Toggle()
63+
widget.persist()
64+
widget.display()
65+
}
66+
67+
func (widget *Widget) unselect() {
68+
widget.list.Unselect()
69+
widget.display()
70+
}

‎modules/todo/widget.go‎

Lines changed: 16 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,10 @@ const offscreen = -1000
3535
const modalWidth = 80
3636
const modalHeight = 7
3737

38+
// A Widget represents a Todo widget
3839
type Widget struct {
3940
wtf.HelpfulWidget
41+
wtf.KeyboardWidget
4042
wtf.TextWidget
4143

4244
app *tview.Application
@@ -46,10 +48,12 @@ type Widget struct {
4648
pages *tview.Pages
4749
}
4850

51+
// NewWidget creates a new instance of a widget
4952
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
5053
widget := Widget{
51-
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
52-
TextWidget: wtf.NewTextWidget(app, settings.common, true),
54+
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
55+
KeyboardWidget: wtf.NewKeyboardWidget(),
56+
TextWidget: wtf.NewTextWidget(app, settings.common, true),
5357

5458
app: app,
5559
settings: settings,
@@ -59,11 +63,13 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
5963
}
6064

6165
widget.init()
62-
widget.HelpfulWidget.SetView(widget.View)
66+
widget.initializeKeyboardControls()
6367

64-
widget.View.SetScrollable(true)
68+
widget.View.SetInputCapture(widget.InputCapture)
6569
widget.View.SetRegions(true)
66-
widget.View.SetInputCapture(widget.keyboardIntercept)
70+
widget.View.SetScrollable(true)
71+
72+
widget.HelpfulWidget.SetView(widget.View)
6773

6874
return &widget
6975
}
@@ -74,20 +80,21 @@ func (widget *Widget) Refresh() {
7480
widget.load()
7581

7682
widget.app.QueueUpdateDraw(func() {
77-
widget.View.SetTitle(widget.ContextualTitle(widget.CommonSettings.Title))
83+
title := widget.ContextualTitle(widget.CommonSettings.Title)
84+
widget.View.SetTitle(title)
7885
widget.display()
7986
})
8087

8188
}
8289

83-
func (widget *Widget) SetList(newList checklist.Checklist) {
84-
widget.list = newList
90+
func (widget *Widget) SetList(list checklist.Checklist) {
91+
widget.list = list
8592
}
8693

8794
/* -------------------- Unexported Functions -------------------- */
8895

8996
// edit opens a modal dialog that permits editing the text of the currently-selected item
90-
func (widget *Widget) editItem() {
97+
func (widget *Widget) editSelected() {
9198
if widget.list.SelectedItem() == nil {
9299
return
93100
}
@@ -115,81 +122,6 @@ func (widget *Widget) init() {
115122
}
116123
}
117124

118-
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
119-
switch string(event.Rune()) {
120-
case " ":
121-
// Check/uncheck selected item
122-
widget.list.Toggle()
123-
widget.persist()
124-
widget.display()
125-
return nil
126-
case "/":
127-
widget.ShowHelp()
128-
return nil
129-
case "j":
130-
// Select the next item down
131-
widget.list.Next()
132-
widget.display()
133-
return nil
134-
case "k":
135-
// Select the next item up
136-
widget.list.Prev()
137-
widget.display()
138-
return nil
139-
case "n":
140-
// Add a new item
141-
widget.newItem()
142-
return nil
143-
case "o":
144-
// Open the file
145-
confDir, _ := cfg.ConfigDir()
146-
wtf.OpenFile(fmt.Sprintf("%s/%s", confDir, widget.filePath))
147-
return nil
148-
}
149-
150-
switch event.Key() {
151-
case tcell.KeyCtrlD:
152-
// Delete the selected item
153-
widget.list.Delete()
154-
widget.persist()
155-
widget.display()
156-
return nil
157-
case tcell.KeyCtrlJ:
158-
// Move selected item down in the list
159-
widget.list.Demote()
160-
widget.persist()
161-
widget.display()
162-
return nil
163-
case tcell.KeyCtrlK:
164-
// Move selected item up in the list
165-
widget.list.Promote()
166-
widget.persist()
167-
widget.display()
168-
return nil
169-
case tcell.KeyDown:
170-
// Select the next item down
171-
widget.list.Next()
172-
widget.display()
173-
return nil
174-
case tcell.KeyEnter:
175-
widget.editItem()
176-
return nil
177-
case tcell.KeyEsc:
178-
// Unselect the current row
179-
widget.list.Unselect()
180-
widget.display()
181-
return event
182-
case tcell.KeyUp:
183-
// Select the next item up
184-
widget.list.Prev()
185-
widget.display()
186-
return nil
187-
default:
188-
// Pass it along
189-
return event
190-
}
191-
}
192-
193125
// Loads the todo list from Yaml file
194126
func (widget *Widget) load() {
195127
confDir, _ := cfg.ConfigDir()

‎wtf/keyboard_widget.go‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package wtf
2+
3+
import (
4+
"github.com/gdamore/tcell"
5+
)
6+
7+
// KeyboardWidget manages keyboard control for a widget
8+
type KeyboardWidget struct {
9+
charMap map[string]func()
10+
keyMap map[tcell.Key]func()
11+
}
12+
13+
// NewKeyboardWidget creates and returns a new instance of KeyboardWidget
14+
func NewKeyboardWidget() KeyboardWidget {
15+
return KeyboardWidget{
16+
charMap: make(map[string]func()),
17+
keyMap: make(map[tcell.Key]func()),
18+
}
19+
}
20+
21+
// SetKeyboardChar sets a character/function combination that responds to key presses
22+
// Example:
23+
//
24+
// widget.SetKeyboardChar("d", widget.deleteSelectedItem)
25+
//
26+
func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func()) {
27+
widget.charMap[char] = fn
28+
}
29+
30+
// SetKeyboardKey sets a tcell.Key/function combination that responds to key presses
31+
// Example:
32+
//
33+
// widget.SetKeyboardKey(tcell.KeyCtrlD, widget.deleteSelectedItem)
34+
//
35+
func (widget *KeyboardWidget) SetKeyboardKey(key tcell.Key, fn func()) {
36+
widget.keyMap[key] = fn
37+
}
38+
39+
// InputCapture is the function passed to tview's SetInputCapture() function
40+
// This is done during the main widget's creation process using the following code:
41+
//
42+
// widget.View.SetInputCapture(widget.InputCapture)
43+
//
44+
func (widget *KeyboardWidget) InputCapture(event *tcell.EventKey) *tcell.EventKey {
45+
fn := widget.charMap[string(event.Rune())]
46+
if fn != nil {
47+
fn()
48+
return nil
49+
}
50+
51+
fn = widget.keyMap[event.Key()]
52+
if fn != nil {
53+
fn()
54+
return nil
55+
}
56+
57+
return event
58+
}

0 commit comments

Comments
 (0)
⌂ 换网址