Skip to content

Commit

Permalink
Added mouse support
Browse files Browse the repository at this point in the history
Signed-off-by: Glenn Vriesman <[email protected]>
  • Loading branch information
glvr182 committed Aug 4, 2019
1 parent 0835533 commit 98ac2d2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
7 changes: 4 additions & 3 deletions examples/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ func show(keyStr string) {
// must will call the MustParseAll function
func must(keyStr string) {
defer func() {
if r := recover(); r != nil {
fmt.Println("Error caught: ", r)
}
if r := recover(); r != nil {
fmt.Println("Error caught: ", r)
}
}()

keys := keybinding.MustParseAll(keyStr)
Expand All @@ -32,6 +32,7 @@ func main() {
fmt.Println("The show calls:")
show("ctrl+a")
show("ctrl+b")
show("MouseLeft")
show("ctrl+/, tab")
show("ctrl+ alt +/")
show("jibber+ jabber +/") // This will fail
Expand Down
27 changes: 24 additions & 3 deletions keybinding.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,31 @@ var supportedKeybindings = map[string]gocui.Key{
"KeySpace": gocui.KeySpace,
"KeyBackspace2": gocui.KeyBackspace2,
"KeyCtrl8": gocui.KeyCtrl8,
"MouseLeft": gocui.MouseLeft,
"MouseMiddle": gocui.MouseMiddle,
"MouseRight": gocui.MouseRight,
"MouseRelease": gocui.MouseRelease,
"MouseWheelUp": gocui.MouseWheelUp,
"MouseWheelDown": gocui.MouseWheelDown,
}

// Key contains all relevant information about the key
type Key struct {
Value gocui.Key
Modifier gocui.Modifier
Tokens []string
}

// Parse turns the input string into an actual Key.
// Returns an error when something goes wrong
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)
normalized := token

if value, exists := translate[normalized]; exists {
normalized = value
Expand All @@ -142,7 +151,10 @@ func Parse(input string) (Key, error) {
normalizedTokens = append(normalizedTokens, normalized)
}

lookup := "Key" + strings.Join(normalizedTokens, "")
lookup := strings.Join(normalizedTokens, "")
if !strings.Contains(lookup, "Mouse") {
lookup = "Key" + strings.Join(normalizedTokens, "")
}

if key, exists := supportedKeybindings[lookup]; exists {
return Key{key, modifier, normalizedTokens}, nil
Expand All @@ -151,9 +163,12 @@ func Parse(input string) (Key, error) {
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)
}

// ParseAll parses all strings to a Key.
// Returns an error when something goes wrong.
func ParseAll(input string) ([]Key, error) {
ret := make([]Key, 0)
for _, value := range strings.Split(input, ",") {
Expand All @@ -169,7 +184,9 @@ func ParseAll(input string) ([]Key, error) {
return ret, nil
}


// MustParse parses the input string to a key but instead an
// error, it panics when things go wrong.
// This forces the caller to react to an error
func MustParse(input string) Key {
if key, err := Parse(input); err != nil {
panic(err)
Expand All @@ -178,6 +195,9 @@ func MustParse(input string) Key {
}
}

// MustParseAll parses all the input strings to a key but instead an
// error, it panics when things go wrong.
// This forces the caller to react to an error
func MustParseAll(input string) []Key {
if key, err := ParseAll(input); err != nil {
panic(err)
Expand All @@ -186,6 +206,7 @@ func MustParseAll(input string) []Key {
}
}

// String returns the Key in string form
func (key Key) String() string {
displayTokens := make([]string, 0)
prefix := ""
Expand Down

0 comments on commit 98ac2d2

Please sign in to comment.