Add 'focus' event
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Can we find a better name? I have considered the followings. * 'point', because "the pointer" points to the current item. * 'shift', 'switch', 'move', etc. These are not technically correct because the current item can change without cursor movement (--tac, reload, search update) * 'change' is already taken. 'change-current' feels a bit wordy and sounds wrong, 'current-changed' is wordy and doesn't go well with the other event names * 'target', not straightforward Close #3053
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+58 added−4 removed
- tests:+9 added−0 removed
- other:+49 added−4 removed
Provenance
- Commit
284d77f- Tree
0dbac33- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
826178f
6 files changed+58 added−4 removed
modified:man/man1/fzf.1+18 added-2 removed
957 unmodified lines958 e.g.959 \fB# Move cursor to the first entry whenever the query is changed960 fzf --bind change:first\fR961 .RE962+\fIfocus\fR963+.RS964+Triggered when the focus changes due to a vertical cursor movement or a search965+result update.966 967+e.g.968+ \fBfzf --bind 'focus:transform-preview-label:echo [ {} ]' --border --preview 'cat {}'969+970+ # Any action bound to the event runs synchronously and thus can make the interface sluggish971+ # e.g. lolcat isn't one of the fastest programs, and every cursor movement in972+ # fzf will be noticeably affected by its execution time973+ fzf --bind 'focus:transform-preview-label:echo [ {} ] | lolcat -f' --border --preview 'cat {}'974+975+ # Beware not to introduce an infinite loop976+ seq 10 | fzf --bind 'focus:up' --cycle\fR977+.RE978+979 \fIbackward-eof\fR980 .RS981 Triggered when the query string is already empty and you try to delete it982 backward.15 unmodified lines998 \fBbackward-kill-word\fR \fIalt-bs\fR999 \fBbackward-word\fR \fIalt-b shift-left\fR1000 \fBbeginning-of-line\fR \fIctrl-a home\fR1001 \fBcancel\fR (clear query string if not empty, abort fzf otherwise)1002+ \fBchange-border-label(...)\fR (change \fB--border-label\fR to the given string)1003 \fBchange-preview(...)\fR (change \fB--preview\fR option)987- \fBchange-preview-window(...)\fR (change \fB--preview-window\fR option; rotate through the multiple option sets separated by '|')1004 \fBchange-preview-label(...)\fR (change \fB--preview-label\fR to the given string)1005+ \fBchange-preview-window(...)\fR (change \fB--preview-window\fR option; rotate through the multiple option sets separated by '|')1006 \fBchange-prompt(...)\fR (change prompt to the given string)990- \fBchange-border-label(...)\fR (change \fB--border-label\fR to the given string)1007 \fBchange-query(...)\fR (change query string to the given string)1008 \fBclear-screen\fR \fIctrl-l\fR1009 \fBclear-selection\fR (clear multi-selection)1010 \fBclose\fR (close preview window if open, abort fzf otherwise)212 unmodified linesmodified:src/tui/tui.go+1 added
90 unmodified lines91 Change92 BackwardEOF93 Start94 Load95+ Focus96 97 AltBS98 99 AltUp614 unmodified linesmodified:src/options.go+2 added
607 unmodified lines608 case "start":609 add(tui.Start)610 case "load":611 add(tui.Load)612+ case "focus":613+ add(tui.Focus)614 case "alt-enter", "alt-return":615 chords[tui.CtrlAltKey('m')] = key616 case "alt-space":617 chords[tui.AltKey(' ')] = key1414 unmodified linesmodified:src/terminal.go+14 added-2 removed
1263 unmodified lines1264 }1265 }1266 1267 func (t *Terminal) printLabel(window tui.Window, render labelPrinter, opts labelOpts, length int, borderShape tui.BorderShape, redrawBorder bool) {1268- if window == nil || render == nil {1268+ if window == nil {1269 return1270 }1271 1272 switch borderShape {1273 case tui.BorderHorizontal, tui.BorderTop, tui.BorderBottom, tui.BorderRounded, tui.BorderSharp, tui.BorderBold, tui.BorderDouble:1274 if redrawBorder {1275 window.DrawHBorder()1276 }1277+ if render == nil {1278+ return1279+ }1280 var col int1281 if opts.column == 0 {1282 col = util.Max(0, (window.Width()-length)/2)1283 } else if opts.column < 0 {1334 unmodified lines2618 t.previewBox.Set(reqPreviewEnqueue, previewRequest{command, t.pwindow, t.evaluateScrollOffset(), list})2619 }2620 }2621 2622+ var onFocus []*action2623+ if actions, prs := t.keymap[tui.Focus.AsEvent()]; prs {2624+ onFocus = actions2625+ }2626+2627 go func() {2628 var focusedIndex int32 = minItem.Index()2629 var version int64 = -12630 running := true27 unmodified lines2658 currentItem := t.currentItem()2659 if currentItem != nil {2660 currentIndex = currentItem.Index()2661 }2662+ focusChanged := focusedIndex != currentIndex2663+ if onFocus != nil && focusChanged {2664+ t.serverChan <- onFocus2665+ }2654- if focusedIndex != currentIndex || version != t.version {2666+ if focusChanged || version != t.version {2667 version = t.version2668 focusedIndex = currentIndex2669 refreshPreview(t.previewOpts.command)2670 }889 unmodified linesmodified:test/test_go.rb+9 added
2471 unmodified lines2472 assert_includes(lines[-2], '100/100 (100)')2473 end2474 end2475 2476+ def test_focus_event2477+ tmux.send_keys 'seq 100 | fzf --bind "focus:transform-prompt(echo [[{}]])"', :Enter2478+ tmux.until { |lines| assert_includes(lines[-1], '[[1]]') }2479+ tmux.send_keys :Up2480+ tmux.until { |lines| assert_includes(lines[-1], '[[2]]') }2481+ tmux.send_keys :X2482+ tmux.until { |lines| assert_includes(lines[-1], '[[]]') }2483+ end2484+2485 def test_labels_center2486 tmux.send_keys 'echo x | fzf --border --border-label foobar --preview : --preview-label barfoo --bind "space:change-border-label(foobarfoo)+change-preview-label(barfoobar),enter:transform-border-label(echo foo{}foo)+transform-preview-label(echo bar{}bar)"', :Enter2487 tmux.until do2488 assert_includes(_1[0], '─foobar─')582 unmodified linesmodified:CHANGELOG.md+14 added
1 unmodified line2 =========3 4 0.37.05 ------6+- New event7+ - `focus` - Triggered when the focus changes due to a vertical cursor8+ movement or a search result update9+ ```sh10+ fzf --bind 'focus:transform-preview-label:echo [ {} ]' --border --preview 'cat {}'11+12+ # Any action bound to the event runs synchronously and thus can make the interface sluggish13+ # e.g. lolcat isn't one of the fastest programs, and every cursor movement in14+ # fzf will be noticeably affected by its execution time15+ fzf --bind 'focus:transform-preview-label:echo [ {} ] | lolcat -f' --border --preview 'cat {}'16+17+ # Beware not to introduce an infinite loop18+ seq 10 | fzf --bind 'focus:up' --cycle19+ ```20 - New actions21 - `change-border-label`22 - `change-preview-label`23 - `transform-border-label`1515 unmodified lines