Add module awesome-gocui/keybinding

This commit is contained in:
cxsu 2020-12-27 20:28:29 +09:00
parent 5e9380dc63
commit b310bdb2d6
4 changed files with 10 additions and 192 deletions

1
go.mod
View file

@ -6,6 +6,7 @@ require (
github.com/Azure/go-ansiterm v0.0.0-20170929234023-d6e3b3328b78 // indirect
github.com/Microsoft/go-winio v0.4.14 // indirect
github.com/awesome-gocui/gocui v0.6.0
github.com/awesome-gocui/keybinding v1.0.0
github.com/cespare/xxhash v1.1.0
github.com/docker/cli v0.0.0-20190906153656-016a3232168d
github.com/docker/distribution v2.7.1+incompatible // indirect

3
go.sum
View file

@ -10,8 +10,11 @@ github.com/OneOfOne/xxhash v1.2.2/go.mod h1:HSdplMjZKSmBqAxg5vPj2TmRDmfkzw+cTzAE
github.com/alecthomas/template v0.0.0-20160405071501-a0175ee3bccc/go.mod h1:LOuyumcjzFXgccqObfd/Ljyb9UuFJ6TxHnclSeseNhc=
github.com/alecthomas/units v0.0.0-20151022065526-2efee857e7cf/go.mod h1:ybxpYRFXyAe+OPACYpWeL0wqObRcbAqCMya13uyzqw0=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/awesome-gocui/gocui v0.5.0/go.mod h1:1QikxFaPhe2frKeKvEwZEIGia3haiOxOUXKinrv17mA=
github.com/awesome-gocui/gocui v0.6.0 h1:hhDJiQC12tEsJNJ+iZBBVaSSLFYo9llFuYpQlL5JZVI=
github.com/awesome-gocui/gocui v0.6.0/go.mod h1:1QikxFaPhe2frKeKvEwZEIGia3haiOxOUXKinrv17mA=
github.com/awesome-gocui/keybinding v1.0.0 h1:CrnjCfEhWpjcqIQUan9IllaXeRGELdwfjeUmY7ljbng=
github.com/awesome-gocui/keybinding v1.0.0/go.mod h1:z0TyCwIhaT97yU+becTse8Dqh2CvYT0FLw0R0uTk0ag=
github.com/awesome-gocui/termbox-go v0.0.0-20190427202837-c0aef3d18bcc h1:wGNpKcHU8Aadr9yOzsT3GEsFLS7HQu8HxQIomnekqf0=
github.com/awesome-gocui/termbox-go v0.0.0-20190427202837-c0aef3d18bcc/go.mod h1:tOy3o5Nf1bA17mnK4W41gD7PS3u4Cv0P0pqFcoWMy8s=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=

View file

@ -4,10 +4,10 @@ import (
"fmt"
"github.com/awesome-gocui/gocui"
"github.com/awesome-gocui/keybinding"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
"github.com/wagoodman/dive/runtime/ui/format"
// "github.com/wagoodman/keybinding"
)
type BindingInfo struct {
@ -20,7 +20,7 @@ type BindingInfo struct {
}
type Binding struct {
key []Key
key []keybinding.Key
displayName string
selectedFn func() bool
actionFn func() error
@ -53,11 +53,11 @@ func GenerateBindings(gui *gocui.Gui, influence string, infos []BindingInfo) ([]
}
func NewBinding(gui *gocui.Gui, influence string, key gocui.Key, mod gocui.Modifier, displayName string, actionFn func() error) (*Binding, error) {
return newBinding(gui, influence, []Key{{Value: key, Modifier: mod}}, displayName, actionFn)
return newBinding(gui, influence, []keybinding.Key{{Value: key, Modifier: mod}}, displayName, actionFn)
}
func NewBindingFromConfig(gui *gocui.Gui, influence string, configKeys []string, displayName string, actionFn func() error) (*Binding, error) {
var parsedKeys []Key
var parsedKeys []keybinding.Key
for _, configKey := range configKeys {
bindStr := viper.GetString(configKey)
if bindStr == "" {
@ -66,7 +66,7 @@ func NewBindingFromConfig(gui *gocui.Gui, influence string, configKeys []string,
}
logrus.Debugf("parsing keybinding '%s' --> '%s'", configKey, bindStr)
keys, err := ParseAll(bindStr)
keys, err := keybinding.ParseAll(bindStr)
if err != nil {
return nil, err
}
@ -83,7 +83,7 @@ func NewBindingFromConfig(gui *gocui.Gui, influence string, configKeys []string,
return newBinding(gui, influence, parsedKeys, displayName, actionFn)
}
func newBinding(gui *gocui.Gui, influence string, keys []Key, displayName string, actionFn func() error) (*Binding, error) {
func newBinding(gui *gocui.Gui, influence string, keys []keybinding.Key, displayName string, actionFn func() error) (*Binding, error) {
binding := &Binding{
key: keys,
displayName: displayName,

View file

@ -1,186 +0,0 @@
package key
import (
"fmt"
"strings"
"unicode"
"github.com/awesome-gocui/gocui"
)
var translate = map[string]string{
"/": "Slash",
"\\": "Backslash",
"[": "LsqBracket",
"]": "RsqBracket",
"_": "Underscore",
"escape": "Esc",
"~": "Tilde",
"pageup": "Pgup",
"pagedown": "Pgdn",
"pgup": "Pgup",
"pgdown": "Pgdn",
"up": "ArrowUp",
"down": "ArrowDown",
"right": "ArrowRight",
"left": "ArrowLeft",
"ctl": "Ctrl",
}
var display = map[string]string{
"Slash": "/",
"Backslash": "\\",
"LsqBracket": "[",
"RsqBracket": "]",
"Underscore": "_",
"Tilde": "~",
"Ctrl": "^",
}
var supportedKeybindings = map[string]gocui.Key{
"KeyF1": gocui.KeyF1,
"KeyF2": gocui.KeyF2,
"KeyF3": gocui.KeyF3,
"KeyF4": gocui.KeyF4,
"KeyF5": gocui.KeyF5,
"KeyF6": gocui.KeyF6,
"KeyF7": gocui.KeyF7,
"KeyF8": gocui.KeyF8,
"KeyF9": gocui.KeyF9,
"KeyF10": gocui.KeyF10,
"KeyF11": gocui.KeyF11,
"KeyF12": gocui.KeyF12,
"KeyInsert": gocui.KeyInsert,
"KeyDelete": gocui.KeyDelete,
"KeyHome": gocui.KeyHome,
"KeyEnd": gocui.KeyEnd,
"KeyPgup": gocui.KeyPgup,
"KeyPgdn": gocui.KeyPgdn,
"KeyArrowUp": gocui.KeyArrowUp,
"KeyArrowDown": gocui.KeyArrowDown,
"KeyArrowLeft": gocui.KeyArrowLeft,
"KeyArrowRight": gocui.KeyArrowRight,
"KeyCtrlTilde": gocui.KeyCtrlTilde,
"KeyCtrl2": gocui.KeyCtrl2,
"KeyCtrlSpace": gocui.KeyCtrlSpace,
"KeyCtrlA": gocui.KeyCtrlA,
"KeyCtrlB": gocui.KeyCtrlB,
"KeyCtrlC": gocui.KeyCtrlC,
"KeyCtrlD": gocui.KeyCtrlD,
"KeyCtrlE": gocui.KeyCtrlE,
"KeyCtrlF": gocui.KeyCtrlF,
"KeyCtrlG": gocui.KeyCtrlG,
"KeyBackspace": gocui.KeyBackspace,
"KeyCtrlH": gocui.KeyCtrlH,
"KeyTab": gocui.KeyTab,
"KeyCtrlI": gocui.KeyCtrlI,
"KeyCtrlJ": gocui.KeyCtrlJ,
"KeyCtrlK": gocui.KeyCtrlK,
"KeyCtrlL": gocui.KeyCtrlL,
"KeyEnter": gocui.KeyEnter,
"KeyCtrlM": gocui.KeyCtrlM,
"KeyCtrlN": gocui.KeyCtrlN,
"KeyCtrlO": gocui.KeyCtrlO,
"KeyCtrlP": gocui.KeyCtrlP,
"KeyCtrlQ": gocui.KeyCtrlQ,
"KeyCtrlR": gocui.KeyCtrlR,
"KeyCtrlS": gocui.KeyCtrlS,
"KeyCtrlT": gocui.KeyCtrlT,
"KeyCtrlU": gocui.KeyCtrlU,
"KeyCtrlV": gocui.KeyCtrlV,
"KeyCtrlW": gocui.KeyCtrlW,
"KeyCtrlX": gocui.KeyCtrlX,
"KeyCtrlY": gocui.KeyCtrlY,
"KeyCtrlZ": gocui.KeyCtrlZ,
"KeyEsc": gocui.KeyEsc,
"KeyCtrlLsqBracket": gocui.KeyCtrlLsqBracket,
"KeyCtrl3": gocui.KeyCtrl3,
"KeyCtrl4": gocui.KeyCtrl4,
"KeyCtrlBackslash": gocui.KeyCtrlBackslash,
"KeyCtrl5": gocui.KeyCtrl5,
"KeyCtrlRsqBracket": gocui.KeyCtrlRsqBracket,
"KeyCtrl6": gocui.KeyCtrl6,
"KeyCtrl7": gocui.KeyCtrl7,
"KeyCtrlSlash": gocui.KeyCtrlSlash,
"KeyCtrlUnderscore": gocui.KeyCtrlUnderscore,
"KeySpace": gocui.KeySpace,
"KeyBackspace2": gocui.KeyBackspace2,
"KeyCtrl8": gocui.KeyCtrl8,
}
type Key struct {
Value gocui.Key
Modifier gocui.Modifier
Tokens []string
}
func Parse(input string) (Key, error) {
f := func(c rune) bool { return unicode.IsSpace(c) || c == '+' }
tokens := strings.FieldsFunc(input, f)
var normalizedTokens []string
var modifier = gocui.ModNone
for _, token := range tokens {
normalized := strings.ToLower(token)
if value, exists := translate[normalized]; exists {
normalized = value
} else {
normalized = strings.Title(normalized)
}
if normalized == "Alt" {
modifier = gocui.ModAlt
continue
}
if len(normalized) == 1 {
normalizedTokens = append(normalizedTokens, strings.ToUpper(normalized))
continue
}
normalizedTokens = append(normalizedTokens, normalized)
}
lookup := "Key" + strings.Join(normalizedTokens, "")
if key, exists := supportedKeybindings[lookup]; exists {
return Key{key, modifier, normalizedTokens}, nil
}
if modifier != gocui.ModNone {
return Key{0, modifier, normalizedTokens}, fmt.Errorf("unsupported keybinding: %s (+%+v)", lookup, modifier)
}
return Key{0, modifier, normalizedTokens}, fmt.Errorf("unsupported keybinding: %s", lookup)
}
func ParseAll(input string) ([]Key, error) {
ret := make([]Key, 0)
for _, value := range strings.Split(input, ",") {
key, err := Parse(value)
if err != nil {
return nil, fmt.Errorf("could not parse keybinding '%s' from request '%s': %+v", value, input, err)
}
ret = append(ret, key)
}
if len(ret) == 0 {
return nil, fmt.Errorf("must have at least one keybinding")
}
return ret, nil
}
func (key Key) String() string {
displayTokens := make([]string, 0)
prefix := ""
for _, token := range key.Tokens {
if token == "Ctrl" {
prefix = "^"
continue
}
if value, exists := display[token]; exists {
token = value
}
displayTokens = append(displayTokens, token)
}
return prefix + strings.Join(displayTokens, "+")
}