Skip to content
Myr

Add --track option to track the current selection

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

Close #3186 Related #1890

authored by Junegunn Choi · committed by Junegunn Choi · parent ae745d9 · Browse files · 6 files+81 added−1 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+81 added−1 removed

  • tests:+23 added−0 removed
  • other:+58 added−1 removed

6 files+81 added−1 removed

modified:man/man1/fzf.1+10 added
90 unmodified lines
91.TP92.B "+s, --no-sort"93Do not sort the result94.TP95.B "--track"96Make fzf track the current selection when the result list is updated.97This can be useful when browsing logs using fzf with sorting disabled.9899.RS100e.g.101 \fBgit log --oneline --graph --color=always | nl |102 fzf --ansi --track --no-sort --layout=reverse-list\fR103.RE104.TP105.B "--tac"106Reverse the order of the input107108.RS
1143 unmodified lines
modified:src/merger.go+16 added-1 removed
15 unmodified lines
16 sorted bool17 tac bool18 final bool19 count int20 pass bool21}2223// PassMerger returns a new Merger that simply returns the items in the24// original order
1 unmodified line
26 mg := Merger{27 pattern: nil,28 chunks: chunks,29 tac: tac,29 count: 0}30 count: 0,31 pass: true}3233 for _, chunk := range *mg.chunks {34 mg.count += chunk.count35 }
23 unmodified lines
59func (mg *Merger) Length() int {60 return mg.count61}6263// FindIndex returns the index of the item with the given item index64func (mg *Merger) FindIndex(itemIndex int32) int {65 if mg.pass {66 return int(itemIndex)67 }68 for i := 0; i < mg.count; i++ {69 if mg.Get(i).item.Index() == itemIndex {70 return i71 }72 }73 return -174}7576// Get returns the pointer to the Result object indexed by the given integer77func (mg *Merger) Get(idx int) Result {78 if mg.chunks != nil {79 if mg.tac {
56 unmodified lines
modified:src/options.go+7 added
31 unmodified lines
32 --with-nth=N[,..] Transform the presentation of each line using33 field index expressions34 -d, --delimiter=STR Field delimiter regex (default: AWK-style)35 +s, --no-sort Do not sort the result36 --track Track the current selection when the result is updated37 --tac Reverse the order of the input38 --disabled Do not perform search39 --tiebreak=CRI[,..] Comma-separated list of sort criteria to apply40 when the scores are tied [length|chunk|begin|end|index]
225 unmodified lines
266 Nth []Range267 WithNth []Range268 Delimiter Delimiter269 Sort int270 Track bool271 Tac bool272 Criteria []criterion273 Multi int274 Ansi bool
64 unmodified lines
339 Nth: make([]Range, 0),340 WithNth: make([]Range, 0),341 Delimiter: Delimiter{},342 Sort: 1000,343 Track: false,344 Tac: false,345 Criteria: []criterion{byScore, byLength},346 Multi: 0,347 Ansi: false,
1216 unmodified lines
1564 case "-s", "--sort":1565 opts.Sort = optionalNumeric(allArgs, &i, 1)1566 case "+s", "--no-sort":1567 opts.Sort = 01568 case "--track":1569 opts.Track = true1570 case "--no-track":1571 opts.Track = false1572 case "--tac":1573 opts.Tac = true1574 case "--no-tac":1575 opts.Tac = false
486 unmodified lines
modified:src/terminal.go+18 added
181 unmodified lines
182 input []rune183 multi int184 sort bool185 toggleSort bool186 track bool187 delimiter Delimiter188 expect map[tui.Event]string189 keymap map[tui.Event][]*action190 keymapOrg map[tui.Event][]*action
408 unmodified lines
599 input: input,600 multi: opts.Multi,601 sort: opts.Sort > 0,602 toggleSort: opts.ToggleSort,603 track: opts.Track,604 delimiter: opts.Delimiter,605 expect: opts.Expect,606 keymap: opts.Keymap,607 keymapOrg: keymapCopy,
297 unmodified lines
905906// UpdateList updates Merger to display the list907func (t *Terminal) UpdateList(merger *Merger, reset bool) {908 t.mutex.Lock()909 var prevIndex int32 = -1910 if !reset && t.track && t.merger.Length() > 0 {911 prevIndex = t.merger.Get(t.cy).item.Index()912 }913 t.progress = 100914 t.merger = merger915 if reset {916 t.selected = make(map[int32]selectedItem)
2 unmodified lines
919 if t.hasLoadActions && t.triggerLoad {920 t.triggerLoad = false921 t.eventChan <- tui.Load.AsEvent()922 }923 if prevIndex >= 0 {924 pos := t.cy - t.offset925 count := t.merger.Length()926 i := t.merger.FindIndex(prevIndex)927 if i >= 0 {928 t.cy = i929 t.offset = t.cy - pos930 } else if t.cy > count {931 // Try to keep the vertical position when the list shrinks932 t.cy = count - util.Min(count, t.maxItems()) + pos933 }934 }935 t.mutex.Unlock()936 t.reqBox.Set(reqInfo, nil)937 t.reqBox.Set(reqList, nil)938}
2680 unmodified lines
modified:test/test_go.rb+23 added
2677 unmodified lines
2678 >2679 OUTPUT2680 tmux.until { assert_block(expected, _1) }2681 end26822683 def test_track2684 tmux.send_keys "seq 1000 | #{FZF} --query 555 --track", :Enter2685 tmux.until do |lines|2686 assert_equal 1, lines.match_count2687 assert_includes lines, '> 555'2688 end2689 tmux.send_keys :BSpace2690 index = tmux.until do |lines|2691 assert_equal 28, lines.match_count2692 assert_includes lines, '> 555'2693 end.index('> 555')2694 tmux.send_keys :BSpace2695 tmux.until do |lines|2696 assert_equal 271, lines.match_count2697 assert_equal '> 555', lines[index]2698 end2699 tmux.send_keys :BSpace2700 tmux.until do |lines|2701 assert_equal 1000, lines.match_count2702 assert_equal '> 555', lines[index]2703 end2704 end2705end27062707module TestShell2708 def setup
481 unmodified lines
modified:CHANGELOG.md+7 added
1 unmodified line
2=========340.39.05------6- Added `--track` option that makes fzf track the current selection when the7 result list is updated. This can be useful when browsing logs using fzf with8 sorting disabled.9 ```sh10 git log --oneline --graph --color=always | nl |11 fzf --ansi --track --no-sort --layout=reverse-list12 ```13- If you use `--listen` option without a port number fzf will automatically14 allocate an available port and export it as `$FZF_PORT` environment15 variable.16 ```sh
1579 unmodified lines