From 168829b5550336886a426073670153f84f8a34b2 Mon Sep 17 00:00:00 2001 From: Junegunn Choi Date: Thu, 27 Oct 2022 00:33:05 +0900 Subject: [PATCH] Add 'start' event that is triggered once when fzf finder starts Close #1622 --- CHANGELOG.md | 9 +++++++++ man/man1/fzf.1 | 11 ++++++++++- src/options.go | 2 ++ src/terminal.go | 10 +++++++++- src/tui/tui.go | 1 + test/test_go.rb | 8 ++++++++ 6 files changed, 39 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 5036d2ba562..50a64c75ed2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,15 @@ CHANGELOG ========= +0.35.0 +------ +- Added `start` event that is triggered only once when fzf finder starts. + Since fzf consumes the input stream asynchronously, the input list is not + available unless you use `--sync`. + ```sh + seq 100 | fzf --multi --sync --bind 'start:last+select-all+preview(echo welcome)' + ``` + 0.34.0 ------ - Added support for adaptive `--height`. If the `--height` value is prefixed diff --git a/man/man1/fzf.1 b/man/man1/fzf.1 index bf71e7cdd27..01a1672dc81 100644 --- a/man/man1/fzf.1 +++ b/man/man1/fzf.1 @@ -21,7 +21,7 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. .. -.TH fzf 1 "Sep 2022" "fzf 0.34.0" "fzf - a command-line fuzzy finder" +.TH fzf 1 "Oct 2022" "fzf 0.35.0" "fzf - a command-line fuzzy finder" .SH NAME fzf - a command-line fuzzy finder @@ -811,6 +811,15 @@ e.g. or any single character .SS AVAILABLE EVENTS: +\fIstart\fR +.RS +Triggered only once when fzf finder starts. Since fzf consumes the input stream +asynchronously, the input list is not available unless you use \fI--sync\fR. + +e.g. + \fB# Move cursor to the last item and select all items + seq 1000 | fzf --multi --sync --bind start:last+select-all\fR +.RE \fIchange\fR .RS Triggered whenever the query string is changed diff --git a/src/options.go b/src/options.go index e17efc9619c..acf67281a4e 100644 --- a/src/options.go +++ b/src/options.go @@ -543,6 +543,8 @@ func parseKeyChords(str string, message string) map[tui.Event]string { add(tui.Change) case "backward-eof": add(tui.BackwardEOF) + case "start": + add(tui.Start) case "alt-enter", "alt-return": chords[tui.CtrlAltKey('m')] = key case "alt-space": diff --git a/src/terminal.go b/src/terminal.go index 493c8b9f8af..369bc4189f3 100644 --- a/src/terminal.go +++ b/src/terminal.go @@ -2362,13 +2362,21 @@ func (t *Terminal) Loop() { }() looping := true + _, startEvent := t.keymap[tui.Start.AsEvent()] + for looping { var newCommand *string changed := false beof := false queryChanged := false - event := t.tui.GetChar() + var event tui.Event + if startEvent { + event = tui.Start.AsEvent() + startEvent = false + } else { + event = t.tui.GetChar() + } t.mutex.Lock() previousInput := t.input diff --git a/src/tui/tui.go b/src/tui/tui.go index c6d71c12f60..90c5327eb40 100644 --- a/src/tui/tui.go +++ b/src/tui/tui.go @@ -90,6 +90,7 @@ const ( Change BackwardEOF + Start AltBS diff --git a/test/test_go.rb b/test/test_go.rb index ab5a4ae66ed..4b82a31eba8 100755 --- a/test/test_go.rb +++ b/test/test_go.rb @@ -2332,6 +2332,14 @@ def test_height_range_overflow OUTPUT tmux.until { assert_block(expected, _1) } end + + def test_start_event + tmux.send_keys 'seq 100 | fzf --multi --sync --preview-window border-none --bind "start:select-all+last+preview(echo welcome)"', :Enter + tmux.until do |lines| + assert_match(/>100.*welcome/, lines[0]) + assert_includes(lines[-2], '100/100 (100)') + end + end end module TestShell