Skip to content

Commit

Permalink
Merge pull request #63 from voidshard/master
Browse files Browse the repository at this point in the history
Fix panic on Ctrl+Arrow
  • Loading branch information
issadarkthing authored Jul 3, 2022
2 parents 3f27aaf + 51d55bb commit cda14ff
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 22 deletions.
34 changes: 26 additions & 8 deletions anko/anko.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,18 @@ func (a *Anko) KeybindExists(panel string, eventKey *tcell.EventKey) bool {
name := eventKey.Name()

if strings.Contains(name, "Ctrl") {
key := extractCtrlRune(name)
key, ok := extractCtrlRune(name)
if !ok {
return false
}
src = fmt.Sprintf("Keybinds.%s[\"ctrl_%s\"]",
panel, strings.ToLower(string(key)))

} else if strings.Contains(name, "Alt") {
key := extractAltRune(name)
key, ok := extractAltRune(name)
if !ok {
return false
}
src = fmt.Sprintf("Keybinds.%s[\"alt_%c\"]", panel, key)

} else if strings.Contains(name, "Rune") {
Expand All @@ -186,12 +192,18 @@ func (a *Anko) ExecKeybind(panel string, eventKey *tcell.EventKey) error {
name := eventKey.Name()

if strings.Contains(name, "Ctrl") {
key := extractCtrlRune(name)
key, ok := extractCtrlRune(name)
if !ok {
return nil
}
src = fmt.Sprintf("Keybinds.%s[\"ctrl_%s\"]()",
panel, strings.ToLower(string(key)))

} else if strings.Contains(name, "Alt") {
key := extractAltRune(name)
key, ok := extractAltRune(name)
if !ok {
return nil
}
src = fmt.Sprintf("Keybinds.%s[\"alt_%c\"]()", panel, key)

} else if strings.Contains(name, "Rune") {
Expand All @@ -210,14 +222,20 @@ func (a *Anko) ExecKeybind(panel string, eventKey *tcell.EventKey) error {
return nil
}

func extractCtrlRune(str string) rune {
func extractCtrlRune(str string) (rune, bool) {
re := regexp.MustCompile(`\+(.)$`)
x := re.FindStringSubmatch(str)
return rune(x[0][1])
if len(x) == 0 {
return rune(' '), false
}
return rune(x[0][1]), true
}

func extractAltRune(str string) rune {
func extractAltRune(str string) (rune, bool) {
re := regexp.MustCompile(`\[(.)\]`)
x := re.FindStringSubmatch(str)
return rune(x[0][1])
if len(x) == 0 {
return rune(' '), false
}
return rune(x[0][1]), true
}
34 changes: 20 additions & 14 deletions anko/anko_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,37 +145,43 @@ func TestExtractCtrlRune(t *testing.T) {
tests := []struct {
in string
out rune
ok bool
}{
{in: "Ctrl+x", out: 'x'},
{in: "Ctrl+]", out: ']'},
{in: "Ctrl+%", out: '%'},
{in: "Ctrl+^", out: '^'},
{in: "Ctrl+7", out: '7'},
{in: "Ctrl+B", out: 'B'},
{in: "Ctrl+x", out: 'x', ok: true},
{in: "Ctrl+]", out: ']', ok: true},
{in: "Ctrl+%", out: '%', ok: true},
{in: "Ctrl+^", out: '^', ok: true},
{in: "Ctrl+7", out: '7', ok: true},
{in: "Ctrl+B", out: 'B', ok: true},
{in: "Ctrl+Down", out: ' ', ok: false},
{in: "Ctrl+Left", out: ' ', ok: false},
}

for _, test := range tests {
got := extractCtrlRune(test.in)
got, ok := extractCtrlRune(test.in)
assert.Equal(t, test.out, got)
assert.Equal(t, test.ok, ok)
}
}

func TestExtractAltRune(t *testing.T) {
tests := []struct {
in string
out rune
ok bool
}{
{in: "Alt+Rune[x]", out: 'x'},
{in: "Alt+Rune[]]", out: ']'},
{in: "Alt+Rune[%]", out: '%'},
{in: "Alt+Rune[^]", out: '^'},
{in: "Alt+Rune[7]", out: '7'},
{in: "Alt+Rune[B]", out: 'B'},
{in: "Alt+Rune[x]", out: 'x', ok: true},
{in: "Alt+Rune[]]", out: ']', ok: true},
{in: "Alt+Rune[%]", out: '%', ok: true},
{in: "Alt+Rune[^]", out: '^', ok: true},
{in: "Alt+Rune[7]", out: '7', ok: true},
{in: "Alt+Rune[B]", out: 'B', ok: true},
}

for _, test := range tests {
got := extractAltRune(test.in)
got, ok := extractAltRune(test.in)
assert.Equal(t, test.out, got)
assert.Equal(t, test.ok, ok)
}
}

Expand Down

0 comments on commit cda14ff

Please sign in to comment.