package utils import ( "github.com/gdamore/tcell/v2" "go.uber.org/zap" ) var ( CommandMenuKey = uiKeyInfo{ Key: tcell.Key(256), KeyRune: 'm', KeyLabel: "m", KeyDesc: "display command menu", } NextScreenKey = uiKeyInfo{ Key: tcell.Key(256), KeyRune: 'l', KeyLabel: "l", KeyDesc: "switch to next screen", } PreviousScreenKey = uiKeyInfo{ Key: tcell.Key(256), KeyRune: 'h', KeyLabel: "h", KeyDesc: "switch to previous screen", } MoveUpKey = uiKeyInfo{ Key: tcell.KeyUp, KeyRune: 'k', KeyLabel: "k", KeyDesc: "move up", } MoveDownKey = uiKeyInfo{ Key: tcell.KeyDown, KeyRune: 'j', KeyLabel: "j", KeyDesc: "move down", } CloseDialogKey = uiKeyInfo{ Key: tcell.KeyEsc, KeyLabel: "Esc", KeyDesc: "close the active dialog", } SwitchFocusKey = uiKeyInfo{ Key: tcell.KeyTab, KeyLabel: "Tab", KeyDesc: "switch between widgets", } ArrowUpKey = uiKeyInfo{ Key: tcell.KeyUp, KeyLabel: "arrow up", KeyDesc: "move up", } ArrowDownKey = uiKeyInfo{ Key: tcell.KeyDown, KeyLabel: "arrow down", KeyDesc: "move down", } ArrowLeftKey = uiKeyInfo{ Key: tcell.KeyLeft, KeyLabel: "Arrow Left", KeyDesc: "previous screen", } ArrowRightKey = uiKeyInfo{ Key: tcell.KeyRight, KeyLabel: "Arrow Right", KeyDesc: "next screen", } AppExitKey = uiKeyInfo{ Key: tcell.KeyCtrlC, KeyLabel: "Ctrl+c", KeyDesc: "exit application", } HelpScreenKey = uiKeyInfo{ Key: tcell.KeyF1, KeyLabel: "F1", KeyDesc: "display help screen", } AssetsScreenKey = uiKeyInfo{ Key: tcell.KeyF2, KeyLabel: "F2", KeyDesc: "display assets screen", } ) var UIKeyBindings = []uiKeyInfo{ CommandMenuKey, NextScreenKey, PreviousScreenKey, MoveUpKey, MoveDownKey, CloseDialogKey, SwitchFocusKey, ArrowUpKey, ArrowDownKey, ArrowLeftKey, ArrowRightKey, AppExitKey, HelpScreenKey, AssetsScreenKey, } type uiKeyInfo struct { Key tcell.Key KeyRune rune KeyLabel string KeyDesc string } func (key *uiKeyInfo) Label() string { return key.KeyLabel } func (key *uiKeyInfo) Rune() rune { return key.KeyRune } func (key *uiKeyInfo) EventKey() tcell.Key { return key.Key } func (key *uiKeyInfo) Description() string { return key.KeyDesc } func ParseKeyEventKey(logger *zap.Logger, event *tcell.EventKey) *tcell.EventKey { logger.Sugar().Debugw("parse key event", "event", event, "key", event.Key(), "name", event.Name()) switch event.Rune() { case MoveUpKey.KeyRune: return tcell.NewEventKey(MoveUpKey.Key, MoveUpKey.KeyRune, tcell.ModNone) case MoveDownKey.KeyRune: return tcell.NewEventKey(MoveDownKey.Key, MoveDownKey.KeyRune, tcell.ModNone) } switch event.Key() { case ArrowLeftKey.Key: return tcell.NewEventKey(PreviousScreenKey.Key, PreviousScreenKey.KeyRune, tcell.ModNone) case ArrowRightKey.Key: return tcell.NewEventKey(NextScreenKey.Key, NextScreenKey.KeyRune, tcell.ModNone) } return event }