initial commit
This commit is contained in:
147
internal/ui/utils/keys.go
Normal file
147
internal/ui/utils/keys.go
Normal file
@@ -0,0 +1,147 @@
|
||||
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
|
||||
}
|
||||
29
internal/ui/utils/utils.go
Normal file
29
internal/ui/utils/utils.go
Normal file
@@ -0,0 +1,29 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/gdamore/tcell/v2"
|
||||
"github.com/rivo/tview"
|
||||
)
|
||||
|
||||
const (
|
||||
RefreshInterval = 250 * time.Millisecond
|
||||
)
|
||||
|
||||
func EmptyBoxSpace(bgColor tcell.Color) *tview.Box {
|
||||
box := tview.NewBox()
|
||||
box.SetBackgroundColor(bgColor)
|
||||
box.SetBorder(false)
|
||||
|
||||
return box
|
||||
}
|
||||
|
||||
func CheckFocus(prims ...tview.Primitive) bool {
|
||||
for _, prim := range prims {
|
||||
if prim.HasFocus() {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
Reference in New Issue
Block a user