Add reload-sync action
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Close #2816
authored by Junegunn Choi · committed by Junegunn Choi · parent 14775aa · Browse files · 6 files·+124 added−66 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+124 added−66 removed
- tests:+9 added−0 removed
- other:+115 added−66 removed
Provenance
- Commit
6c37177- Tree
91c818e- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
14775aa
6 files changed+124 added−66 removed
modified:man/man1/fzf.1+11 added
1017 unmodified lines1018 \fBput(...)\fR (put the given string to the prompt)1019 \fBrefresh-preview\fR1020 \fBrebind(...)\fR (rebind bindings after \fBunbind\fR)1021 \fBreload(...)\fR (see below for the details)1022+ \fBreload-sync(...)\fR (see below for the details)1023 \fBreplace-query\fR (replace query string with the current selection)1024 \fBselect\fR1025 \fBselect-all\fR (select all matches)1026 \fBtoggle\fR (\fIright-click\fR)95 unmodified lines1122 FZF_DEFAULT_COMMAND="$RG_PREFIX '$INITIAL_QUERY'" \\1123 fzf --bind "change:reload:$RG_PREFIX {q} || true" \\1124 --ansi --disabled --query "$INITIAL_QUERY"\fR1125 1126+\fBreload-sync(...)\fR is a synchronous version of \fBreload\fR that replaces1127+the list only when the command is complete. This is useful when the command1128+takes a while to produce the initial output and you don't want fzf to run1129+against an empty list while the command is running.1130+1131+1132+e.g.1133+ \fB# You can still filter and select entries from the initial list for 3 seconds1134+ seq 100 | fzf --bind 'load:reload-sync(sleep 3; seq 1000)+unbind(load)'\fR1135+1136 .SS PREVIEW BINDING1137 1138 With \fBpreview(...)\fR action, you can specify multiple different preview1139 commands in addition to the default preview command given by \fB--preview\fR47 unmodified linesmodified:src/core.go+32 added-11 removed
211 unmodified lines212 clearCache := util.Once(false)213 clearSelection := util.Once(false)214 ticks := 0215 var nextCommand *string216- restart := func(command string) {217- reading = true218- clearCache = util.Once(true)219- clearSelection = util.Once(true)220- chunkList.Clear()221- itemIndex = 0222- header = make([]string, 0, opts.HeaderLines)223- go reader.restart(command)224- }216 eventBox.Watch(EvtReadNew)217 total := 0218 query := []rune{}219 determine := func(final bool) {6 unmodified lines226 deferred = false227 terminal.startChan <- fitpad{-1, -1}228 }229 }230+231+ useSnapshot := false232+ var snapshot []*Chunk233+ var prevSnapshot []*Chunk234+ var count int235+ restart := func(command string) {236+ reading = true237+ clearCache = util.Once(true)238+ clearSelection = util.Once(true)239+ // We should not update snapshot if reload is triggered again while240+ // the previous reload is in progress241+ if useSnapshot && prevSnapshot != nil {242+ snapshot, count = chunkList.Snapshot()243+ }244+ chunkList.Clear()245+ itemIndex = 0246+ header = make([]string, 0, opts.HeaderLines)247+ go reader.restart(command)248+ }249 for {250 delay := true251 ticks++252 input := func(reloaded bool) []rune {23 unmodified lines276 break277 } else {278 reading = reading && evt == EvtReadNew279 }280+ if useSnapshot && evt == EvtReadFin {281+ useSnapshot = false282+ prevSnapshot = nil283+ }284+ if !useSnapshot {270- snapshot, count := chunkList.Snapshot()285+ snapshot, count = chunkList.Snapshot()286+ }287 total = count288 terminal.UpdateCount(total, !reading, value.(*string))289 if opts.Sync {290 opts.Sync = false10 unmodified lines301 switch val := value.(type) {302 case searchRequest:303 sort = val.sort304 command = val.command305+ if command != nil {306+ useSnapshot = val.sync307+ }308 }309 if command != nil {310 if reading {311 reader.terminate()2 unmodified lines314 restart(*command)315 }316 break317 }318+ if !useSnapshot {299- snapshot, _ := chunkList.Snapshot()319+ snapshot, _ = chunkList.Snapshot()320+ }321 reset := clearCache()322 matcher.Reset(snapshot, input(reset), true, !reading, sort, reset)323 delay = false324 51 unmodified linesmodified:src/options.go+3 added-1 removed
890 unmodified lines891 )892 893 func init() {894 executeRegexp = regexp.MustCompile(895- `(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-query|change-prompt|change-preview-window|change-preview|(?:re|un)bind|pos|put|transform-query)`)895+ `(?si)[:+](execute(?:-multi|-silent)?|reload(?:-sync)?|preview|change-query|change-prompt|change-preview-window|change-preview|(?:re|un)bind|pos|put|transform-query)`)896 splitRegexp = regexp.MustCompile("[,:]+")897 actionNameRegexp = regexp.MustCompile("(?i)^[a-z-]+")898 }899 284 unmodified lines1184 prefix := actionNameRegexp.FindString(str)1185 switch prefix {1186 case "reload":1187 return actReload1188+ case "reload-sync":1189+ return actReloadSync1190 case "unbind":1191 return actUnbind1192 case "rebind":1193 return actRebind787 unmodified linesmodified:src/terminal.go+6 added-2 removed
333 unmodified lines334 actSigStop335 actFirst336 actLast337 actReload338+ actReloadSync339 actDisableSearch340 actEnableSearch341 actSelect342 actDeselect10 unmodified lines353 }354 355 type searchRequest struct {356 sort bool357+ sync bool358 command *string359 }360 361 type previewRequest struct {2179 unmodified lines2541 }2542 }()2543 for looping {2544 var newCommand *string2545+ var reloadSync bool2546 changed := false2547 beof := false2548 queryChanged := false2549 482 unmodified lines3032 return doActions(actionsFor(tui.RightClick))3033 }3034 }3035 }3033- case actReload:3036+ case actReload, actReloadSync:3037 t.failed = nil3038 3039 valid, list := t.buildPlusList(a.a, false)3040 if !valid {5 unmodified lines3046 }3047 if valid {3048 command := t.replacePlaceholder(a.a, false, string(t.input), list)3049 newCommand = &command3050+ reloadSync = a.t == actReloadSync3051 t.reading = true3052 }3053 case actUnbind:3054 keys := parseKeyChords(a.a, "PANIC")121 unmodified lines3176 3177 t.mutex.Unlock() // Must be unlocked before touching reqBox3178 3179 if changed || newCommand != nil {3176- t.eventBox.Set(EvtSearchNew, searchRequest{sort: t.sort, command: newCommand})3180+ t.eventBox.Set(EvtSearchNew, searchRequest{sort: t.sort, sync: reloadSync, command: newCommand})3181 }3182 for _, event := range events {3183 t.reqBox.Set(event, nil)3184 }65 unmodified linesmodified:test/test_go.rb+9 added
2159 unmodified lines2160 tmux.until { |lines| assert_equal 1, lines.item_count }2161 tmux.until { |lines| assert_includes lines[1], '4' }2162 end2163 2164+ def test_reload_sync2165+ tmux.send_keys "seq 100 | #{FZF} --bind 'load:reload-sync(sleep 1; seq 1000)+unbind(load)'", :Enter2166+ tmux.until { |lines| assert_equal 100, lines.item_count }2167+ tmux.send_keys '00'2168+ tmux.until { |lines| assert_equal 1, lines.match_count }2169+ # After 1 second2170+ tmux.until { |lines| assert_equal 10, lines.match_count }2171+ end2172+2173 def test_scroll_off2174 tmux.send_keys "seq 1000 | #{FZF} --scroll-off=3 --bind l:last", :Enter2175 tmux.until { |lines| assert_equal 1000, lines.item_count }2176 height = tmux.until { |lines| lines }.first.to_i795 unmodified linesmodified:CHANGELOG.md+63 added-52 removed
10 unmodified lines11 12 # Send actions to the server13 curl -XPOST localhost:6266 -d 'reload(seq 100)+change-prompt(hundred> )'14 ```15-- Added `pos(...)` action to move the cursor to the numeric position16- - `first` and `last` are equivalent to `pos(1)` and `pos(-1)` respectively17- ```sh18- # Put the cursor on the 10th item19- seq 100 | fzf --sync --bind 'start:pos(10)'15+- New event16+ - Added `load` event that is triggered when the input stream is complete17+ and the initial processing of the list is complete.18+ ```sh19+ # Change the prompt to "loaded" when the input stream is complete20+ (seq 10; sleep 1; seq 11 20) | fzf --prompt 'Loading> ' --bind 'load:change-prompt:Loaded> '21 21- # Put the cursor on the 10th to last item22- seq 100 | fzf --sync --bind 'start:pos(-10)'23- ```24-- Added `load` event that is triggered when the input stream is complete and25- the initial processing of the list is complete.26- ```sh27- # Change the prompt to "loaded" when the input stream is complete28- (seq 10; sleep 1; seq 11 20) | fzf --prompt 'Loading> ' --bind 'load:change-prompt:Loaded> '22+ # You can use it instead of 'start' event without `--sync` if asynchronous23+ # trigger is not an issue.24+ (seq 10; sleep 1; seq 11 20) | fzf --bind 'load:last'25+ ```26+- New actions27+ - Added `pos(...)` action to move the cursor to the numeric position28+ - `first` and `last` are equivalent to `pos(1)` and `pos(-1)` respectively29+ ```sh30+ # Put the cursor on the 10th item31+ seq 100 | fzf --sync --bind 'start:pos(10)'32 33+ # Put the cursor on the 10th to last item34+ seq 100 | fzf --sync --bind 'start:pos(-10)'35+ ```36+ - Added `reload-sync(...)` action which replaces the current list only after37+ the reload process is complete. This is useful when the command takes38+ a while to produce the initial output and you don't want fzf to run against39+ an empty list while the command is running.30- # You can use it instead of 'start' event without `--sync` if asynchronous31- # trigger is not an issue.32- (seq 10; sleep 1; seq 11 20) | fzf --bind 'load:last'33- ```34-- Added `next-selected` and `prev-selected` actions to move between selected35- items36- ```sh37- # `next-selected` will move the pointer to the next selected item below the current line38- # `prev-selected` will move the pointer to the previous selected item above the current line39- seq 10 | fzf --multi --bind ctrl-n:next-selected,ctrl-p:prev-selected40+ ```sh41+ # You can still filter and select entries from the initial list for 3 seconds42+ seq 100 | fzf --bind 'load:reload-sync(sleep 3; seq 1000)+unbind(load)'43+ ```44+ - Added `next-selected` and `prev-selected` actions to move between selected45+ items46+ ```sh47+ # `next-selected` will move the pointer to the next selected item below the current line48+ # `prev-selected` will move the pointer to the previous selected item above the current line49+ seq 10 | fzf --multi --bind ctrl-n:next-selected,ctrl-p:prev-selected50 41- # Both actions respect --layout option42- seq 10 | fzf --multi --bind ctrl-n:next-selected,ctrl-p:prev-selected --layout reverse43- ```44-- Added `change-query(...)` action that simply changes the query string to the45- given static string. This can be useful when used with `--listen`.46- ```sh47- curl localhost:6266 -d "change-query:$(date)"48- ```49-- Added `transform-query(...)` action for transforming the query string using50- an external command51- ```sh52- # Press space to convert the query to uppercase letters53- fzf --bind 'space:transform-query(tr [:lower:] [:upper:] <<< {q})'51+ # Both actions respect --layout option52+ seq 10 | fzf --multi --bind ctrl-n:next-selected,ctrl-p:prev-selected --layout reverse53+ ```54+ - Added `change-query(...)` action that simply changes the query string to the55+ given static string. This can be useful when used with `--listen`.56+ ```sh57+ curl localhost:6266 -d "change-query:$(date)"58+ ```59+ - Added `transform-query(...)` action for transforming the query string using60+ an external command61+ ```sh62+ # Press space to convert the query to uppercase letters63+ fzf --bind 'space:transform-query(tr [:lower:] [:upper:] <<< {q})'64 55- # Bind it to 'change' event for automatic conversion56- fzf --bind 'change:transform-query(tr [:lower:] [:upper:] <<< {q})'65+ # Bind it to 'change' event for automatic conversion66+ fzf --bind 'change:transform-query(tr [:lower:] [:upper:] <<< {q})'67 68+ # Can only type numbers58- # Can only type numbers59- fzf --bind 'change:transform-query(sed 's/[^0-9]//g' <<< {q})'60- ```61-- `put` action can optionally take an argument string62- ```sh63- # a will put 'alpha' on the prompt, ctrl-b will put 'bravo'64- fzf --bind 'a:put+put(lpha),ctrl-b:put(bravo)'65- ```66-- `double-click` will behave the same as `enter` unless otherwise specified,67- so you don't have to repeat the same action twice in `--bind` in most cases.68- ```sh69- # No need to bind 'double-click' to the same action70- fzf --bind 'enter:execute:less {}' # --bind 'double-click:execute:less {}'71- ```69+ fzf --bind 'change:transform-query(sed 's/[^0-9]//g' <<< {q})'70+ ```71+- Improvements72+ - `put` action can optionally take an argument string73+ ```sh74+ # a will put 'alpha' on the prompt, ctrl-b will put 'bravo'75+ fzf --bind 'a:put+put(lpha),ctrl-b:put(bravo)'76+ ```77+ - `double-click` will behave the same as `enter` unless otherwise specified,78+ so you don't have to repeat the same action twice in `--bind` in most cases.79+ ```sh80+ # No need to bind 'double-click' to the same action81+ fzf --bind 'enter:execute:less {}' # --bind 'double-click:execute:less {}'82+ ```83 - Added color name `preview-label` for `--preview-label` (defaults to `label`84 for `--border-label`)85 - Minor bug fixes and improvements86 1389 unmodified lines