mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Have keyboard widget manage its own help
Define help with keys This means that keys and help are automatically in sync This means that you can't define keys, but forget help This unfortunately also means that formatting may not be quite as good
This commit is contained in:
@@ -1,40 +0,0 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
type HelpfulWidget struct {
|
||||
app *tview.Application
|
||||
helpText string
|
||||
pages *tview.Pages
|
||||
view *tview.TextView
|
||||
}
|
||||
|
||||
func NewHelpfulWidget(app *tview.Application, pages *tview.Pages, helpText string) HelpfulWidget {
|
||||
return HelpfulWidget{
|
||||
app: app,
|
||||
helpText: helpText,
|
||||
pages: pages,
|
||||
}
|
||||
}
|
||||
|
||||
func (widget *HelpfulWidget) SetView(view *tview.TextView) {
|
||||
widget.view = view
|
||||
}
|
||||
|
||||
func (widget *HelpfulWidget) ShowHelp() {
|
||||
closeFunc := func() {
|
||||
widget.pages.RemovePage("help")
|
||||
widget.app.SetFocus(widget.view)
|
||||
}
|
||||
|
||||
modal := NewBillboardModal(widget.helpText, closeFunc)
|
||||
|
||||
widget.pages.AddPage("help", modal, false, true)
|
||||
widget.app.SetFocus(modal)
|
||||
|
||||
widget.app.QueueUpdate(func() {
|
||||
widget.app.Draw()
|
||||
})
|
||||
}
|
||||
@@ -1,20 +1,39 @@
|
||||
package wtf
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
"github.com/rivo/tview"
|
||||
"github.com/wtfutil/wtf/cfg"
|
||||
)
|
||||
|
||||
type helpItem struct {
|
||||
Key string
|
||||
Text string
|
||||
}
|
||||
|
||||
// KeyboardWidget manages keyboard control for a widget
|
||||
type KeyboardWidget struct {
|
||||
app *tview.Application
|
||||
pages *tview.Pages
|
||||
view *tview.TextView
|
||||
settings *cfg.Common
|
||||
|
||||
charMap map[string]func()
|
||||
keyMap map[tcell.Key]func()
|
||||
helpMap []helpItem
|
||||
}
|
||||
|
||||
// NewKeyboardWidget creates and returns a new instance of KeyboardWidget
|
||||
func NewKeyboardWidget() KeyboardWidget {
|
||||
func NewKeyboardWidget(app *tview.Application, pages *tview.Pages, settings *cfg.Common) KeyboardWidget {
|
||||
return KeyboardWidget{
|
||||
charMap: make(map[string]func()),
|
||||
keyMap: make(map[tcell.Key]func()),
|
||||
app: app,
|
||||
pages: pages,
|
||||
settings: settings,
|
||||
charMap: make(map[string]func()),
|
||||
keyMap: make(map[tcell.Key]func()),
|
||||
helpMap: []helpItem{},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,8 +42,9 @@ func NewKeyboardWidget() KeyboardWidget {
|
||||
//
|
||||
// widget.SetKeyboardChar("d", widget.deleteSelectedItem)
|
||||
//
|
||||
func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func()) {
|
||||
func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func(), helpText string) {
|
||||
widget.charMap[char] = fn
|
||||
widget.helpMap = append(widget.helpMap, helpItem{char, helpText})
|
||||
}
|
||||
|
||||
// SetKeyboardKey sets a tcell.Key/function combination that responds to key presses
|
||||
@@ -32,8 +52,9 @@ func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func()) {
|
||||
//
|
||||
// widget.SetKeyboardKey(tcell.KeyCtrlD, widget.deleteSelectedItem)
|
||||
//
|
||||
func (widget *KeyboardWidget) SetKeyboardKey(key tcell.Key, fn func()) {
|
||||
func (widget *KeyboardWidget) SetKeyboardKey(key tcell.Key, fn func(), helpText string) {
|
||||
widget.keyMap[key] = fn
|
||||
widget.helpMap = append(widget.helpMap, helpItem{tcell.KeyNames[key], helpText})
|
||||
}
|
||||
|
||||
// InputCapture is the function passed to tview's SetInputCapture() function
|
||||
@@ -56,3 +77,34 @@ func (widget *KeyboardWidget) InputCapture(event *tcell.EventKey) *tcell.EventKe
|
||||
|
||||
return event
|
||||
}
|
||||
|
||||
func (widget *KeyboardWidget) helpText() string {
|
||||
|
||||
str := "Keyboard commands for " + widget.settings.Module.Type + "\n\n"
|
||||
|
||||
for _, item := range widget.helpMap {
|
||||
str = str + fmt.Sprintf("%s: %s\n", item.Key, item.Text)
|
||||
}
|
||||
|
||||
return str
|
||||
}
|
||||
|
||||
func (widget *KeyboardWidget) SetView(view *tview.TextView) {
|
||||
widget.view = view
|
||||
}
|
||||
|
||||
func (widget *KeyboardWidget) ShowHelp() {
|
||||
closeFunc := func() {
|
||||
widget.pages.RemovePage("help")
|
||||
widget.app.SetFocus(widget.view)
|
||||
}
|
||||
|
||||
modal := NewBillboardModal(widget.helpText(), closeFunc)
|
||||
|
||||
widget.pages.AddPage("help", modal, false, true)
|
||||
widget.app.SetFocus(modal)
|
||||
|
||||
widget.app.QueueUpdate(func() {
|
||||
widget.app.Draw()
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user