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
Provenance
- Commit
1c7534f- Tree
58ec99c- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
ae745d9
6 files changed+81 added−1 removed
modified:man/man1/fzf.1+10 added
90 unmodified lines91 .TP92 .B "+s, --no-sort"93 Do not sort the result94 .TP95+.B "--track"96+Make fzf track the current selection when the result list is updated.97+This can be useful when browsing logs using fzf with sorting disabled.98+99+.RS100+e.g.101+ \fBgit log --oneline --graph --color=always | nl |102+ fzf --ansi --track --no-sort --layout=reverse-list\fR103+.RE104+.TP105 .B "--tac"106 Reverse the order of the input107 108 .RS1143 unmodified linesmodified:src/merger.go+16 added-1 removed
15 unmodified lines16 sorted bool17 tac bool18 final bool19 count int20+ pass bool21 }22 23 // PassMerger returns a new Merger that simply returns the items in the24 // original order1 unmodified line26 mg := Merger{27 pattern: nil,28 chunks: chunks,29 tac: tac,29- count: 0}30+ count: 0,31+ pass: true}32 33 for _, chunk := range *mg.chunks {34 mg.count += chunk.count35 }23 unmodified lines59 func (mg *Merger) Length() int {60 return mg.count61 }62 63+// FindIndex returns the index of the item with the given item index64+func (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+}75+76 // Get returns the pointer to the Result object indexed by the given integer77 func (mg *Merger) Get(idx int) Result {78 if mg.chunks != nil {79 if mg.tac {56 unmodified linesmodified:src/options.go+7 added
31 unmodified lines32 --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 lines266 Nth []Range267 WithNth []Range268 Delimiter Delimiter269 Sort int270+ Track bool271 Tac bool272 Criteria []criterion273 Multi int274 Ansi bool64 unmodified lines339 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 lines1564 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 = false486 unmodified linesmodified:src/terminal.go+18 added
181 unmodified lines182 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][]*action408 unmodified lines599 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 lines905 906 // UpdateList updates Merger to display the list907 func (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 lines919 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 linesmodified:test/test_go.rb+23 added
2677 unmodified lines2678 >2679 OUTPUT2680 tmux.until { assert_block(expected, _1) }2681 end2682+2683+ 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+ end2705 end2706 2707 module TestShell2708 def setup481 unmodified linesmodified:CHANGELOG.md+7 added
1 unmodified line2 =========3 4 0.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 ```sh1579 unmodified lines