mirror of
https://github.com/taigrr/wtf
synced 2025-01-18 04:03:14 -08:00
Split out char/key mapping help
This makes help more inline with previous instances Wrap everything in `[]`, so keys like ` ` show up correctly Pad characters for keypresses, so things have pretty alignment
This commit is contained in:
parent
cd35d1e0a3
commit
9b8ce06990
@ -22,7 +22,9 @@ type KeyboardWidget struct {
|
||||
|
||||
charMap map[string]func()
|
||||
keyMap map[tcell.Key]func()
|
||||
helpMap []helpItem
|
||||
charHelp []helpItem
|
||||
keyHelp []helpItem
|
||||
maxKey int
|
||||
}
|
||||
|
||||
// NewKeyboardWidget creates and returns a new instance of KeyboardWidget
|
||||
@ -33,7 +35,8 @@ func NewKeyboardWidget(app *tview.Application, pages *tview.Pages, settings *cfg
|
||||
settings: settings,
|
||||
charMap: make(map[string]func()),
|
||||
keyMap: make(map[tcell.Key]func()),
|
||||
helpMap: []helpItem{},
|
||||
charHelp: []helpItem{},
|
||||
keyHelp: []helpItem{},
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,7 +47,7 @@ func NewKeyboardWidget(app *tview.Application, pages *tview.Pages, settings *cfg
|
||||
//
|
||||
func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func(), helpText string) {
|
||||
widget.charMap[char] = fn
|
||||
widget.helpMap = append(widget.helpMap, helpItem{char, helpText})
|
||||
widget.charHelp = append(widget.charHelp, helpItem{char, helpText})
|
||||
}
|
||||
|
||||
// SetKeyboardKey sets a tcell.Key/function combination that responds to key presses
|
||||
@ -54,7 +57,10 @@ func (widget *KeyboardWidget) SetKeyboardChar(char string, fn func(), helpText s
|
||||
//
|
||||
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})
|
||||
widget.keyHelp = append(widget.keyHelp, helpItem{tcell.KeyNames[key], helpText})
|
||||
if len(tcell.KeyNames[key]) > widget.maxKey {
|
||||
widget.maxKey = len(tcell.KeyNames[key])
|
||||
}
|
||||
}
|
||||
|
||||
// InputCapture is the function passed to tview's SetInputCapture() function
|
||||
@ -82,8 +88,13 @@ 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)
|
||||
for _, item := range widget.charHelp {
|
||||
str = str + fmt.Sprintf(" [%s]: %s\n", item.Key, item.Text)
|
||||
}
|
||||
str = str + "\n\n"
|
||||
|
||||
for _, item := range widget.keyHelp {
|
||||
str = str + fmt.Sprintf(" [%-*s]: %s\n", widget.maxKey, item.Key, item.Text)
|
||||
}
|
||||
|
||||
return str
|
||||
|
Loading…
x
Reference in New Issue
Block a user