Start updating keybindings to be tcell-style

pull/232/head
Simon Roberts 3 years ago
parent 18eb944c28
commit f5dbbe4ea5
No known key found for this signature in database
GPG Key ID: 0F30F99E6B771FD4

@ -4,171 +4,110 @@ import (
"strings"
"github.com/cointop-sh/cointop/pkg/gocui"
"github.com/gdamore/tcell/v2"
log "github.com/sirupsen/logrus"
)
func keyMap(k string) string {
// compatability mapping
key := k
switch k {
case "lsqrbracket", "leftsqrbracket", "leftsquarebracket":
key = "["
case "rsqrbracket", "rightsqrbracket", "rightsquarebracket":
key = "]"
case "space":
key = " "
case "backslash":
key = "\\"
case "underscore":
key = "_"
// case "\\\\":
// key = '\\'
}
return key
}
// ParseKeys returns string keyboard key as gocui key type
func (ct *Cointop) ParseKeys(s string) (interface{}, gocui.Modifier) {
func (ct *Cointop) ParseKeys(s string) (interface{}, tcell.ModMask) {
// TODO: change file convention to match tcell
// TODO: change to return EventKey
var key interface{}
mod := gocui.ModNone
mod := tcell.ModNone
// First try looking up keyname directly
keyName := strings.ToLower(strings.TrimSpace(strings.Replace(s, "+", "-", -1)))
for key, name := range tcell.KeyNames {
if strings.ToLower(name) == keyName {
// Found direct match
if strings.HasPrefix(name, "Ctrl-") {
mod = tcell.ModCtrl
}
// log.Debugf("XXX direct map %s: %d %s", s, key, mod) // TODO: remove
return key, mod
}
}
split := strings.Split(s, "+")
if len(split) > 1 {
m := strings.ToLower(strings.TrimSpace(split[0]))
k := strings.ToLower(strings.TrimSpace(split[1]))
k = keyMap(k)
if m == "alt" {
mod = gocui.ModAlt
mod = tcell.ModAlt
s = k
} else if m == "ctrl" {
switch k {
case "0":
key = '0'
case "1":
key = '1'
case "2":
key = gocui.KeyCtrl2
case "3":
key = gocui.KeyCtrl3
case "4":
key = gocui.KeyCtrl4
case "5":
key = gocui.KeyCtrl5
case "6":
key = gocui.KeyCtrl6
case "7":
key = gocui.KeyCtrl7
case "8":
key = gocui.KeyCtrl8
case "9":
key = '9'
case "a":
key = gocui.KeyCtrlA
case "b":
key = gocui.KeyCtrlB
case "c":
key = gocui.KeyCtrlC
case "d":
key = gocui.KeyCtrlD
case "e":
key = gocui.KeyCtrlE
case "f":
key = gocui.KeyCtrlF
case "g":
key = gocui.KeyCtrlG
case "h":
key = gocui.KeyCtrlH
case "i":
key = gocui.KeyCtrlI
case "j":
key = gocui.KeyCtrlJ
case "k":
key = gocui.KeyCtrlK
case "l":
key = gocui.KeyCtrlL
case "m":
key = gocui.KeyCtrlL
case "n":
key = gocui.KeyCtrlN
case "o":
key = gocui.KeyCtrlO
case "p":
key = gocui.KeyCtrlP
case "q":
key = gocui.KeyCtrlQ
case "r":
key = gocui.KeyCtrlR
case "s":
key = gocui.KeyCtrlS
case "t":
key = gocui.KeyCtrlT
case "u":
key = gocui.KeyCtrlU
case "v":
key = gocui.KeyCtrlV
case "w":
key = gocui.KeyCtrlW
case "x":
key = gocui.KeyCtrlX
case "y":
key = gocui.KeyCtrlY
case "z":
key = gocui.KeyCtrlZ
case "~":
key = gocui.KeyCtrlTilde
case "[", "lsqrbracket", "leftsqrbracket", "leftsquarebracket":
key = gocui.KeyCtrlLsqBracket
case "]", "rsqrbracket", "rightsqrbracket", "rightsquarebracket":
key = gocui.KeyCtrlRsqBracket
case "space":
key = gocui.KeyCtrlSpace
case "backslash":
key = gocui.KeyCtrlBackslash
case "underscore":
key = gocui.KeyCtrlUnderscore
case "\\\\":
key = '\\'
}
return key, mod
// k = keyMap(k)
}
// return key, mod
}
s = keyMap(s)
if len(s) == 1 {
r := []rune(s)
key = r[0]
// log.Debugf("XXX one-rune map %s: %d %s", s, key, mod) // TODO: remove
return key, mod
}
s = strings.ToLower(s)
switch s {
case "arrowup", "uparrow", "up":
key = gocui.KeyArrowUp
case "arrowdown", "downarrow", "down":
key = gocui.KeyArrowDown
case "arrowleft", "leftarrow", "left":
key = gocui.KeyArrowLeft
case "arrowright", "rightarrow", "right":
key = gocui.KeyArrowRight
case "enter", "return":
key = gocui.KeyEnter
case "space", "spacebar":
key = gocui.KeySpace
case "esc", "escape":
key = gocui.KeyEsc
case "f1":
key = gocui.KeyF1
case "f2":
key = gocui.KeyF2
case "f3":
key = gocui.KeyF3
case "f4":
key = gocui.KeyF4
case "f5":
key = gocui.KeyF5
case "f6":
key = gocui.KeyF6
case "f7":
key = gocui.KeyF7
case "f8":
key = gocui.KeyF8
case "f9":
key = gocui.KeyF9
case "tab":
key = gocui.KeyTab
// case "arrowup", "uparrow", "up":
// key = tcell.KeyUp
// case "arrowdown", "downarrow", "down":
// key = tcell.KeyDown
// case "arrowleft", "leftarrow", "left":
// key = tcell.KeyLeft
// case "arrowright", "rightarrow", "right":
// key = tcell.KeyRight
// case "enter", "return":
// key = tcell.KeyEnter
// case "space", "spacebar":
// key = " "
// case "esc", "escape":
// key = tcell.KeyEsc
// case "tab":
// key = tcell.KeyTab
case "pageup", "pgup":
key = gocui.KeyPgup
key = tcell.KeyPgUp
case "pagedown", "pgdown", "pgdn":
key = gocui.KeyPgdn
case "home":
key = gocui.KeyHome
case "end":
key = gocui.KeyEnd
key = tcell.KeyPgDn
// case "home":
// key = tcell.KeyHome
// case "end":
// key = tcell.KeyEnd
case "\\\\":
key = '\\'
case "scrollup":
key = gocui.MouseWheelUp
key = gocui.MouseWheelUp // TODO: change to mouse event
case "scrolldown":
key = gocui.MouseWheelDown
key = gocui.MouseWheelDown // TODO: change to mouse event
}
if key == nil {
log.Debugf("XXX NO map %s: %d %s", s, key, mod) // TODO: remove
}
return key, mod
}
@ -347,47 +286,48 @@ func (ct *Cointop) SetKeybindings() error {
}
// keys to force quit
ct.SetKeybindingMod(gocui.KeyCtrlC, gocui.ModNone, ct.Keyfn(ct.Quit), "")
ct.SetKeybindingMod(gocui.KeyCtrlZ, gocui.ModNone, ct.Keyfn(ct.Quit), "")
ct.SetKeybindingMod(tcell.KeyCtrlC, tcell.ModNone, ct.Keyfn(ct.Quit), "")
ct.SetKeybindingMod(tcell.KeyCtrlZ, tcell.ModNone, ct.Keyfn(ct.Quit), "")
// searchfield keys
ct.SetKeybindingMod(gocui.KeyEnter, gocui.ModNone, ct.Keyfn(ct.DoSearch), ct.Views.SearchField.Name())
ct.SetKeybindingMod(gocui.KeyEsc, gocui.ModNone, ct.Keyfn(ct.CancelSearch), ct.Views.SearchField.Name())
ct.SetKeybindingMod(tcell.KeyEnter, tcell.ModNone, ct.Keyfn(ct.DoSearch), ct.Views.SearchField.Name())
ct.SetKeybindingMod(tcell.KeyEsc, tcell.ModNone, ct.Keyfn(ct.CancelSearch), ct.Views.SearchField.Name())
// keys to quit help when open
ct.SetKeybindingMod(gocui.KeyEsc, gocui.ModNone, ct.Keyfn(ct.HideHelp), ct.Views.Menu.Name())
ct.SetKeybindingMod('q', gocui.ModNone, ct.Keyfn(ct.HideHelp), ct.Views.Menu.Name())
ct.SetKeybindingMod(tcell.KeyEsc, tcell.ModNone, ct.Keyfn(ct.HideHelp), ct.Views.Menu.Name())
ct.SetKeybindingMod('q', tcell.ModNone, ct.Keyfn(ct.HideHelp), ct.Views.Menu.Name())
// keys to quit portfolio update menu when open
ct.SetKeybindingMod(gocui.KeyEsc, gocui.ModNone, ct.Keyfn(ct.HidePortfolioUpdateMenu), ct.Views.Input.Name())
ct.SetKeybindingMod('q', gocui.ModNone, ct.Keyfn(ct.HidePortfolioUpdateMenu), ct.Views.Input.Name())
ct.SetKeybindingMod(tcell.KeyEsc, tcell.ModNone, ct.Keyfn(ct.HidePortfolioUpdateMenu), ct.Views.Input.Name())
ct.SetKeybindingMod('q', tcell.ModNone, ct.Keyfn(ct.HidePortfolioUpdateMenu), ct.Views.Input.Name())
// keys to quit convert menu when open
ct.SetKeybindingMod(gocui.KeyEsc, gocui.ModNone, ct.Keyfn(ct.HideConvertMenu), ct.Views.Menu.Name())
ct.SetKeybindingMod('q', gocui.ModNone, ct.Keyfn(ct.HideConvertMenu), ct.Views.Menu.Name())
ct.SetKeybindingMod(tcell.KeyEsc, tcell.ModNone, ct.Keyfn(ct.HideConvertMenu), ct.Views.Menu.Name())
ct.SetKeybindingMod('q', tcell.ModNone, ct.Keyfn(ct.HideConvertMenu), ct.Views.Menu.Name())
// keys to update portfolio holdings
ct.SetKeybindingMod(gocui.KeyEnter, gocui.ModNone, ct.Keyfn(ct.EnterKeyPressHandler), ct.Views.Input.Name())
ct.SetKeybindingMod(tcell.KeyEnter, tcell.ModNone, ct.Keyfn(ct.EnterKeyPressHandler), ct.Views.Input.Name())
key, mod := ct.ParseKeys("/")
ct.DeleteKeybindingMod(key, mod, "")
// TODO: FIXME
// key, mod := ct.ParseKeys("/")
// ct.DeleteKeybindingMod(key, mod, "")
// mouse events
ct.SetKeybindingMod(gocui.MouseLeft, gocui.ModNone, ct.Keyfn(ct.MouseLeftClick), ct.Views.Table.Name()) // click to focus
ct.SetKeybindingMod(gocui.MouseLeft, tcell.ModNone, ct.Keyfn(ct.MouseLeftClick), ct.Views.Table.Name()) // click to focus
// clicking table headers sorts table
ct.SetKeybindingMod(gocui.MouseLeft, gocui.ModNone, ct.Keyfn(ct.TableHeaderMouseLeftClick), ct.Views.TableHeader.Name())
ct.SetKeybindingMod(gocui.MouseLeft, gocui.ModNone, ct.Keyfn(ct.StatusbarMouseLeftClick), ct.Views.Statusbar.Name())
ct.SetKeybindingMod(gocui.MouseLeft, tcell.ModNone, ct.Keyfn(ct.TableHeaderMouseLeftClick), ct.Views.TableHeader.Name())
ct.SetKeybindingMod(gocui.MouseLeft, tcell.ModNone, ct.Keyfn(ct.StatusbarMouseLeftClick), ct.Views.Statusbar.Name())
// debug mouse clicks
ct.SetKeybindingMod(gocui.MouseLeft, gocui.ModNone, ct.Keyfn(ct.MouseDebug), "")
ct.SetKeybindingMod(gocui.MouseLeft, tcell.ModNone, ct.Keyfn(ct.MouseDebug), "")
// character key press to select option
// TODO: use scrolling table
keys := ct.SortedSupportedCurrencyConversions()
for i, k := range keys {
ct.SetKeybindingMod(alphanumericcharacters[i], gocui.ModNone, ct.Keyfn(ct.SetCurrencyConverstionFn(k)), ct.Views.Menu.Name())
ct.SetKeybindingMod(alphanumericcharacters[i], tcell.ModNone, ct.Keyfn(ct.SetCurrencyConverstionFn(k)), ct.Views.Menu.Name())
}
ct.SetKeybindingMod(gocui.MouseLeft, gocui.ModNone, ct.Keyfn(ct.ConversionMouseLeftClick), ct.Views.Menu.Name())
ct.SetKeybindingMod(gocui.MouseLeft, tcell.ModNone, ct.Keyfn(ct.ConversionMouseLeftClick), ct.Views.Menu.Name())
return nil
}
@ -403,13 +343,14 @@ func (ct *Cointop) MouseDebug() error {
}
// SetKeybindingMod sets the keybinding modifier key
func (ct *Cointop) SetKeybindingMod(key interface{}, mod gocui.Modifier, callback func(g *gocui.Gui, v *gocui.View) error, view string) error {
func (ct *Cointop) SetKeybindingMod(key interface{}, mod tcell.ModMask, callback func(g *gocui.Gui, v *gocui.View) error, view string) error {
// TODO: take EventKey
var err error
switch t := key.(type) {
case gocui.Key:
err = ct.g.SetKeybinding(view, t, mod, callback)
case tcell.Key:
err = ct.g.SetKeybinding(view, t, 0, mod, callback)
case rune:
err = ct.g.SetKeybinding(view, t, mod, callback)
err = ct.g.SetKeybinding(view, tcell.KeyRune, t, mod, callback)
}
return err
}
@ -421,13 +362,14 @@ func (ct *Cointop) DeleteKeybinding(shortcutKey string) error {
}
// DeleteKeybindingMod ...
func (ct *Cointop) DeleteKeybindingMod(key interface{}, mod gocui.Modifier, view string) error {
func (ct *Cointop) DeleteKeybindingMod(key interface{}, mod tcell.ModMask, view string) error {
// TODO: take EventKey
var err error
switch t := key.(type) {
case gocui.Key:
err = ct.g.DeleteKeybinding(view, t, mod)
case tcell.Key:
err = ct.g.DeleteKeybinding(view, t, 0, mod)
case rune:
err = ct.g.DeleteKeybinding(view, t, mod)
err = ct.g.DeleteKeybinding(view, tcell.KeyRune, t, mod)
}
return err
}

@ -54,7 +54,7 @@ Views can also be created using relative coordinates:
Configure keybindings:
if err := g.SetKeybinding("viewname", gocui.KeyEnter, gocui.ModNone, fcn); err != nil {
if err := g.SetKeybinding("viewname", gocui.KeyEnter, tcell.ModNone, fcn); err != nil {
// handle error
}
@ -64,7 +64,7 @@ gocui implements full mouse support that can be enabled with:
Mouse events are handled like any other keybinding:
if err := g.SetKeybinding("viewname", gocui.MouseLeft, gocui.ModNone, fcn); err != nil {
if err := g.SetKeybinding("viewname", gocui.MouseLeft, tcell.ModNone, fcn); err != nil {
// handle error
}

@ -4,22 +4,26 @@
package gocui
import "errors"
import (
"errors"
"github.com/gdamore/tcell/v2"
)
const maxInt = int(^uint(0) >> 1)
// Editor interface must be satisfied by gocui editors.
type Editor interface {
Edit(v *View, key Key, ch rune, mod Modifier)
Edit(v *View, key tcell.Key, ch rune, mod tcell.ModMask)
}
// The EditorFunc type is an adapter to allow the use of ordinary functions as
// Editors. If f is a function with the appropriate signature, EditorFunc(f)
// is an Editor object that calls f.
type EditorFunc func(v *View, key Key, ch rune, mod Modifier)
type EditorFunc func(v *View, key tcell.Key, ch rune, mod tcell.ModMask)
// Edit calls f(v, key, ch, mod)
func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) {
func (f EditorFunc) Edit(v *View, key tcell.Key, ch rune, mod tcell.ModMask) {
f(v, key, ch, mod)
}
@ -27,27 +31,27 @@ func (f EditorFunc) Edit(v *View, key Key, ch rune, mod Modifier) {
var DefaultEditor Editor = EditorFunc(simpleEditor)
// simpleEditor is used as the default gocui editor.
func simpleEditor(v *View, key Key, ch rune, mod Modifier) {
func simpleEditor(v *View, key tcell.Key, ch rune, mod tcell.ModMask) {
switch {
case ch != 0 && mod == 0:
case key == tcell.KeyRune && ch != 0 && mod == 0:
v.EditWrite(ch)
case key == KeySpace:
case key == ' ':
v.EditWrite(' ')
case key == KeyBackspace || key == KeyBackspace2:
case key == tcell.KeyBackspace || key == tcell.KeyBackspace2:
v.EditDelete(true)
case key == KeyDelete:
case key == tcell.KeyDelete:
v.EditDelete(false)
case key == KeyInsert:
case key == tcell.KeyInsert:
v.Overwrite = !v.Overwrite
case key == KeyEnter:
case key == tcell.KeyEnter:
v.EditNewLine()
case key == KeyArrowDown:
case key == tcell.KeyDown:
v.MoveCursor(0, 1, false)
case key == KeyArrowUp:
case key == tcell.KeyUp:
v.MoveCursor(0, -1, false)
case key == KeyArrowLeft:
case key == tcell.KeyLeft:
v.MoveCursor(-1, 0, false)
case key == KeyArrowRight:
case key == tcell.KeyRight:
v.MoveCursor(1, 0, false)
}
}

@ -8,6 +8,8 @@ import (
"errors"
"github.com/cointop-sh/cointop/pkg/termbox"
"github.com/gdamore/tcell/v2"
log "github.com/sirupsen/logrus"
)
var (
@ -249,27 +251,27 @@ func (g *Gui) CurrentView() *View {
// SetKeybinding creates a new keybinding. If viewname equals to ""
// (empty string) then the keybinding will apply to all views. key must
// be a rune or a Key.
func (g *Gui) SetKeybinding(viewname string, key interface{}, mod Modifier, handler func(*Gui, *View) error) error {
func (g *Gui) SetKeybinding(viewname string, key tcell.Key, ch rune, mod tcell.ModMask, handler func(*Gui, *View) error) error {
var kb *keybinding
k, ch, err := getKey(key)
if err != nil {
return err
}
kb = newKeybinding(viewname, k, ch, mod, handler)
// k, ch, err := getKey(key)
// if err != nil {
// return err
// }
kb = newKeybinding(viewname, key, ch, mod, handler)
g.keybindings = append(g.keybindings, kb)
return nil
}
// DeleteKeybinding deletes a keybinding.
func (g *Gui) DeleteKeybinding(viewname string, key interface{}, mod Modifier) error {
k, ch, err := getKey(key)
if err != nil {
return err
}
func (g *Gui) DeleteKeybinding(viewname string, key tcell.Key, ch rune, mod tcell.ModMask) error {
// k, ch, err := getKey(key)
// if err != nil {
// return err
// }
for i, kb := range g.keybindings {
if kb.viewName == viewname && kb.ch == ch && kb.key == k && kb.mod == mod {
if kb.viewName == viewname && kb.ch == ch && kb.key == key && kb.mod == mod {
g.keybindings = append(g.keybindings[:i], g.keybindings[i+1:]...)
return nil
}
@ -290,16 +292,16 @@ func (g *Gui) DeleteKeybindings(viewname string) {
// getKey takes an empty interface with a key and returns the corresponding
// typed Key or rune.
func getKey(key interface{}) (Key, rune, error) {
switch t := key.(type) {
case Key:
return t, 0, nil
case rune:
return 0, t, nil
default:
return 0, 0, errors.New("unknown type")
}
}
// func getKey(key interface{}) (tcell.Key, rune, error) {
// switch t := key.(type) {
// case Key:
// return t, 0, nil
// case rune:
// return 0, t, nil
// default:
// return 0, 0, errors.New("unknown type")
// }
// }
// userEvent represents an event triggered by the user.
type userEvent struct {
@ -601,7 +603,7 @@ func (g *Gui) onKey(ev *termbox.Event) error {
break
}
if g.currentView != nil && g.currentView.Editable && g.currentView.Editor != nil {
g.currentView.Editor.Edit(g.currentView, Key(ev.Key), ev.Ch, Modifier(ev.Mod))
g.currentView.Editor.Edit(g.currentView, ev.Key, ev.Ch, ev.Mod)
}
case termbox.EventMouse:
v, _, _, err := g.GetViewRelativeMousePosition(ev)
@ -645,12 +647,14 @@ func (g *Gui) SetCursorFromCurrentMouseEvent() error {
// execKeybindings executes the keybinding handlers that match the passed view
// and event. The value of matched is true if there is a match and no errors.
func (g *Gui) execKeybindings(v *View, ev *termbox.Event) (matched bool, err error) {
log.Debugf("XXX hunting for k=%d c=%d mod=%d", ev.Key, ev.Ch, ev.Mod)
matched = false
for _, kb := range g.keybindings {
if kb.handler == nil {
continue
}
if kb.matchKeypress(Key(ev.Key), ev.Ch, Modifier(ev.Mod)) && kb.matchView(v) {
if kb.matchKeypress(ev.Key, ev.Ch, ev.Mod) && kb.matchView(v) {
log.Debugf("XXX dispatching k=%d c=%d mod=%d", ev.Key, ev.Ch, ev.Mod)
if err := kb.handler(g, v); err != nil {
return false, err
}

@ -4,19 +4,22 @@
package gocui
import "github.com/cointop-sh/cointop/pkg/termbox"
import (
"github.com/cointop-sh/cointop/pkg/termbox"
"github.com/gdamore/tcell/v2"
)
// Keybidings are used to link a given key-press event with a handler.
type keybinding struct {
viewName string
key Key
key tcell.Key
ch rune
mod Modifier
mod tcell.ModMask
handler func(*Gui, *View) error
}
// newKeybinding returns a new Keybinding object.
func newKeybinding(viewname string, key Key, ch rune, mod Modifier, handler func(*Gui, *View) error) (kb *keybinding) {
func newKeybinding(viewname string, key tcell.Key, ch rune, mod tcell.ModMask, handler func(*Gui, *View) error) (kb *keybinding) {
kb = &keybinding{
viewName: viewname,
key: key,
@ -28,8 +31,12 @@ func newKeybinding(viewname string, key Key, ch rune, mod Modifier, handler func
}
// matchKeypress returns if the keybinding matches the keypress.
func (kb *keybinding) matchKeypress(key Key, ch rune, mod Modifier) bool {
return kb.key == key && kb.ch == ch && kb.mod == mod
func (kb *keybinding) matchKeypress(key tcell.Key, ch rune, mod tcell.ModMask) bool {
// TODO: check mask not ==mod?
if key == tcell.KeyRune {
return kb.key == key && kb.ch == ch && kb.mod == mod
}
return kb.key == key && kb.mod == mod
}
// matchView returns if the keybinding matches the current view.
@ -41,97 +48,97 @@ func (kb *keybinding) matchView(v *View) bool {
}
// Key represents special keys or keys combinations.
type Key termbox.Key
// type Key tcell.Key
// Special keys.
const (
KeyF1 Key = Key(termbox.KeyF1)
KeyF2 = Key(termbox.KeyF2)
KeyF3 = Key(termbox.KeyF3)
KeyF4 = Key(termbox.KeyF4)
KeyF5 = Key(termbox.KeyF5)
KeyF6 = Key(termbox.KeyF6)
KeyF7 = Key(termbox.KeyF7)
KeyF8 = Key(termbox.KeyF8)
KeyF9 = Key(termbox.KeyF9)
KeyF10 = Key(termbox.KeyF10)
KeyF11 = Key(termbox.KeyF11)
KeyF12 = Key(termbox.KeyF12)
KeyInsert = Key(termbox.KeyInsert)
KeyDelete = Key(termbox.KeyDelete)
KeyHome = Key(termbox.KeyHome)
KeyEnd = Key(termbox.KeyEnd)
KeyPgup = Key(termbox.KeyPgup)
KeyPgdn = Key(termbox.KeyPgdn)
KeyArrowUp = Key(termbox.KeyArrowUp)
KeyArrowDown = Key(termbox.KeyArrowDown)
KeyArrowLeft = Key(termbox.KeyArrowLeft)
KeyArrowRight = Key(termbox.KeyArrowRight)
// KeyF1 Key = tcell.KeyF1
// KeyF2 = tcell.KeyF2
// KeyF3 = tcell.KeyF3
// KeyF4 = tcell.KeyF4
// KeyF5 = tcell.KeyF5
// KeyF6 = tcell.KeyF6
// KeyF7 = tcell.KeyF7
// KeyF8 = tcell.KeyF8
// KeyF9 = tcell.KeyF9
// KeyF10 = tcell.KeyF10
// KeyF11 = tcell.KeyF11
// KeyF12 = tcell.KeyF12
// KeyInsert = tcell.KeyInsert
// KeyDelete = tcell.KeyDelete
// KeyHome = tcell.KeyHome
// KeyEnd = tcell.KeyEnd
// KeyPgup = tcell.KeyPgup
// KeyPgdn = tcell.KeyPgdn
// KeyArrowUp = tcell.KeyArrowUp
// KeyArrowDown = tcell.KeyArrowDown
// KeyArrowLeft = tcell.KeyArrowLeft
// KeyArrowRight = tcell.KeyArrowRight
MouseLeft = Key(termbox.MouseLeft)
MouseMiddle = Key(termbox.MouseMiddle)
MouseRight = Key(termbox.MouseRight)
MouseRelease = Key(termbox.MouseRelease)
MouseWheelUp = Key(termbox.MouseWheelUp)
MouseWheelDown = Key(termbox.MouseWheelDown)
MouseLeft = termbox.MouseLeft
MouseMiddle = termbox.MouseMiddle
MouseRight = termbox.MouseRight
MouseRelease = termbox.MouseRelease
MouseWheelUp = termbox.MouseWheelUp
MouseWheelDown = termbox.MouseWheelDown
)
// Keys combinations.
const (
KeyCtrlTilde Key = Key(termbox.KeyCtrlTilde)
KeyCtrl2 = Key(termbox.KeyCtrl2)
KeyCtrlSpace = Key(termbox.KeyCtrlSpace)
KeyCtrlA = Key(termbox.KeyCtrlA)
KeyCtrlB = Key(termbox.KeyCtrlB)
KeyCtrlC = Key(termbox.KeyCtrlC)
KeyCtrlD = Key(termbox.KeyCtrlD)
KeyCtrlE = Key(termbox.KeyCtrlE)
KeyCtrlF = Key(termbox.KeyCtrlF)
KeyCtrlG = Key(termbox.KeyCtrlG)
KeyBackspace = Key(termbox.KeyBackspace)
KeyCtrlH = Key(termbox.KeyCtrlH)
KeyTab = Key(termbox.KeyTab)
KeyCtrlI = Key(termbox.KeyCtrlI)
KeyCtrlJ = Key(termbox.KeyCtrlJ)
KeyCtrlK = Key(termbox.KeyCtrlK)
KeyCtrlL = Key(termbox.KeyCtrlL)
KeyEnter = Key(termbox.KeyEnter)
KeyCtrlM = Key(termbox.KeyCtrlM)
KeyCtrlN = Key(termbox.KeyCtrlN)
KeyCtrlO = Key(termbox.KeyCtrlO)
KeyCtrlP = Key(termbox.KeyCtrlP)
KeyCtrlQ = Key(termbox.KeyCtrlQ)
KeyCtrlR = Key(termbox.KeyCtrlR)
KeyCtrlS = Key(termbox.KeyCtrlS)
KeyCtrlT = Key(termbox.KeyCtrlT)
KeyCtrlU = Key(termbox.KeyCtrlU)
KeyCtrlV = Key(termbox.KeyCtrlV)
KeyCtrlW = Key(termbox.KeyCtrlW)
KeyCtrlX = Key(termbox.KeyCtrlX)
KeyCtrlY = Key(termbox.KeyCtrlY)
KeyCtrlZ = Key(termbox.KeyCtrlZ)
KeyEsc = Key(termbox.KeyEsc)
KeyCtrlLsqBracket = Key(termbox.KeyCtrlLsqBracket)
KeyCtrl3 = Key(termbox.KeyCtrl3)
KeyCtrl4 = Key(termbox.KeyCtrl4)
KeyCtrlBackslash = Key(termbox.KeyCtrlBackslash)
KeyCtrl5 = Key(termbox.KeyCtrl5)
KeyCtrlRsqBracket = Key(termbox.KeyCtrlRsqBracket)
KeyCtrl6 = Key(termbox.KeyCtrl6)
KeyCtrl7 = Key(termbox.KeyCtrl7)
KeyCtrlSlash = Key(termbox.KeyCtrlSlash)
KeyCtrlUnderscore = Key(termbox.KeyCtrlUnderscore)
KeySpace = Key(termbox.KeySpace)
KeyBackspace2 = Key(termbox.KeyBackspace2)
KeyCtrl8 = Key(termbox.KeyCtrl8)
)
// const (
// KeyCtrlTilde tcell.Key = tcell.KeyCtrlTilde
// KeyCtrl2 = tcell.KeyCtrl2
// KeyCtrlSpace = tcell.KeyCtrlSpace
// KeyCtrlA = tcell.KeyCtrlA
// KeyCtrlB = tcell.KeyCtrlB
// KeyCtrlC = tcell.KeyCtrlC
// KeyCtrlD = tcell.KeyCtrlD
// KeyCtrlE = tcell.KeyCtrlE
// KeyCtrlF = tcell.KeyCtrlF
// KeyCtrlG = tcell.KeyCtrlG
// KeyBackspace = tcell.KeyBackspace
// KeyCtrlH = tcell.KeyCtrlH
// KeyTab = tcell.KeyTab
// KeyCtrlI = tcell.KeyCtrlI
// KeyCtrlJ = tcell.KeyCtrlJ
// KeyCtrlK = tcell.KeyCtrlK
// KeyCtrlL = tcell.KeyCtrlL
// KeyEnter = tcell.KeyEnter
// KeyCtrlM = tcell.KeyCtrlM
// KeyCtrlN = tcell.KeyCtrlN
// KeyCtrlO = tcell.KeyCtrlO
// KeyCtrlP = tcell.KeyCtrlP
// KeyCtrlQ = tcell.KeyCtrlQ
// KeyCtrlR = tcell.KeyCtrlR
// KeyCtrlS = tcell.KeyCtrlS
// KeyCtrlT = tcell.KeyCtrlT
// KeyCtrlU = tcell.KeyCtrlU
// KeyCtrlV = tcell.KeyCtrlV
// KeyCtrlW = tcell.KeyCtrlW
// KeyCtrlX = tcell.KeyCtrlX
// KeyCtrlY = tcell.KeyCtrlY
// KeyCtrlZ = tcell.KeyCtrlZ
// KeyEsc = tcell.KeyEsc
// KeyCtrlLsqBracket = tcell.KeyCtrlLsqBracket
// KeyCtrl3 = tcell.KeyCtrl3
// KeyCtrl4 = tcell.KeyCtrl4
// KeyCtrlBackslash = tcell.KeyCtrlBackslash
// KeyCtrl5 = tcell.KeyCtrl5
// KeyCtrlRsqBracket = tcell.KeyCtrlRsqBracket
// KeyCtrl6 = tcell.KeyCtrl6
// KeyCtrl7 = tcell.KeyCtrl7
// KeyCtrlSlash = tcell.KeyCtrlSlash
// KeyCtrlUnderscore = tcell.KeyCtrlUnderscore
// KeySpace = tcell.KeySpace
// KeyBackspace2 = tcell.KeyBackspace2
// KeyCtrl8 = tcell.KeyCtrl8
// )
// Modifier allows to define special keys combinations. They can be used
// in combination with Keys or Runes when a new keybinding is defined.
type Modifier termbox.Modifier
// type Modifier tcell.ModMask
// Modifiers.
const (
ModNone Modifier = Modifier(0)
ModAlt = Modifier(termbox.ModAlt)
)
// const (
// ModNone Modifier = Modifier(0)
// ModAlt = Modifier(termbox.ModAlt)
// )

@ -216,16 +216,16 @@ func SetCell(x, y int, ch rune, fg, bg Attribute) {
type EventType uint8
// Modifier represents the possible modifier keys.
type Modifier tcell.ModMask
// type Modifier tcell.ModMask
// Key is a key press.
type Key tcell.Key
// type Key tcell.Key
// Event represents an event like a key press, mouse action, or window resize.
type Event struct {
Type EventType
Mod Modifier
Key Key
Mod tcell.ModMask
Key tcell.Key
Ch rune
Width int
Height int
@ -248,91 +248,17 @@ const (
// Keys codes.
const (
KeyF1 = Key(tcell.KeyF1)
KeyF2 = Key(tcell.KeyF2)
KeyF3 = Key(tcell.KeyF3)
KeyF4 = Key(tcell.KeyF4)
KeyF5 = Key(tcell.KeyF5)
KeyF6 = Key(tcell.KeyF6)
KeyF7 = Key(tcell.KeyF7)
KeyF8 = Key(tcell.KeyF8)
KeyF9 = Key(tcell.KeyF9)
KeyF10 = Key(tcell.KeyF10)
KeyF11 = Key(tcell.KeyF11)
KeyF12 = Key(tcell.KeyF12)
KeyInsert = Key(tcell.KeyInsert)
KeyDelete = Key(tcell.KeyDelete)
KeyHome = Key(tcell.KeyHome)
KeyEnd = Key(tcell.KeyEnd)
KeyArrowUp = Key(tcell.KeyUp)
KeyArrowDown = Key(tcell.KeyDown)
KeyArrowRight = Key(tcell.KeyRight)
KeyArrowLeft = Key(tcell.KeyLeft)
KeyCtrlA = Key(tcell.KeyCtrlA)
KeyCtrlB = Key(tcell.KeyCtrlB)
KeyCtrlC = Key(tcell.KeyCtrlC)
KeyCtrlD = Key(tcell.KeyCtrlD)
KeyCtrlE = Key(tcell.KeyCtrlE)
KeyCtrlF = Key(tcell.KeyCtrlF)
KeyCtrlG = Key(tcell.KeyCtrlG)
KeyCtrlH = Key(tcell.KeyCtrlH)
KeyCtrlI = Key(tcell.KeyCtrlI)
KeyCtrlJ = Key(tcell.KeyCtrlJ)
KeyCtrlK = Key(tcell.KeyCtrlK)
KeyCtrlL = Key(tcell.KeyCtrlL)
KeyCtrlM = Key(tcell.KeyCtrlM)
KeyCtrlN = Key(tcell.KeyCtrlN)
KeyCtrlO = Key(tcell.KeyCtrlO)
KeyCtrlP = Key(tcell.KeyCtrlP)
KeyCtrlQ = Key(tcell.KeyCtrlQ)
KeyCtrlR = Key(tcell.KeyCtrlR)
KeyCtrlS = Key(tcell.KeyCtrlS)
KeyCtrlT = Key(tcell.KeyCtrlT)
KeyCtrlU = Key(tcell.KeyCtrlU)
KeyCtrlV = Key(tcell.KeyCtrlV)
KeyCtrlW = Key(tcell.KeyCtrlW)
KeyCtrlX = Key(tcell.KeyCtrlX)
KeyCtrlY = Key(tcell.KeyCtrlY)
KeyCtrlZ = Key(tcell.KeyCtrlZ)
KeyCtrlUnderscore = Key(tcell.KeyCtrlUnderscore)
KeyBackspace = Key(tcell.KeyBackspace)
KeyBackspace2 = Key(tcell.KeyBackspace2)
KeyTab = Key(tcell.KeyTab)
KeyEnter = Key(tcell.KeyEnter)
KeyEsc = Key(tcell.KeyEscape)
KeyPgdn = Key(tcell.KeyPgDn)
KeyPgup = Key(tcell.KeyPgUp)
KeySpace = Key(tcell.Key(' '))
KeyTilde = Key(tcell.Key('~'))
KeyCtrlSpace = Key(tcell.KeyCtrlSpace)
// The following assignments are provided for termbox
// compatibility. Their use in applications is discouraged.
// The mouse keys are completely not supported as tcell uses
// a separate mouse event instead of key strokes.
MouseLeft = Key(tcell.KeyF63) // arbitrary assignments
MouseRight = Key(tcell.KeyF62)
MouseMiddle = Key(tcell.KeyF61)
MouseRelease = Key(tcell.KeyF60)
MouseWheelUp = Key(tcell.KeyF59)
MouseWheelDown = Key(tcell.KeyF58)
KeyCtrlTilde = Key(tcell.KeyCtrlSpace) // termbox defines a bunch of weird ones, don't use them
KeyCtrl2 = Key(tcell.KeyNUL)
KeyCtrl3 = Key(tcell.KeyEscape)
KeyCtrl4 = Key(tcell.KeyCtrlBackslash)
KeyCtrl5 = Key(tcell.KeyCtrlRightSq)
KeyCtrl6 = Key(tcell.KeyCtrlCarat)
KeyCtrl7 = Key(tcell.KeyCtrlUnderscore)
KeyCtrl8 = Key(tcell.KeyDEL)
KeyCtrlSlash = Key(tcell.KeyCtrlUnderscore)
KeyCtrlRsqBracket = Key(tcell.KeyCtrlRightSq)
KeyCtrlBackslash = Key(tcell.KeyCtrlBackslash)
KeyCtrlLsqBracket = Key(tcell.KeyCtrlLeftSq)
MouseLeft = tcell.KeyF63 // arbitrary assignments
MouseRight = tcell.KeyF62
MouseMiddle = tcell.KeyF61
MouseRelease = tcell.KeyF60
MouseWheelUp = tcell.KeyF59
MouseWheelDown = tcell.KeyF58
)
// Modifiers.
const (
ModAlt = Modifier(tcell.ModAlt)
ModAlt = tcell.ModAlt
)
func makeEvent(tev tcell.Event) Event {
@ -343,28 +269,29 @@ func makeEvent(tev tcell.Event) Event {
w, h := tev.Size()
return Event{Type: EventResize, Width: w, Height: h}
case *tcell.EventKey:
k := tev.Key()
ch := rune(0)
if k == tcell.KeyRune {
ch = tev.Rune()
if ch == ' ' {
k = tcell.Key(' ')
} else {
k = tcell.Key(0)
}
}
mod := tev.Modifiers()
// k := tev.Key()
// ch := rune(0)
// Remove space hack
// if k == tcell.KeyRune {
// ch = tev.Rune()
// if ch == ' ' {
// k = tcell.Key(' ')
// } else {
// k = tcell.Key(0)
// }
// }
// mod := tev.Modifiers()
return Event{
Type: EventKey,
Key: Key(k),
Ch: ch,
Mod: Modifier(mod),
Key: tev.Key(),
Ch: tev.Rune(),
Mod: tev.Modifiers(),
}
case *tcell.EventMouse:
x, y := tev.Position()
button := tev.Buttons()
// Don't worry about combo buttons for now
key := Key(tcell.KeyNUL)
key := tcell.KeyNUL
if button&tcell.Button1 != 0 {
key = MouseLeft
} else if button&tcell.Button2 != 0 {

@ -5,7 +5,6 @@
package termui
import (
"fmt"
"path"
"sync"
"time"
@ -50,25 +49,25 @@ func evtKbd(e termbox.Event) EvtKbd {
// }
// TODO: FIXME
if e.Key <= 0x7F {
pre = "C-"
k = fmt.Sprintf("%v", 'a'-1+int(e.Key))
kmap := map[termbox.Key][2]string{
termbox.KeyCtrlSpace: {"C-", "<space>"}, // TODO: FIXME
termbox.KeyBackspace: {"", "<backspace>"},
termbox.KeyTab: {"", "<tab>"},
termbox.KeyEnter: {"", "<enter>"},
termbox.KeyEsc: {"", "<escape>"},
termbox.KeyCtrlBackslash: {"C-", "\\"},
termbox.KeyCtrlSlash: {"C-", "/"},
termbox.KeySpace: {"", "<space>"},
termbox.KeyCtrl8: {"C-", "8"}, // TODO: FIXME
}
if sk, ok := kmap[e.Key]; ok {
pre = sk[0]
k = sk[1]
}
}
// if e.Key <= 0x7F {
// pre = "C-"
// k = fmt.Sprintf("%v", 'a'-1+int(e.Key))
// kmap := map[termbox.Key][2]string{
// termbox.KeyCtrlSpace: {"C-", "<space>"}, // TODO: FIXME
// termbox.KeyBackspace: {"", "<backspace>"},
// termbox.KeyTab: {"", "<tab>"},
// termbox.KeyEnter: {"", "<enter>"},
// termbox.KeyEsc: {"", "<escape>"},
// termbox.KeyCtrlBackslash: {"C-", "\\"},
// termbox.KeyCtrlSlash: {"C-", "/"},
// termbox.KeySpace: {"", "<space>"},
// termbox.KeyCtrl8: {"C-", "8"}, // TODO: FIXME
// }
// if sk, ok := kmap[e.Key]; ok {
// pre = sk[0]
// k = sk[1]
// }
// }
}
ek.KeyStr = pre + mod + k

Loading…
Cancel
Save