Add jump and jump-accept actions for --bind
Before adoption: this commit came in with the history the project adopted, so no landing records it.
jump and jump-accept implement EasyMotion-like movement in fzf. Suggested by @mhrebenyuk. Close #569.
authored by Junegunn Choi · committed by Junegunn Choi · parent 4adebfc · Browse files · 4 files·+117 added−13 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+117 added−13 removed
- tests:+37 added−0 removed
- other:+80 added−13 removed
Provenance
- Commit
6d235bc- Tree
7649dba- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
4adebfc
4 files changed+117 added−13 removed
modified:src/constants.go+3 added
34 unmodified lines35 mergerCacheMax int = 10000036 37 // History38 defaultHistoryMax int = 100039+40+ // Jump labels41+ defaultJumpLabels string = "qwertyuiopasdfghjklzxcvbnm1234567890QWERTYUIOPASDFGHJKLZXCVBNM"42 )43 44 // fzf events45 const (15 unmodified linesmodified:src/options.go+15 added
43 unmodified lines44 --no-hscroll Disable horizontal scroll45 --hscroll-off=COL Number of screen columns to keep to the right of the46 highlighted substring (default: 10)47 --inline-info Display finder info inline with the query48+ --jump-labels=CHARS Label characters for jump and jump-accept49 --prompt=STR Input prompt (default: '> ')50 --bind=KEYBINDS Custom key bindings. Refer to the man page.51 --history=FILE History file52 --history-size=N Maximum number of history entries (default: 1000)59 unmodified lines112 Cycle bool113 Hscroll bool114 HscrollOff int115 InlineInfo bool116+ JumpLabels string117 Prompt string118 Query string119 Select1 bool120 Exit0 bool33 unmodified lines154 Cycle: false,155 Hscroll: true,156 HscrollOff: 10,157 InlineInfo: false,158+ JumpLabels: defaultJumpLabels,159 Prompt: "> ",160 Query: "",161 Select1: false,162 Exit0: false,392 unmodified lines555 case "forward-char":556 keymap[key] = actForwardChar557 case "forward-word":558 keymap[key] = actForwardWord559+ case "jump":560+ keymap[key] = actJump561+ case "jump-accept":562+ keymap[key] = actJumpAccept563 case "kill-line":564 keymap[key] = actKillLine565 case "kill-word":566 keymap[key] = actKillWord243 unmodified lines810 case "--inline-info":811 opts.InlineInfo = true812 case "--no-inline-info":813 opts.InlineInfo = false814+ case "--jump-labels":815+ opts.JumpLabels = nextString(allArgs, &i, "label characters required")816 case "-1", "--select-1":817 opts.Select1 = true818 case "+1", "--no-select-1":819 opts.Select1 = false79 unmodified lines899 } else if match, value := optString(arg, "--tabstop="); match {900 opts.Tabstop = atoi(value)901 } else if match, value := optString(arg, "--hscroll-off="); match {902 opts.HscrollOff = atoi(value)903+ } else if match, value := optString(arg, "--jump-labels="); match {904+ opts.JumpLabels = value905 } else {906 errorExit("unknown option: " + arg)907 }908 }9 unmodified lines918 919 if opts.Tabstop < 1 {920 errorExit("tab stop must be a positive integer")921 }922+923+ if len(opts.JumpLabels) == 0 {924+ errorExit("empty jump labels")925+ }926 }927 928 func postProcessOptions(opts *Options) {929 // Default actions for CTRL-N / CTRL-P when --history is set47 unmodified linesmodified:src/terminal.go+62 added-13 removed
17 unmodified lines18 19 "github.com/junegunn/go-runewidth"20 )21 22+type jumpMode int23+24+const (25+ jumpDisabled jumpMode = iota26+ jumpEnabled27+ jumpAcceptEnabled28+)29+30 // Terminal represents terminal input/output31 type Terminal struct {32 initDelay time.Duration33 inlineInfo bool23 unmodified lines57 marginInt [4]int58 count int59 progress int60 reading bool61+ jumping jumpMode62+ jumpLabels string63 merger *Merger64 selected map[int32]selectedItem65 reqBox *util.EventBox66 eventBox *util.EventBox30 unmodified lines97 reqPrompt util.EventType = iota98 reqInfo99 reqHeader100 reqList101+ reqJump102 reqRefresh103 reqRedraw104 reqClose105 reqPrintQuery37 unmodified lines143 actDown144 actUp145 actPageUp146 actPageDown147+ actJump148+ actJumpAccept149 actPrintQuery150 actToggleSort151 actPreviousHistory152 actNextHistory94 unmodified lines247 header: header,248 header0: header,249 ansi: opts.Ansi,250 reading: true,251+ jumping: jumpDisabled,252+ jumpLabels: opts.JumpLabels,253 merger: EmptyMerger,254 selected: make(map[int32]selectedItem),255 reqBox: util.NewEventBox(),256 eventBox: eventBox,254 unmodified lines511 line--512 }513 t.move(line, 0, true)514 if i < count {500- t.printItem(t.merger.Get(i+t.offset), i == t.cy-t.offset)515+ t.printItem(t.merger.Get(i+t.offset), i, i == t.cy-t.offset)516 }517 }518 }519 505-func (t *Terminal) printItem(item *Item, current bool) {520+func (t *Terminal) printItem(item *Item, i int, current bool) {521 _, selected := t.selected[item.Index()]522+ label := " "523+ if t.jumping != jumpDisabled {524+ if i < len(t.jumpLabels) {525+ // Striped526+ current = i%2 == 0527+ label = t.jumpLabels[i : i+1]528+ }529+ } else if current {530+ label = ">"531+ }532+ C.CPrint(C.ColCursor, true, label)533 if current {508- C.CPrint(C.ColCursor, true, ">")534 if selected {535 C.CPrint(C.ColSelected, true, ">")536 } else {537 C.CPrint(C.ColCurrent, true, " ")538 }539 t.printHighlighted(item, true, C.ColCurrent, C.ColCurrentMatch, true)540 } else {516- C.CPrint(C.ColCursor, true, " ")541 if selected {542 C.CPrint(C.ColSelected, true, ">")543 } else {544 C.Print(" ")284 unmodified lines829 case reqInfo:830 t.printInfo()831 case reqList:832 t.printList()833+ case reqJump:834+ if t.merger.Length() == 0 {835+ t.jumping = jumpDisabled836+ }837+ t.printList()838 case reqHeader:839 t.printHeader()840 case reqRefresh:841 t.suppress = false211 unmodified lines1053 req(reqList)1054 case actPageDown:1055 t.vmove(-(t.maxItems() - 1))1056 req(reqList)1057+ case actJump:1058+ t.jumping = jumpEnabled1059+ req(reqJump)1060+ case actJumpAccept:1061+ t.jumping = jumpAcceptEnabled1062+ req(reqJump)1063 case actBackwardWord:1064 t.cx = findLastMatch("[^[:alnum:]][[:alnum:]]", string(t.input[:t.cx])) + 11065 case actForwardWord:1066 t.cx += findFirstMatch("[[:alnum:]][^[:alnum:]]|(.$)", string(t.input[t.cx:])) + 171 unmodified lines1138 }1139 }1140 return true1141 }1107- action := t.keymap[event.Type]1142+ changed := false1143 mapkey := event.Type1144+ if t.jumping == jumpDisabled {1145+ action := t.keymap[mapkey]1109- if event.Type == C.Rune {1110- mapkey = int(event.Char) + int(C.AltZ)1111- if act, prs := t.keymap[mapkey]; prs {1112- action = act1146+ if mapkey == C.Rune {1147+ mapkey = int(event.Char) + int(C.AltZ)1148+ if act, prs := t.keymap[mapkey]; prs {1149+ action = act1150+ }1151 }1152+ if !doAction(action, mapkey) {1153+ continue1154+ }1155+ changed = string(previousInput) != string(t.input)1156+ } else {1157+ if mapkey == C.Rune {1158+ if idx := strings.IndexRune(t.jumpLabels, event.Char); idx >= 0 {1159+ t.cy = idx + t.offset1160+ if t.jumping == jumpAcceptEnabled {1161+ req(reqClose)1162+ }1163+ }1164+ }1165+ t.jumping = jumpDisabled1166+ req(reqList)1167 }1115- if !doAction(action, mapkey) {1116- continue1117- }1118- changed := string(previousInput) != string(t.input)1168 t.mutex.Unlock() // Must be unlocked before touching reqBox1169 1170 if changed {1171 t.eventBox.Set(EvtSearchNew, t.sort)54 unmodified linesmodified:test/test_go.rb+37 added
1179 unmodified lines1180 tmux.until { |lines| lines[-2] == ' 28/1000' }1181 tmux.send_keys :Enter1182 end1183 1184+ def test_jump1185+ tmux.send_keys "seq 1000 | #{fzf "--multi --jump-labels 12345 --bind 'ctrl-j:jump'"}", :Enter1186+ tmux.until { |lines| lines[-2] == ' 1000/1000' }1187+ tmux.send_keys 'C-j'1188+ tmux.until { |lines| lines[-7] == '5 5' }1189+ tmux.until { |lines| lines[-8] == ' 6' }1190+ tmux.send_keys '5'1191+ tmux.until { |lines| lines[-7] == '> 5' }1192+ tmux.send_keys :Tab1193+ tmux.until { |lines| lines[-7] == ' >5' }1194+ tmux.send_keys 'C-j'1195+ tmux.until { |lines| lines[-7] == '5>5' }1196+ tmux.send_keys '2'1197+ tmux.until { |lines| lines[-4] == '> 2' }1198+ tmux.send_keys :Tab1199+ tmux.until { |lines| lines[-4] == ' >2' }1200+ tmux.send_keys 'C-j'1201+ tmux.until { |lines| lines[-7] == '5>5' }1202+1203+ # Press any key other than jump labels to cancel jump1204+ tmux.send_keys '6'1205+ tmux.until { |lines| lines[-3] == '> 1' }1206+ tmux.send_keys :Tab1207+ tmux.until { |lines| lines[-3] == '>>1' }1208+ tmux.send_keys :Enter1209+ assert_equal %w[5 2 1], readonce.split($/)1210+ end1211+1212+ def test_jump_accept1213+ tmux.send_keys "seq 1000 | #{fzf "--multi --jump-labels 12345 --bind 'ctrl-j:jump-accept'"}", :Enter1214+ tmux.until { |lines| lines[-2] == ' 1000/1000' }1215+ tmux.send_keys 'C-j'1216+ tmux.until { |lines| lines[-7] == '5 5' }1217+ tmux.send_keys '3'1218+ assert_equal '3', readonce.chomp1219+ end1220+1221 private1222 def writelines path, lines1223 File.unlink path while File.exists? path1224 File.open(path, 'w') { |f| f << lines.join($/) + $/ }335 unmodified lines