Skip to content
Myr

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

authored by Junegunn Choi · committed by Junegunn Choi · parent 826178f · Browse files · 6 files+58 added−4 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+58 added−4 removed

  • tests:+9 added−0 removed
  • other:+49 added−4 removed

6 files+58 added−4 removed

modified:man/man1/fzf.1+18 added-2 removed
957 unmodified lines
958e.g.959 \fB# Move cursor to the first entry whenever the query is changed960 fzf --bind change:first\fR961.RE962\fIfocus\fR963.RS964Triggered when the focus changes due to a vertical cursor movement or a search965result update.966967e.g.968 \fBfzf --bind 'focus:transform-preview-label:echo [ {} ]' --border --preview 'cat {}'969970 # 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 {}'974975 # Beware not to introduce an infinite loop976 seq 10 | fzf --bind 'focus:up' --cycle\fR977.RE978979\fIbackward-eof\fR980.RS981Triggered when the query string is already empty and you try to delete it982backward.
15 unmodified lines
998 \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 lines
modified:src/tui/tui.go+1 added
90 unmodified lines
91 Change92 BackwardEOF93 Start94 Load95 Focus9697 AltBS9899 AltUp
614 unmodified lines
modified:src/options.go+2 added
607 unmodified lines
608 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(' ')] = key
1414 unmodified lines
modified:src/terminal.go+14 added-2 removed
1263 unmodified lines
1264 }1265}12661267func (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 }12711272 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 lines
2618 t.previewBox.Set(reqPreviewEnqueue, previewRequest{command, t.pwindow, t.evaluateScrollOffset(), list})2619 }2620 }26212622 var onFocus []*action2623 if actions, prs := t.keymap[tui.Focus.AsEvent()]; prs {2624 onFocus = actions2625 }26262627 go func() {2628 var focusedIndex int32 = minItem.Index()2629 var version int64 = -12630 running := true
27 unmodified lines
2658 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 lines
modified:test/test_go.rb+9 added
2471 unmodified lines
2472 assert_includes(lines[-2], '100/100 (100)')2473 end2474 end24752476 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 end24842485 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 lines
modified:CHANGELOG.md+14 added
1 unmodified line
2=========340.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 {}'1112 # 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 {}'1617 # 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