Trigger focus event after reload (#4931)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
- Indexes restart after reload, so same-index item was not seen as new - With --id-nth, compare by key. No event if focused item keeps its key. - Skip focus check while track is blocked - Document that binding reload to focus can loop Close #4930
authored by Junegunn Choi · committed by GitHub · parent 33a3456 · Browse files · 4 files·+95 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+95 added−8 removed
- tests:+55 added−0 removed
- other:+40 added−8 removed
Provenance
- Commit
25adb67- Tree
643f5d4- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
33a3456
4 files changed+95 added−8 removed
modified:man/man1/fzf.1+4 added-1 removed
1907 unmodified lines1908 .RE1909 \fIfocus\fR1910 .RS1911 Triggered when the focus changes due to a vertical cursor movement or a search1912-result update.1912+result update. After a reload, the focused item is considered new unless1913+\fB\-\-id\-nth\fR is set and the item has the same key as the previously1914+focused item. Because of this, binding \fBreload\fR to this event can cause an1915+infinite loop.1916 1917 e.g.1918 \fBfzf \-\-bind 'focus:transform\-preview\-label:echo [ {} ]' \-\-preview 'cat {}'1919 506 unmodified linesmodified:src/terminal.go+33 added-7 removed
465 unmodified lines466 executing *util.AtomicBool467 termSize tui.TermSize468 lastAction actionType469 lastKey string470- lastFocus int32470+ lastFocus focusedItem471 areaLines int472 areaColumns int473 forcePreview bool474 clickHeaderLine int16 unmodified lines491 at time.Time492 item *Item493 }494 495+type focusedItem struct {496+ major int // revision.major; changes on reload497+ item *Item498+}499+500 type byTimeOrder []selectedItem501 502 func (a byTimeOrder) Len() int {503 return len(a)686 unmodified lines1190 ttyin: ttyin,1191 initFunc: func() error { return renderer.Init() },1192 executing: util.NewAtomicBool(false),1193 lastAction: actStart,1189- lastFocus: minItem.Index(),1194+ lastFocus: focusedItem{},1195 lastActivity: time.Now(),1196 numLinesCache: make(map[int32]numLinesCacheValue),1197 // The initial load counts as a search in progress ('start:wait').1198 // Set before the reader starts so the first final result clears it.4871 unmodified lines6070 }6071 return minItem.Index()6072 }6073 6074+func (t *Terminal) currentFocus() focusedItem {6075+ return focusedItem{t.revision.major, t.currentItem()}6076+}6077+6078+// Records the current focus and returns whether it differs from the last one.6079+// Indexes restart after a reload, so items are compared by --id-nth key, if6080+// set. Without it, any focused item after a reload is a new one.6081+func (t *Terminal) updateFocus() bool {6082+ curr := t.currentFocus()6083+ last := t.lastFocus6084+ t.lastFocus = curr6085+ if curr.item == nil || last.item == nil {6086+ return curr.item != last.item6087+ }6088+ if curr.major == last.major {6089+ return curr.item.Index() != last.item.Index()6090+ }6091+ return len(t.idNth) == 0 || t.trackKeyFor(curr.item, t.idNth) != t.trackKeyFor(last.item, t.idNth)6092+}6093+6094 func (t *Terminal) trackKeyFor(item *Item, nth []Range) string {6095 tokens := Tokenize(item.AsString(t.ansi), t.delimiter)6096 return StripLastDelimiter(JoinTokens(Transform(tokens, nth)), t.delimiter)6097 }557 unmodified lines6655 t.track = trackDisabled6656 info = true6657 }6658 focusChanged := focusedIndex != currentIndex6634- if (t.hasFocusActions || t.infoCommand != "") && focusChanged && currentIndex != t.lastFocus {6635- t.lastFocus = currentIndex6659+ if (t.hasFocusActions || t.infoCommand != "") && !t.trackBlocked && t.updateFocus() {6660 t.eventChan <- tui.Focus.AsEvent()6661 if t.infoCommand != "" {6662 info = true6663 }376 unmodified lines7040 break7041 }7042 }7043 7044+ // Actions are ignored while blocked. The render loop will7045+ // report the focus after the reload.7020- if onFocus, prs := t.keymap[tui.Focus.AsEvent()]; prs && iter < maxFocusEvents {7021- if newIndex := t.currentIndex(); newIndex != currentIndex {7022- t.lastFocus = newIndex7046+ if onFocus, prs := t.keymap[tui.Focus.AsEvent()]; prs && iter < maxFocusEvents && !t.trackBlocked {7047+ if t.currentIndex() != currentIndex {7048+ t.lastFocus = t.currentFocus()7049 if t.infoCommand != "" {7050 req(reqInfo)7051 }7052 actions = onFocus1824 unmodified linesmodified:test/test_core.rb+55 added
1530 unmodified lines1531 tmux.until { |lines| assert_equal 100, lines.match_count }1532 tmux.until { |lines| refute_includes(lines[-1], '[[1]]') }1533 end1534 1535+ def assert_focus_log(expected)1536+ wait do1537+ assert_path_exists tempname1538+ assert_equal expected, File.readlines(tempname, chomp: true)1539+ end1540+ end1541+1542+ def test_focus_event_after_reload1543+ # Without --id-nth, the item at the same index after reload is a new item1544+ tmux.send_keys %(printf 'a\\nb\\n' | #{FZF} --bind 'focus:execute-silent(echo {} >> #{tempname})' --bind 'ctrl-r:reload(printf "c\\nd\\n")'), :Enter1545+ tmux.until { |lines| assert_includes lines, '> a' }1546+ assert_focus_log(%w[a])1547+ tmux.send_keys 'C-r'1548+ tmux.until { |lines| assert_includes lines, '> c' }1549+ assert_focus_log(%w[a c])1550+ end1551+1552+ def test_focus_event_after_reload_id_nth_same_key1553+ # Same --id-nth key after reload: no focus event1554+ tmux.send_keys %(printf '1 a\\n2 b\\n' | #{FZF} --track --id-nth 1 --bind 'focus:execute-silent(echo {} >> #{tempname})' --bind 'ctrl-r:reload(printf "0 x\\n1 aa\\n2 bb\\n")'), :Enter1555+ tmux.until { |lines| assert_includes lines, '> 1 a' }1556+ assert_focus_log(['1 a'])1557+ tmux.send_keys 'C-r'1558+ tmux.until { |lines| assert_includes lines, '> 1 aa' }1559+ tmux.send_keys :Up1560+ tmux.until { |lines| assert_includes lines, '> 2 bb' }1561+ assert_focus_log(['1 a', '2 bb'])1562+ end1563+1564+ def test_focus_event_after_reload_id_nth_key_not_found1565+ tmux.send_keys %(printf '1 a\\n2 b\\n' | #{FZF} --track --id-nth 1 --bind 'focus:execute-silent(echo {} >> #{tempname})' --bind 'ctrl-r:reload(printf "3 c\\n4 d\\n")'), :Enter1566+ tmux.until { |lines| assert_includes lines, '> 1 a' }1567+ assert_focus_log(['1 a'])1568+ tmux.send_keys 'C-r'1569+ tmux.until { |lines| assert_includes lines, '> 3 c' }1570+ assert_focus_log(['1 a', '3 c'])1571+ end1572+1573+ def test_focus_event_after_async_reload_id_nth1574+ # No focus event for the items shown before the tracked item arrives.1575+ # 'result' binding makes fzf redraw the list while blocked.1576+ tmux.send_keys %(printf '1 a\\n2 b\\n' | #{FZF} --track --id-nth 1 --bind 'result:change-footer(R)' --bind 'focus:execute-silent(echo {} >> #{tempname})' --bind 'ctrl-r:reload(printf "0 x\\n"; sleep 1; printf "1 aa\\n2 bb\\n")'), :Enter1577+ tmux.until { |lines| assert_includes lines, '> 1 a' }1578+ assert_focus_log(['1 a'])1579+ tmux.send_keys 'C-r'1580+ tmux.until { |lines| assert_includes lines[-2], '+T*' }1581+ tmux.until do |lines|1582+ assert_includes lines, '> 1 aa'1583+ refute_includes lines[-2], '+T*'1584+ end1585+ tmux.send_keys :Up1586+ tmux.until { |lines| assert_includes lines, '> 2 bb' }1587+ assert_focus_log(['1 a', '2 bb'])1588+ end1589+1590 def test_result_event1591 tmux.send_keys '(echo 0; seq 10) | fzf --bind "result:pos(2)"', :Enter1592 tmux.until { |lines| assert_equal 11, lines.match_count }1593 tmux.until { |lines| assert_includes lines, '> 1' }1432 unmodified linesmodified:CHANGELOG.md+3 added
4 unmodified lines5 ------6 - Fixed `--gap-line` cutting a grapheme cluster when filling the last cells of the line (#4920)7 - Fixed an escape sequence fzf does not recognize being partly consumed, which leaked the rest into the query (#4926)8 - Fixed a key lost when it arrived in the same read as an escape sequence9+- Fixed `focus` event not triggered after `reload` when the new item is at the same position as the previous one (#4930)10+ - With `--id-nth`, the event is not triggered if the focus moves directly from an item to another item with the same key11+ - A `focus` binding that runs `reload` now triggers itself again after each reload, unless `--id-nth` is set and the key does not change12 13 0.74.414 ------15 - Fixed an escape sequence split across reads being parsed as a fragment, which leaked the rest into the query (#4899)3451 unmodified lines