Skip to content
Myr

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

Before adoption: this commit came in with the history the project adopted, so no landing records it.

enter (return), space, tab, btab, esc, up, down, left, right

authored by Junegunn Choi · committed by Junegunn Choi · parent e287bd7 · Browse files · 2 files+64 added−22 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+64 added−22 removed

  • other:+64 added−22 removed

2 files+64 added−22 removed

modified:src/options_test.go+21 added-7 removed
70 unmodified lines
71 }72}7374func TestParseKeys(t *testing.T) {75 keys := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g", "")75 keys := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g", "", false)76 check := func(key int, expected int) {77 if key != expected {78 t.Errorf("%d != %d", key, expected)79 }
7 unmodified lines
87 check(keys[5], curses.AltZ+'!')88 check(keys[6], curses.CtrlA+'g'-'a')89 check(keys[7], curses.AltZ+'J')90 check(keys[8], curses.AltZ+'g')9192 // Synonyms93 keys = parseKeyChords("enter,return,space,tab,btab,esc,up,down,left,right", "", true)94 check(len(keys), 10)95 check(keys[0], curses.CtrlM)96 check(keys[1], curses.CtrlM)97 check(keys[2], curses.AltZ+' ')98 check(keys[3], curses.Tab)99 check(keys[4], curses.BTab)100 check(keys[5], curses.ESC)101 check(keys[6], curses.Up)102 check(keys[7], curses.Down)103 check(keys[8], curses.Left)104 check(keys[9], curses.Right)105}106107func TestParseKeysWithComma(t *testing.T) {108 check := func(key int, expected int) {
1 unmodified line
110 t.Errorf("%d != %d", key, expected)111 }112 }113100 keys := parseKeyChords(",", "")114 keys := parseKeyChords(",", "", false)115 check(len(keys), 1)116 check(keys[0], curses.AltZ+',')117104 keys = parseKeyChords(",,a,b", "")118 keys = parseKeyChords(",,a,b", "", false)119 check(len(keys), 3)120 check(keys[0], curses.AltZ+'a')121 check(keys[1], curses.AltZ+'b')122 check(keys[2], curses.AltZ+',')123110 keys = parseKeyChords("a,b,,", "")124 keys = parseKeyChords("a,b,,", "", false)125 check(len(keys), 3)126 check(keys[0], curses.AltZ+'a')127 check(keys[1], curses.AltZ+'b')128 check(keys[2], curses.AltZ+',')129116 keys = parseKeyChords("a,,,b", "")130 keys = parseKeyChords("a,,,b", "", false)131 check(len(keys), 3)132 check(keys[0], curses.AltZ+'a')133 check(keys[1], curses.AltZ+'b')134 check(keys[2], curses.AltZ+',')135122 keys = parseKeyChords("a,,,b,c", "")136 keys = parseKeyChords("a,,,b,c", "", false)137 check(len(keys), 4)138 check(keys[0], curses.AltZ+'a')139 check(keys[1], curses.AltZ+'b')140 check(keys[2], curses.AltZ+'c')141 check(keys[3], curses.AltZ+',')142129 keys = parseKeyChords(",,,", "")143 keys = parseKeyChords(",,,", "", false)144 check(len(keys), 1)145 check(keys[0], curses.AltZ+',')146}147
95 unmodified lines
modified:src/options.go+43 added-15 removed
260 unmodified lines
261func isAlphabet(char uint8) bool {262 return char >= 'a' && char <= 'z'263}264265func parseKeyChords(str string, message string) []int {265func parseKeyChords(str string, message string, bind bool) []int {266 if len(str) == 0 {267 errorExit(message)268 }269
7 unmodified lines
277 if len(key) == 0 {278 continue // ignore279 }280 lkey := strings.ToLower(key)281 if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {282 chords = append(chords, curses.CtrlA+int(lkey[5])-'a')283 } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {284 chords = append(chords, curses.AltA+int(lkey[4])-'a')285 } else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '4' {286 chords = append(chords, curses.F1+int(key[1])-'1')287 } else if utf8.RuneCountInString(key) == 1 {288 chords = append(chords, curses.AltZ+int([]rune(key)[0]))289 } else {290 errorExit("unsupported key: " + key)281 chord := 0282 if bind {283 switch lkey {284 case "up":285 chord = curses.Up286 case "down":287 chord = curses.Down288 case "left":289 chord = curses.Left290 case "right":291 chord = curses.Right292 case "enter", "return":293 chord = curses.CtrlM294 case "space":295 chord = curses.AltZ + int(' ')296 case "tab":297 chord = curses.Tab298 case "btab":299 chord = curses.BTab300 case "esc":301 chord = curses.ESC302 }303 }304 if chord == 0 {305 if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {306 chord = curses.CtrlA + int(lkey[5]) - 'a'307 } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {308 chord = curses.AltA + int(lkey[4]) - 'a'309 } else if len(key) == 2 && strings.HasPrefix(lkey, "f") && key[1] >= '1' && key[1] <= '4' {310 chord = curses.F1 + int(key[1]) - '1'311 } else if utf8.RuneCountInString(key) == 1 {312 chord = curses.AltZ + int([]rune(key)[0])313 } else {314 errorExit("unsupported key: " + key)315 }316 }317 if chord > 0 {318 chords = append(chords, chord)319 }320 }321 return chords322}323
105 unmodified lines
429 pair := strings.SplitN(pairStr, ":", 2)430 if len(pair) != 2 {431 fail()432 }405 keys := parseKeyChords(pair[0], "key name required")433 keys := parseKeyChords(pair[0], "key name required", true)434 if len(keys) != 1 {435 fail()436 }437 key := keys[0]
87 unmodified lines
525 return false526}527528func checkToggleSort(keymap map[int]actionType, str string) map[int]actionType {501 keys := parseKeyChords(str, "key name required")529 keys := parseKeyChords(str, "key name required", true)530 if len(keys) != 1 {531 errorExit("multiple keys specified")532 }533 keymap[keys[0]] = actToggleSort
40 unmodified lines
574 case "-f", "--filter":575 filter := nextString(allArgs, &i, "query string required")576 opts.Filter = &filter577 case "--expect":550 opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required")578 opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required", false)579 case "--tiebreak":580 opts.Tiebreak = parseTiebreak(nextString(allArgs, &i, "sort criterion required"))581 case "--bind":582 keymap, opts.Execmap, opts.ToggleSort =
104 unmodified lines
687 } else if match, value := optString(arg, "--toggle-sort="); match {688 keymap = checkToggleSort(keymap, value)689 opts.ToggleSort = true690 } else if match, value := optString(arg, "--expect="); match {663 opts.Expect = parseKeyChords(value, "key names required")691 opts.Expect = parseKeyChords(value, "key names required", false)692 } else if match, value := optString(arg, "--tiebreak="); match {693 opts.Tiebreak = parseTiebreak(value)694 } else if match, value := optString(arg, "--color="); match {695 opts.Theme = parseTheme(opts.Theme, value)
52 unmodified lines