Improve handling of key names
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Remember the exact string given as the key name so that it's possible to correctly handle synonyms and print the original string.
authored by Junegunn Choi · committed by Junegunn Choi · parent 5e8d8da · Browse files · 3 files·+142 added−124 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+142 added−124 removed
- other:+142 added−124 removed
Provenance
- Commit
a8b2c25- Tree
ba9b6ec- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
5e8d8da
3 files changed+142 added−124 removed
modified:src/options_test.go+80 added-56 removed
70 unmodified lines71 }72 }73 74 func TestParseKeys(t *testing.T) {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)75+ pairs := parseKeyChords("ctrl-z,alt-z,f2,@,Alt-a,!,ctrl-G,J,g", "")76+ check := func(i int, s string) {77+ if pairs[i] != s {78+ t.Errorf("%s != %s", pairs[i], s)79 }80 }81- check(len(keys), 9)82- check(keys[0], curses.CtrlZ)83- check(keys[1], curses.AltZ)84- check(keys[2], curses.F2)85- check(keys[3], curses.AltZ+'@')86- check(keys[4], curses.AltA)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')81+ if len(pairs) != 9 {82+ t.Error(9)83+ }84+ check(curses.CtrlZ, "ctrl-z")85+ check(curses.AltZ, "alt-z")86+ check(curses.F2, "f2")87+ check(curses.AltZ+'@', "@")88+ check(curses.AltA, "Alt-a")89+ check(curses.AltZ+'!', "!")90+ check(curses.CtrlA+'g'-'a', "ctrl-G")91+ check(curses.AltZ+'J', "J")92+ check(curses.AltZ+'g', "g")93 94 // 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)95+ pairs = parseKeyChords("enter,Return,space,tab,btab,esc,up,down,left,right", "")96+ if len(pairs) != 9 {97+ t.Error(9)98+ }99+ check(curses.CtrlM, "Return")100+ check(curses.AltZ+' ', "space")101+ check(curses.Tab, "tab")102+ check(curses.BTab, "btab")103+ check(curses.ESC, "esc")104+ check(curses.Up, "up")105+ check(curses.Down, "down")106+ check(curses.Left, "left")107+ check(curses.Right, "right")108+109+ pairs = parseKeyChords("Tab,Ctrl-I,PgUp,page-up,pgdn,Page-Down,Home,End,Alt-BS,Alt-BSpace,shift-left,shift-right,btab,shift-tab,return,Enter,bspace", "")110+ if len(pairs) != 11 {111+ t.Error(11)112+ }113+ check(curses.Tab, "Ctrl-I")114+ check(curses.PgUp, "page-up")115+ check(curses.PgDn, "Page-Down")116+ check(curses.Home, "Home")117+ check(curses.End, "End")118+ check(curses.AltBS, "Alt-BSpace")119+ check(curses.SLeft, "shift-left")120+ check(curses.SRight, "shift-right")121+ check(curses.BTab, "shift-tab")122+ check(curses.CtrlM, "Enter")123+ check(curses.BSpace, "bspace")124 }125 126 func TestParseKeysWithComma(t *testing.T) {108- check := func(key int, expected int) {109- if key != expected {110- t.Errorf("%d != %d", key, expected)127+ checkN := func(a int, b int) {128+ if a != b {129+ t.Errorf("%d != %d", a, b)130 }131 }132+ check := func(pairs map[int]string, i int, s string) {133+ if pairs[i] != s {134+ t.Errorf("%s != %s", pairs[i], s)135+ }136+ }137 114- keys := parseKeyChords(",", "", false)115- check(len(keys), 1)116- check(keys[0], curses.AltZ+',')138+ pairs := parseKeyChords(",", "")139+ checkN(len(pairs), 1)140+ check(pairs, curses.AltZ+',', ",")141 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+',')142+ pairs = parseKeyChords(",,a,b", "")143+ checkN(len(pairs), 3)144+ check(pairs, curses.AltZ+'a', "a")145+ check(pairs, curses.AltZ+'b', "b")146+ check(pairs, curses.AltZ+',', ",")147 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+',')148+ pairs = parseKeyChords("a,b,,", "")149+ checkN(len(pairs), 3)150+ check(pairs, curses.AltZ+'a', "a")151+ check(pairs, curses.AltZ+'b', "b")152+ check(pairs, curses.AltZ+',', ",")153 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+',')154+ pairs = parseKeyChords("a,,,b", "")155+ checkN(len(pairs), 3)156+ check(pairs, curses.AltZ+'a', "a")157+ check(pairs, curses.AltZ+'b', "b")158+ check(pairs, curses.AltZ+',', ",")159 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+',')160+ pairs = parseKeyChords("a,,,b,c", "")161+ checkN(len(pairs), 4)162+ check(pairs, curses.AltZ+'a', "a")163+ check(pairs, curses.AltZ+'b', "b")164+ check(pairs, curses.AltZ+'c', "c")165+ check(pairs, curses.AltZ+',', ",")166 143- keys = parseKeyChords(",,,", "", false)144- check(len(keys), 1)145- check(keys[0], curses.AltZ+',')167+ pairs = parseKeyChords(",,,", "")168+ checkN(len(pairs), 1)169+ check(pairs, curses.AltZ+',', ",")170 }171 172 func TestBind(t *testing.T) {173 check := func(action actionType, expected actionType) {95 unmodified linesmodified:src/options.go+56 added-52 removed
115 unmodified lines116 Select1 bool117 Exit0 bool118 Filter *string119 ToggleSort bool120- Expect []int120+ Expect map[int]string121 Keymap map[int]actionType122 Execmap map[int]string123 PrintQuery bool124 ReadZero bool33 unmodified lines158 Select1: false,159 Exit0: false,160 Filter: nil,161 ToggleSort: false,162- Expect: []int{},162+ Expect: make(map[int]string),163 Keymap: defaultKeymap(),164 Execmap: make(map[int]string),165 PrintQuery: false,166 ReadZero: false,97 unmodified lines264 func isAlphabet(char uint8) bool {265 return char >= 'a' && char <= 'z'266 }267 268-func parseKeyChords(str string, message string, bind bool) []int {268+func parseKeyChords(str string, message string) map[int]string {269 if len(str) == 0 {270 errorExit(message)271 }272 1 unmodified line274 if str == "," || strings.HasPrefix(str, ",,") || strings.HasSuffix(str, ",,") || strings.Index(str, ",,,") >= 0 {275 tokens = append(tokens, ",")276 }277 278- var chords []int278+ chords := make(map[int]string)279 for _, key := range tokens {280 if len(key) == 0 {281 continue // ignore282 }283 lkey := strings.ToLower(key)284 chord := 0285- if bind {286- switch lkey {287- case "up":288- chord = curses.Up289- case "down":290- chord = curses.Down291- case "left":292- chord = curses.Left293- case "right":294- chord = curses.Right295- case "enter", "return":296- chord = curses.CtrlM297- case "space":298- chord = curses.AltZ + int(' ')299- case "bspace":300- chord = curses.BSpace301- case "alt-bs", "alt-bspace":302- chord = curses.AltBS303- case "tab":304- chord = curses.Tab305- case "btab":306- chord = curses.BTab307- case "esc":308- chord = curses.ESC309- case "del":310- chord = curses.Del311- case "home":312- chord = curses.Home313- case "end":314- chord = curses.End315- case "pgup", "page-up":316- chord = curses.PgUp317- case "pgdn", "page-down":318- chord = curses.PgDn319- case "shift-left":320- chord = curses.SLeft321- case "shift-right":322- chord = curses.SRight323- }285+ switch lkey {286+ case "up":287+ chord = curses.Up288+ case "down":289+ chord = curses.Down290+ case "left":291+ chord = curses.Left292+ case "right":293+ chord = curses.Right294+ case "enter", "return":295+ chord = curses.CtrlM296+ case "space":297+ chord = curses.AltZ + int(' ')298+ case "bspace", "bs":299+ chord = curses.BSpace300+ case "alt-bs", "alt-bspace":301+ chord = curses.AltBS302+ case "tab":303+ chord = curses.Tab304+ case "btab", "shift-tab":305+ chord = curses.BTab306+ case "esc":307+ chord = curses.ESC308+ case "del":309+ chord = curses.Del310+ case "home":311+ chord = curses.Home312+ case "end":313+ chord = curses.End314+ case "pgup", "page-up":315+ chord = curses.PgUp316+ case "pgdn", "page-down":317+ chord = curses.PgDn318+ case "shift-left":319+ chord = curses.SLeft320+ case "shift-right":321+ chord = curses.SRight322+ default:324- }325- if chord == 0 {323 if len(key) == 6 && strings.HasPrefix(lkey, "ctrl-") && isAlphabet(lkey[5]) {324 chord = curses.CtrlA + int(lkey[5]) - 'a'325 } else if len(key) == 5 && strings.HasPrefix(lkey, "alt-") && isAlphabet(lkey[4]) {326 chord = curses.AltA + int(lkey[4]) - 'a'5 unmodified lines332 errorExit("unsupported key: " + key)333 }334 }335 if chord > 0 {339- chords = append(chords, chord)336+ chords[chord] = key337 }338 }339 return chords340 }83 unmodified lines424 }425 426 var executeRegexp *regexp.Regexp427 428+func firstKey(keymap map[int]string) int {429+ for k := range keymap {430+ return k431+ }432+ return 0433+}434+435 func parseKeymap(keymap map[int]actionType, execmap map[int]string, toggleSort bool, str string) (map[int]actionType, map[int]string, bool) {436 if executeRegexp == nil {437 // Backreferences are not supported.438 // "~!@#$%^&*;/|".each_char.map { |c| Regexp.escape(c) }.map { |c| "#{c}[^#{c}]*#{c}" }.join('|')15 unmodified lines454 pair := strings.SplitN(pairStr, ":", 2)455 if len(pair) != 2 {456 fail()457 }454- keys := parseKeyChords(pair[0], "key name required", true)458+ keys := parseKeyChords(pair[0], "key name required")459 if len(keys) != 1 {460 fail()461 }458- key := keys[0]462+ key := firstKey(keys)463 act := strings.ToLower(pair[1])464 switch act {465 case "ignore":466 keymap[key] = actIgnore87 unmodified lines554 return false555 }556 557 func checkToggleSort(keymap map[int]actionType, str string) map[int]actionType {554- keys := parseKeyChords(str, "key name required", true)558+ keys := parseKeyChords(str, "key name required")559 if len(keys) != 1 {560 errorExit("multiple keys specified")561 }558- keymap[keys[0]] = actToggleSort562+ keymap[firstKey(keys)] = actToggleSort563 return keymap564 }565 566 func parseOptions(opts *Options, allArgs []string) {36 unmodified lines603 case "-f", "--filter":604 filter := nextString(allArgs, &i, "query string required")605 opts.Filter = &filter606 case "--expect":603- opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required", false)607+ opts.Expect = parseKeyChords(nextString(allArgs, &i, "key names required"), "key names required")608 case "--tiebreak":609 opts.Tiebreak = parseTiebreak(nextString(allArgs, &i, "sort criterion required"))610 case "--bind":611 keymap, opts.Execmap, opts.ToggleSort =108 unmodified lines720 } else if match, value := optString(arg, "--toggle-sort="); match {721 keymap = checkToggleSort(keymap, value)722 opts.ToggleSort = true723 } else if match, value := optString(arg, "--expect="); match {720- opts.Expect = parseKeyChords(value, "key names required", false)724+ opts.Expect = parseKeyChords(value, "key names required")725 } else if match, value := optString(arg, "--tiebreak="); match {726 opts.Tiebreak = parseTiebreak(value)727 } else if match, value := optString(arg, "--color="); match {728 opts.Theme = parseTheme(opts.Theme, value)52 unmodified linesmodified:src/terminal.go+6 added-16 removed
31 unmodified lines32 input []rune33 multi bool34 sort bool35 toggleSort bool36- expect []int36+ expect map[int]string37 keymap map[int]actionType38 execmap map[int]string39- pressed int39+ pressed string40 printQuery bool41 history *History42 cycle bool43 count int148 unmodified lines192 toggleSort: opts.ToggleSort,193 expect: opts.Expect,194 keymap: opts.Keymap,195 execmap: opts.Execmap,196- pressed: 0,196+ pressed: "",197 printQuery: opts.PrintQuery,198 history: opts.History,199 cycle: opts.Cycle,200 reading: true,55 unmodified lines256 if t.printQuery {257 fmt.Println(string(t.input))258 }259 if len(t.expect) > 0 {260- if t.pressed == 0 {261- fmt.Println()260+ fmt.Println(t.pressed)262- } else if util.Between(t.pressed, C.AltA, C.AltZ) {263- fmt.Printf("alt-%c\n", t.pressed+'a'-C.AltA)264- } else if util.Between(t.pressed, C.F1, C.F4) {265- fmt.Printf("f%c\n", t.pressed+'1'-C.F1)266- } else if util.Between(t.pressed, C.CtrlA, C.CtrlZ) {267- fmt.Printf("ctrl-%c\n", t.pressed+'a'-C.CtrlA)268- } else {269- fmt.Printf("%c\n", t.pressed-C.AltZ)270- }261 }262 if len(t.selected) == 0 {263 cnt := t.merger.Length()264 if cnt > 0 && cnt > t.cy {451 unmodified lines716 toggleY(t.cy)717 req(reqInfo)718 }719 }730- for _, key := range t.expect {720+ for key, ret := range t.expect {721 if keyMatch(key, event) {732- t.pressed = key722+ t.pressed = ret723 req(reqClose)724 break725 }726 }257 unmodified lines