Skip to content
Myr

Run bg-transform callbacks while wait/track-blocked

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

Background transform results are unrelated to the block; dropping them would silently lose completed work. Applies both the dedicated bg-transform-* setters and actions parsed from generic bg-transform output (inBgCallback); a 'wait' in such output joins the ongoing wait. Searches initiated by a bg result are not waited for.

authored by Junegunn Choi · committed by Junegunn Choi · parent 8ec443b · Browse files · 3 files+66 added−9 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+66 added−9 removed

  • tests:+33 added−0 removed
  • other:+33 added−9 removed

3 files+66 added−9 removed

modified:man/man1/fzf.1+8 added
2324 unmodified lines
2325default), which cancel the wait and discard the pending actions instead of2326performing their usual role. The remaining actions of such a binding still run,2327so a binding like \fBesc:cancel+first\fR is possible.23282329Asynchronous \fBbg\-transform\-*\fR actions are not affected; their results are2330applied as soon as they arrive, even while waiting. For the same reason,2331\fBwait\fR does not pair with them: in2332\fBbg\-transform\-query(...)+wait\fR, the background command completes only2333after \fBwait\fR has already been evaluated, so the search its result2334eventually triggers is not waited for. Use the synchronous2335\fBtransform\-query(...)\fR variant instead when chaining with \fBwait\fR.23362337If the search takes long enough, fzf indicates that it is waiting by dimming the2338input, hiding the cursor, and showing \fB(..)\fR on the info line. This visual2339feedback is debounced so that quick searches do not cause flickering.2340
56 unmodified lines
modified:src/terminal.go+25 added-9 removed
1898 unmodified lines
1899 if result.final() {1900 t.wait.searching = false1901 // If waiting, unblock so main loop can execute pending actions.1902 // Note: any final result unblocks the wait, not just the one for the1903 // search that armed it. Back-to-back searches (--listen, reload+wait)1904 // can therefore unblock early and run the pending actions on the1905 // previous result set. Accepted; a per-search generation token1906 // through the matcher isn't worth the complexity.1903 // search that armed it. Back-to-back searches (--listen, bg-transform1904 // callbacks, reload+wait) can therefore unblock early and run the1905 // pending actions on the previous result set. Accepted; a per-search1906 // generation token through the matcher isn't worth the complexity.1907 if t.wait.blocked {1908 t.unblockWait()1909 wakeUp = len(t.wait.pending) > 01910 }
3924 unmodified lines
5835 }5836}58375838// The wait state machine. Invariant: arming captures every action after5839// 'wait' at any nesting level into wait.pending; while blocked, actions5840// are dropped; abort/cancel discards everything.5839// 'wait' at any nesting level into wait.pending; while blocked, actions are5840// dropped unless they are results of work started before the block5841// (bg-transform callbacks); abort/cancel discards everything.58425843// blockWait blocks action execution and defers the given actions until the5844// current search completes (see UpdateList)5845func (t *Terminal) blockWait(pending []*action) {
722 unmodified lines
6568 var newHeaderLines *int6569 var newCommand *commandSpec6570 var reloadSync bool6571 var denylist []int326572 // True while running bg-transform callbacks. Declared outside the loop6573 // because callbacks invoke the doActions closure of the iteration that6574 // scheduled them, not the one executing actAsync.6575 inBgCallback := false6576 req := func(evts ...util.EventType) {6577 for _, event := range evts {6578 events = append(events, event)6579 if isTerminalEvent(event) {
149 unmodified lines
6729 for iter := 0; iter <= maxFocusEvents; iter++ {6730 currentIndex := t.currentIndex()6731 for i, action := range actions {6732 if action.t == actWait {6728 // Already waiting; ignore so input can't reset the blocked state6733 // Already waiting. Actions parsed from a bg-transform6734 // result join the current wait; user input can't reset6735 // the blocked state6736 if t.wait.blocked {6737 if inBgCallback {6738 t.wait.pending = append(t.wait.pending, actions[i+1:]...)6739 }6740 return true6741 }6742 // Block if search is in progress or will be triggered6743 if changed || newCommand != nil || t.wait.searching || queryBefore != string(t.input) {
57 unmodified lines
6801 // change-*6802 callback(a.a)6803 }6804 }6805 // Actions that run even while wait/track-blocked: bg-transform6806 // callbacks and their parsed actions (results of processes6807 // started before the block).6808 passthrough := inBgCallback || a.t == actAsync6809 // When wait-blocked, only allow abort/cancel6796 if t.wait.blocked {6810 if t.wait.blocked && !passthrough {6811 if a.t == actAbort || a.t == actCancel {6812 t.cancelWait()6813 req(reqPrompt, reqInfo)6814 }6815 return true6816 }6817 // When track-blocked, only allow abort/cancel and track-disabling actions6804 if t.trackBlocked && a.t != actToggleTrack && a.t != actToggleTrackCurrent && a.t != actUntrackCurrent {6818 if t.trackBlocked && !passthrough && a.t != actToggleTrack && a.t != actToggleTrackCurrent && a.t != actUntrackCurrent {6819 if a.t == actAbort || a.t == actCancel {6820 t.unblockTrack()6821 req(reqPrompt, reqInfo)6822 }
2 unmodified lines
6825 Action:6826 switch a.t {6827 case actIgnore, actStart, actClick:6828 case actAsync:6829 inBgCallback = true6830 for _, callback := range callbacks {6831 if t.bgVersion == callback.version {6832 callback.callback()6833 }6834 }6835 inBgCallback = false6836 case actBecome:6837 valid, list := t.buildPlusList(a.a, false)6838 if valid {6839 // We do not remove temp files in this case
1723 unmodified lines
modified:test/test_core.rb+33 added
1183 unmodified lines
1184 tmux.send_keys '99'1185 tmux.until { |lines| assert_equal 1, lines.match_count }1186 end11871188 def test_wait_action_bg_transform1189 # A bg-transform result is unrelated to the wait, so it's applied while the1190 # wait is still blocking rather than dropped. The long read keeps the wait1191 # blocked so the (instant) bg-transform completes during the block; the1192 # header must show while '(..)' is still displayed.1193 tmux.send_keys %((seq 100; sleep 2) | #{FZF} --bind 'start:bg-transform-header(echo hello)+search(5)+wait'), :Enter1194 tmux.until { |lines| assert(lines.any_include?('(..)') && lines.any_include?('hello')) }1195 end11961197 def test_wait_action_bg_transform_actions1198 # Actions parsed from a generic bg-transform result are also applied1199 # while wait-blocked1200 tmux.send_keys %((seq 100; sleep 2) | #{FZF} --bind 'start:bg-transform(echo change-header:hello)+search(5)+wait'), :Enter1201 tmux.until { |lines| assert(lines.any_include?('(..)') && lines.any_include?('hello')) }1202 end12031204 def test_wait_action_bg_transform_join1205 # A 'wait' in a bg-transform result body joins the ongoing wait; the1206 # actions after it run when the wait unblocks instead of being dropped1207 tmux.send_keys %((seq 100; sleep 2) | #{FZF} --bind 'start:bg-transform(echo change-header{hello}+wait+change-footer{world})+search(5)+wait'), :Enter1208 tmux.until { |lines| assert(lines.any_include?('(..)') && lines.any_include?('hello') && !lines.any_include?('world')) }1209 tmux.until { |lines| assert(lines.any_include?('world') && !lines.any_include?('(..)')) }1210 end12111212 def test_wait_action_query_change1213 # Query-editing actions must also make wait block; accept must run on the1214 # results of the new query, not the stale ones1215 tmux.send_keys %(seq 100 | #{fzf("--bind 'space:change-query(55)+wait+accept'")}), :Enter
46 unmodified lines
1262 tmux.send_keys 'C-t'1263 assert_equal %w[ctrl-t 1], fzf_output_lines1264 end12651266 def test_track_blocked_bg_transform1267 # A bg-transform result completing while track-blocked is applied, not1268 # dropped. The header must show while '+T*' is still displayed.1269 tmux.send_keys "seq 100 | #{FZF} --track --id-nth .. --bind 'ctrl-r:bg-transform-header(echo hello)+reload(sleep 2; seq 100)'", :Enter1270 tmux.until { |lines| assert_includes lines[-2], '+T' }1271 tmux.send_keys 'C-r'1272 tmux.until { |lines| assert(lines.any_include?('+T*') && lines.any_include?('hello')) }1273 end12741275 def test_clear_selection1276 tmux.send_keys %(seq 100 | #{FZF} --multi --bind space:clear-selection), :Enter1277 tmux.until { |lines| assert_equal 100, lines.match_count }1278 tmux.send_keys :Tab
1665 unmodified lines