summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2015-06-15 01:26:18 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2015-06-15 01:26:18 +0900
commit7db53e645923944a711e81515a2fcc5bb014f81f (patch)
tree9ea370a9dd4e12ab2373de4f8e1bfefbab61f6af
parente287bd7f04fd31b237d24616167074ada16e8bf1 (diff)
Add synonyms for some keys to be used with --bind and --toggle-sort
enter (return), space, tab, btab, esc, up, down, left, right
-rw-r--r--src/options.go58
-rw-r--r--src/options_test.go28
2 files changed, 64 insertions, 22 deletions
diff --git a/src/options.go b/src/options.go
index fbeb9e40..2bd013d0 100644
--- a/src/options.go
+++ b/src/options.go
@@ -262,7 +262,7 @@ func isAlphabet(char uint8) bool {
return char >= 'a' && char <= 'z'
}
-func parseKeyChords(str string, message string) []int {
+func parseKeyChords(str string, message string, bind bool) []int {
if len(str) == 0 {
errorExit(message)
}
@@ -278,16 +278,44 @@ func parseKeyChords(str string, message string) []int {
continue // ignore
}
lkey := strings.ToLower(key)
- if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {
- chords = append(chords, curses.CtrlA+int(lkey[5])-'a')
- } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {
- chords = append(chords, curses.AltA+int(lkey[4])-'a')
- } else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '4' {
- chords = append(chords, curses.F1+int(key[1])-'1')
- } else if utf8.RuneCountInString(key) == 1 {
- chords = append(chords, curses.AltZ+int([]rune(key)[0]))
- } else {
- errorExit("unsupported key: " + key)
+ chord := 0
+ if bind {
+ switch lkey {
+ case "up":
+ chord = curses.Up
+ case "down":
+ chord = curses.Down
+ case "left":
+ chord = curses.Left
+ case "right":
+ chord = curses.Right
+ case "enter", "return":
+ chord = curses.CtrlM
+ case "space":
+ chord = curses.AltZ + int(' ')
+ case "tab":
+ chord = curses.Tab
+ case "btab":
+ chord = curses.BTab
+ case "esc":
+ chord = curses.ESC
+ }
+ }
+ if chord == 0 {
+ if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {
+ chord = curses.CtrlA + int(lkey[5]) - 'a'
+ } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {
+ chord = curses.AltA + int(lkey[4]) - 'a'
+ } else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '4' {
+ chord = curses.F1 + int(key[1]) - '1'
+ } else if utf8.RuneCountInString(key) == 1 {
+ chord = curses.AltZ + int([]rune(key)[0])
+ } else {
+ errorExit("unsupported key: " + key)
+ }
+ }
+ if chord > 0 {
+ chords = append(chords, chord)
}
}
return chords
@@ -402,7 +430,7 @@ func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort b
if len(pair) != 2 {
fail()
}
- keys := parseKeyChords(pair[0], "key name required")
+ keys := parseKeyChords(pair[0], "key name required", true)
if len(keys) != 1 {
fail()
}
@@ -498,7 +526,7 @@ func isExecuteAction(str string) bool {
}
func checkToggleSort(keymap map[int]actionType, str string) map[int]actionType {
- keys := parseKeyChords(str, "key name required")
+ keys := parseKeyChords(str, "key name required", true)
if len(keys) != 1 {
errorExit("multiple keys specified")
}
@@ -547,7 +575,7 @@ func parseOptions(opts *Options, allArgs []string) {
filter := nextString(allArgs, &i, "query string required")
opts.Filter = &filter
case "--expect":
- opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required")
+ opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required", false)
case "--tiebreak":
opts.Tiebreak = parseTiebreak(nextString(allArgs, &i, "sort criterion required"))
case "--bind":
@@ -660,7 +688,7 @@ func parseOptions(opts *Options, allArgs []string) {
keymap = checkToggleSort(keymap, value)
opts.ToggleSort = true
} else if match, value := optString(arg, "--expect="); match {
- opts.Expect = parseKeyChords(value, "key names required")
+ opts.Expect = parseKeyChords(value, "key names required", false)
} else if match, value := optString(arg, "--tiebreak="); match {
opts.Tiebreak = parseTiebreak(value)
} else if match, value := optString(arg, "--color="); match {
diff --git a/src/options_test.go b/src/options_test.go
index 297acbfd..630bde3b 100644
--- a/src/options_test.go
+++ b/src/options_test.go
@@ -72,7 +72,7 @@ func TestIrrelevantNth(t *testing.T) {
}
func TestParseKeys(t *testing.T) {
- keys := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g", "")
+ keys := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g", "", false)
check := func(key int, expected int) {
if key != expected {
t.Errorf("%d != %d", key, expected)
@@ -88,6 +88,20 @@ func TestParseKeys(t *testing.T) {
check(keys[6], curses.CtrlA+'g'-'a')
check(keys[7], curses.AltZ+'J')
check(keys[8], curses.AltZ+'g')
+
+ // Synonyms
+ keys = parseKeyChords("enter,return,space,tab,btab,esc,up,down,left,right", "", true)
+ check(len(keys), 10)
+ check(keys[0], curses.CtrlM)
+ check(keys[1], curses.CtrlM)
+ check(keys[2], curses.AltZ+' ')
+ check(keys[3], curses.Tab)
+ check(keys[4], curses.BTab)
+ check(keys[5], curses.ESC)
+ check(keys[6], curses.Up)
+ check(keys[7], curses.Down)
+ check(keys[8], curses.Left)
+ check(keys[9], curses.Right)
}
func TestParseKeysWithComma(t *testing.T) {
@@ -97,36 +111,36 @@ func TestParseKeysWithComma(t *testing.T) {
}
}
- keys := parseKeyChords(",", "")
+ keys := parseKeyChords(",", "", false)
check(len(keys), 1)
check(keys[0], curses.AltZ+',')
- keys = parseKeyChords(",,a,b", "")
+ keys = parseKeyChords(",,a,b", "", false)
check(len(keys), 3)
check(keys[0], curses.AltZ+'a')
check(keys[1], curses.AltZ+'b')
check(keys[2], curses.AltZ+',')
- keys = parseKeyChords("a,b,,", "")
+ keys = parseKeyChords("a,b,,", "", false)
check(len(keys), 3)
check(keys[0], curses.AltZ+'a')
check(keys[1], curses.AltZ+'b')
check(keys[2], curses.AltZ+',')
- keys = parseKeyChords("a,,,b", "")
+ keys = parseKeyChords("a,,,b", "", false)
check(len(keys), 3)
check(keys[0], curses.AltZ+'a')
check(keys[1], curses.AltZ+'b')
check(keys[2], curses.AltZ+',')
- keys = parseKeyChords("a,,,b,c", "")
+ keys = parseKeyChords("a,,,b,c", "", false)
check(len(keys), 4)
check(keys[0], curses.AltZ+'a')
check(keys[1], curses.AltZ+'b')
check(keys[2], curses.AltZ+'c')
check(keys[3], curses.AltZ+',')
- keys = parseKeyChords(",,,", "")
+ keys = parseKeyChords(",,,", "", false)
check(len(keys), 1)
check(keys[0], curses.AltZ+',')
}