Reworking Keymaps
This commit is contained in:
67
wdgt_cli.go
67
wdgt_cli.go
@@ -54,7 +54,7 @@ type Cli struct {
|
||||
|
||||
commands []*CliCommand
|
||||
|
||||
keyMap, customKeyMap KeyMap
|
||||
keyMap *KeyMap
|
||||
}
|
||||
|
||||
// TODO: Fix Command/SubCommand finding
|
||||
@@ -81,22 +81,8 @@ func (w *Cli) HandleResize(ev *tcell.EventResize) {
|
||||
}
|
||||
}
|
||||
|
||||
func (w *Cli) SetKeyMap(km KeyMap, def bool) {
|
||||
if def {
|
||||
w.keyMap = km
|
||||
} else {
|
||||
w.customKeyMap = km
|
||||
}
|
||||
}
|
||||
func (w *Cli) AddToKeyMap(km KeyMap) { w.customKeyMap.Merge(km) }
|
||||
func (w *Cli) RemoveFromKeyMap(km KeyMap) {
|
||||
for k := range km.Keys {
|
||||
w.customKeyMap.Remove(k)
|
||||
}
|
||||
for r := range km.Runes {
|
||||
w.customKeyMap.RemoveRune(r)
|
||||
}
|
||||
}
|
||||
func (w *Cli) GetKeyMap() *KeyMap { return w.keyMap }
|
||||
func (w *Cli) SetKeyMap(km *KeyMap) { w.keyMap = km }
|
||||
|
||||
func (w *Cli) HandleKey(ev *tcell.EventKey) bool {
|
||||
if !w.active {
|
||||
@@ -114,8 +100,7 @@ func (w *Cli) HandleKey(ev *tcell.EventKey) bool {
|
||||
w.value = w.value[:w.cursor] + w.value[w.cursor+1:]
|
||||
}
|
||||
}
|
||||
b1, b2 := w.keyMap.Handle(ev), w.customKeyMap.Handle(ev)
|
||||
if b1 || b2 {
|
||||
if w.keyMap.Handle(ev) {
|
||||
return true
|
||||
}
|
||||
|
||||
@@ -216,12 +201,12 @@ func (w *Cli) MinW() int { return 20 }
|
||||
func (w *Cli) MinH() int { return 6 }
|
||||
|
||||
func (w *Cli) initKeyMap() {
|
||||
w.keyMap = NewKeyMap(map[tcell.Key]func(ev *tcell.EventKey) bool{
|
||||
tcell.KeyEsc: func(ev *tcell.EventKey) bool {
|
||||
w.keyMap = NewKeyMap(
|
||||
NewKey(BuildEK(tcell.KeyEsc), func(ev *tcell.EventKey) bool {
|
||||
w.SetActive(false)
|
||||
return true
|
||||
},
|
||||
tcell.KeyUp: func(ev *tcell.EventKey) bool {
|
||||
}),
|
||||
NewKey(BuildEK(tcell.KeyUp), func(ev *tcell.EventKey) bool {
|
||||
if w.historyPosition < len(w.history)-1 {
|
||||
w.historyPosition++
|
||||
w.value = w.history[w.historyPosition]
|
||||
@@ -229,8 +214,8 @@ func (w *Cli) initKeyMap() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyDown: func(ev *tcell.EventKey) bool {
|
||||
}),
|
||||
NewKey(BuildEK(tcell.KeyDown), func(ev *tcell.EventKey) bool {
|
||||
if w.historyPosition > -1 {
|
||||
w.historyPosition--
|
||||
if w.historyPosition == -1 {
|
||||
@@ -242,27 +227,27 @@ func (w *Cli) initKeyMap() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyLeft: func(ev *tcell.EventKey) bool {
|
||||
}),
|
||||
NewKey(BuildEK(tcell.KeyLeft), func(ev *tcell.EventKey) bool {
|
||||
if w.cursor > 0 {
|
||||
w.cursor--
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyRight: func(ev *tcell.EventKey) bool {
|
||||
}),
|
||||
NewKey(BuildEK(tcell.KeyRight), func(ev *tcell.EventKey) bool {
|
||||
if w.cursor < len(w.value) {
|
||||
w.cursor++
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyCtrlU: func(ev *tcell.EventKey) bool {
|
||||
}),
|
||||
NewKey(BuildEK(tcell.KeyCtrlU), func(ev *tcell.EventKey) bool {
|
||||
w.value = w.value[w.cursor:]
|
||||
w.cursor = 0
|
||||
return true
|
||||
},
|
||||
tcell.KeyTab: func(ev *tcell.EventKey) bool {
|
||||
}),
|
||||
NewKey(BuildEK(tcell.KeyTab), func(ev *tcell.EventKey) bool {
|
||||
// Auto-complete
|
||||
guess := w.findBestGuess()
|
||||
if guess != nil {
|
||||
@@ -273,8 +258,8 @@ func (w *Cli) initKeyMap() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyEnter: func(ev *tcell.EventKey) bool {
|
||||
}),
|
||||
NewKey(BuildEK(tcell.KeyEnter), func(ev *tcell.EventKey) bool {
|
||||
args := strings.Split(w.value, " ")
|
||||
w.historyPosition = -1
|
||||
w.value = ""
|
||||
@@ -289,8 +274,8 @@ func (w *Cli) initKeyMap() {
|
||||
}
|
||||
w.history = append(w.history, fmt.Sprintf("%s: command not found", w.value))
|
||||
return true
|
||||
},
|
||||
tcell.KeyPgUp: func(ev *tcell.EventKey) bool {
|
||||
}),
|
||||
NewKey(BuildEK(tcell.KeyPgUp), func(ev *tcell.EventKey) bool {
|
||||
if w.logPosition < len(w.log)-w.h-2 {
|
||||
w.logPosition += (w.h - 2)
|
||||
if w.logPosition > len(w.log) {
|
||||
@@ -299,8 +284,8 @@ func (w *Cli) initKeyMap() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
tcell.KeyPgDn: func(ev *tcell.EventKey) bool {
|
||||
}),
|
||||
NewKey(BuildEK(tcell.KeyPgDn), func(ev *tcell.EventKey) bool {
|
||||
if w.logPosition > 0 {
|
||||
w.logPosition -= (w.h - 2)
|
||||
if w.logPosition < 0 {
|
||||
@@ -309,8 +294,8 @@ func (w *Cli) initKeyMap() {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
},
|
||||
})
|
||||
}),
|
||||
)
|
||||
}
|
||||
func (w *Cli) SetTitle(ttl string) { w.title = ttl }
|
||||
func (w *Cli) Title() string { return w.title }
|
||||
|
||||
Reference in New Issue
Block a user