Add synonyms for some keys to be used with --bind and --toggle-sort

enter (return), space, tab, btab, esc, up, down, left, right
This commit is contained in:
Junegunn Choi 2015-06-15 01:26:18 +09:00
parent e287bd7f04
commit 7db53e6459
2 changed files with 64 additions and 22 deletions

View File

@ -262,7 +262,7 @@ func isAlphabet(char uint8) bool {
return char >= 'a' && char <= 'z' 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 { if len(str) == 0 {
errorExit(message) errorExit(message)
} }
@ -278,16 +278,44 @@ func parseKeyChords(str string, message string) []int {
continue // ignore continue // ignore
} }
lkey := strings.ToLower(key) lkey := strings.ToLower(key)
if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) { chord := 0
chords = append(chords, curses.CtrlA+int(lkey[5])-'a') if bind {
} else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) { switch lkey {
chords = append(chords, curses.AltA+int(lkey[4])-'a') case "up":
} else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '4' { chord = curses.Up
chords = append(chords, curses.F1+int(key[1])-'1') case "down":
} else if utf8.RuneCountInString(key) == 1 { chord = curses.Down
chords = append(chords, curses.AltZ+int([]rune(key)[0])) case "left":
} else { chord = curses.Left
errorExit("unsupported key: " + key) 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 return chords
@ -402,7 +430,7 @@ func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort b
if len(pair) != 2 { if len(pair) != 2 {
fail() fail()
} }
keys := parseKeyChords(pair[0], "key name required") keys := parseKeyChords(pair[0], "key name required", true)
if len(keys) != 1 { if len(keys) != 1 {
fail() fail()
} }
@ -498,7 +526,7 @@ func isExecuteAction(str string) bool {
} }
func checkToggleSort(keymap map[int]actionType, str string) map[int]actionType { 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 { if len(keys) != 1 {
errorExit("multiple keys specified") errorExit("multiple keys specified")
} }
@ -547,7 +575,7 @@ func parseOptions(opts *Options, allArgs []string) {
filter := nextString(allArgs, &i, "query string required") filter := nextString(allArgs, &i, "query string required")
opts.Filter = &filter opts.Filter = &filter
case "--expect": 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": case "--tiebreak":
opts.Tiebreak = parseTiebreak(nextString(allArgs, &i, "sort criterion required")) opts.Tiebreak = parseTiebreak(nextString(allArgs, &i, "sort criterion required"))
case "--bind": case "--bind":
@ -660,7 +688,7 @@ func parseOptions(opts *Options, allArgs []string) {
keymap = checkToggleSort(keymap, value) keymap = checkToggleSort(keymap, value)
opts.ToggleSort = true opts.ToggleSort = true
} else if match, value := optString(arg, "--expect="); match { } 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 { } else if match, value := optString(arg, "--tiebreak="); match {
opts.Tiebreak = parseTiebreak(value) opts.Tiebreak = parseTiebreak(value)
} else if match, value := optString(arg, "--color="); match { } else if match, value := optString(arg, "--color="); match {

View File

@ -72,7 +72,7 @@ func TestIrrelevantNth(t *testing.T) {
} }
func TestParseKeys(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) { check := func(key int, expected int) {
if key != expected { if key != expected {
t.Errorf("%d != %d", 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[6], curses.CtrlA+'g'-'a')
check(keys[7], curses.AltZ+'J') check(keys[7], curses.AltZ+'J')
check(keys[8], curses.AltZ+'g') 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) { func TestParseKeysWithComma(t *testing.T) {
@ -97,36 +111,36 @@ func TestParseKeysWithComma(t *testing.T) {
} }
} }
keys := parseKeyChords(",", "") keys := parseKeyChords(",", "", false)
check(len(keys), 1) check(len(keys), 1)
check(keys[0], curses.AltZ+',') check(keys[0], curses.AltZ+',')
keys = parseKeyChords(",,a,b", "") keys = parseKeyChords(",,a,b", "", false)
check(len(keys), 3) check(len(keys), 3)
check(keys[0], curses.AltZ+'a') check(keys[0], curses.AltZ+'a')
check(keys[1], curses.AltZ+'b') check(keys[1], curses.AltZ+'b')
check(keys[2], curses.AltZ+',') check(keys[2], curses.AltZ+',')
keys = parseKeyChords("a,b,,", "") keys = parseKeyChords("a,b,,", "", false)
check(len(keys), 3) check(len(keys), 3)
check(keys[0], curses.AltZ+'a') check(keys[0], curses.AltZ+'a')
check(keys[1], curses.AltZ+'b') check(keys[1], curses.AltZ+'b')
check(keys[2], curses.AltZ+',') check(keys[2], curses.AltZ+',')
keys = parseKeyChords("a,,,b", "") keys = parseKeyChords("a,,,b", "", false)
check(len(keys), 3) check(len(keys), 3)
check(keys[0], curses.AltZ+'a') check(keys[0], curses.AltZ+'a')
check(keys[1], curses.AltZ+'b') check(keys[1], curses.AltZ+'b')
check(keys[2], curses.AltZ+',') check(keys[2], curses.AltZ+',')
keys = parseKeyChords("a,,,b,c", "") keys = parseKeyChords("a,,,b,c", "", false)
check(len(keys), 4) check(len(keys), 4)
check(keys[0], curses.AltZ+'a') check(keys[0], curses.AltZ+'a')
check(keys[1], curses.AltZ+'b') check(keys[1], curses.AltZ+'b')
check(keys[2], curses.AltZ+'c') check(keys[2], curses.AltZ+'c')
check(keys[3], curses.AltZ+',') check(keys[3], curses.AltZ+',')
keys = parseKeyChords(",,,", "") keys = parseKeyChords(",,,", "", false)
check(len(keys), 1) check(len(keys), 1)
check(keys[0], curses.AltZ+',') check(keys[0], curses.AltZ+',')
} }