1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

Simplify the view loading for the keyboard widget

Signed-off-by: Chris Cummer <chriscummer@me.com>
This commit is contained in:
Chris Cummer
2020-11-26 14:05:34 -08:00
parent 4a820dd0e5
commit f9a06540f1
44 changed files with 29 additions and 85 deletions

View File

@@ -15,8 +15,8 @@ type BarGraph struct {
maxStars int
starChar string
Base
KeyboardWidget
*Base
*KeyboardWidget
View *tview.TextView
}

View File

@@ -25,8 +25,8 @@ type Base struct {
// NewBase creates and returns an instance of the Base module, the lowest-level
// primitive module from which all others are derived
func NewBase(app *tview.Application, commonSettings *cfg.Common) Base {
base := Base{
func NewBase(app *tview.Application, commonSettings *cfg.Common) *Base {
base := &Base{
commonSettings: commonSettings,
app: app,

View File

@@ -32,8 +32,8 @@ type KeyboardWidget struct {
}
// NewKeyboardWidget creates and returns a new instance of KeyboardWidget
func NewKeyboardWidget(app *tview.Application, pages *tview.Pages, settings *cfg.Common) KeyboardWidget {
keyWidget := KeyboardWidget{
func NewKeyboardWidget(app *tview.Application, pages *tview.Pages, settings *cfg.Common) *KeyboardWidget {
keyWidget := &KeyboardWidget{
app: app,
pages: pages,
settings: settings,
@@ -50,6 +50,17 @@ func NewKeyboardWidget(app *tview.Application, pages *tview.Pages, settings *cfg
/* -------------------- Exported Functions --------------------- */
// AssignedChars returns a list of all the text characters assigned to an operation
func (widget *KeyboardWidget) AssignedChars() []string {
chars := []string{}
for char := range widget.charMap {
chars = append(chars, char)
}
return chars
}
// HelpText returns the help text and keyboard command info for this widget
func (widget *KeyboardWidget) HelpText() string {
str := " [green::b]Keyboard commands for " + strings.Title(widget.settings.Module.Type) + "[white]\n\n"

View File

@@ -11,7 +11,7 @@ import (
func test() {}
func testKeyboardWidget() KeyboardWidget {
func testKeyboardWidget() *KeyboardWidget {
keyWid := NewKeyboardWidget(
tview.NewApplication(),
tview.NewPages(),
@@ -118,25 +118,25 @@ func Test_SetKeyboardKey(t *testing.T) {
func Test_InputCapture(t *testing.T) {
tests := []struct {
name string
before func(keyWid KeyboardWidget) KeyboardWidget
before func(keyWid *KeyboardWidget) *KeyboardWidget
event *tcell.EventKey
expected *tcell.EventKey
}{
{
name: "with nil event",
before: func(keyWid KeyboardWidget) KeyboardWidget { return keyWid },
before: func(keyWid *KeyboardWidget) *KeyboardWidget { return keyWid },
event: nil,
expected: nil,
},
{
name: "with undefined event",
before: func(keyWid KeyboardWidget) KeyboardWidget { return keyWid },
before: func(keyWid *KeyboardWidget) *KeyboardWidget { return keyWid },
event: tcell.NewEventKey(tcell.KeyRune, 'a', tcell.ModNone),
expected: tcell.NewEventKey(tcell.KeyRune, 'a', tcell.ModNone),
},
{
name: "with defined event and char handler",
before: func(keyWid KeyboardWidget) KeyboardWidget {
before: func(keyWid *KeyboardWidget) *KeyboardWidget {
keyWid.SetKeyboardChar("a", test, "help")
return keyWid
},
@@ -145,7 +145,7 @@ func Test_InputCapture(t *testing.T) {
},
{
name: "with defined event and key handler",
before: func(keyWid KeyboardWidget) KeyboardWidget {
before: func(keyWid *KeyboardWidget) *KeyboardWidget {
keyWid.SetKeyboardKey(tcell.KeyRune, test, "help")
return keyWid
},

View File

@@ -10,8 +10,8 @@ import (
// TextWidget defines the data necessary to make a text widget
type TextWidget struct {
Base
KeyboardWidget
*Base
*KeyboardWidget
View *tview.TextView
}
@@ -24,7 +24,9 @@ func NewTextWidget(app *tview.Application, pages *tview.Pages, commonSettings *c
}
widget.View = widget.createView(widget.bordered)
widget.View.SetInputCapture(widget.InputCapture)
widget.View.SetInputCapture(widget.KeyboardWidget.InputCapture)
widget.KeyboardWidget.SetView(widget.View)
return widget
}