Fixed --track when used with --tac

Fix #3234
This commit is contained in:
Junegunn Choi 2023-04-22 15:06:22 +09:00
parent 44cfc7e62a
commit 7c6f5dba63
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
5 changed files with 30 additions and 14 deletions

View File

@ -3,7 +3,9 @@ CHANGELOG
0.39.1 0.39.1
------ ------
- Disallow using `--track` with `--tac` as the result can be very confusing - Fixed `--track` behavior when used with `--tac`
- However, using `--track` with `--tac` is not recommended. The resulting
behavior can be very confusing.
- Bug fixes and improvements - Bug fixes and improvements
0.39.0 0.39.0

View File

@ -95,7 +95,6 @@ Do not sort the result
.B "--track" .B "--track"
Make fzf track the current selection when the result list is updated. Make fzf track the current selection when the result list is updated.
This can be useful when browsing logs using fzf with sorting disabled. This can be useful when browsing logs using fzf with sorting disabled.
This option is not compatible with \fB--tac\fR.
.RS .RS
e.g. e.g.

View File

@ -60,17 +60,30 @@ func (mg *Merger) Length() int {
return mg.count return mg.count
} }
func (mg *Merger) First() Result {
if mg.tac && !mg.sorted {
return mg.Get(mg.count - 1)
}
return mg.Get(0)
}
// FindIndex returns the index of the item with the given item index // FindIndex returns the index of the item with the given item index
func (mg *Merger) FindIndex(itemIndex int32) int { func (mg *Merger) FindIndex(itemIndex int32) int {
index := -1
if mg.pass { if mg.pass {
return int(itemIndex) index = int(itemIndex)
} if mg.tac {
for i := 0; i < mg.count; i++ { index = mg.count - index - 1
if mg.Get(i).item.Index() == itemIndex { }
return i } else {
for i := 0; i < mg.count; i++ {
if mg.Get(i).item.Index() == itemIndex {
index = i
break
}
} }
} }
return -1 return index
} }
// Get returns the pointer to the Result object indexed by the given integer // Get returns the pointer to the Result object indexed by the given integer

View File

@ -1083,6 +1083,8 @@ func parseActionList(masked string, original string, prevActions []*action, putA
appendAction(actToggleAll) appendAction(actToggleAll)
case "toggle-search": case "toggle-search":
appendAction(actToggleSearch) appendAction(actToggleSearch)
case "toggle-track":
appendAction(actToggleTrack)
case "select": case "select":
appendAction(actSelect) appendAction(actSelect)
case "select-all": case "select-all":
@ -1938,10 +1940,6 @@ func postProcessOptions(opts *Options) {
errorExit("scrollbar display width should be 1") errorExit("scrollbar display width should be 1")
} }
if opts.Track && opts.Tac {
errorExit("--track cannot be used with --tac")
}
// Default actions for CTRL-N / CTRL-P when --history is set // Default actions for CTRL-N / CTRL-P when --history is set
if opts.History != nil { if opts.History != nil {
if _, prs := opts.Keymap[tui.CtrlP.AsEvent()]; !prs { if _, prs := opts.Keymap[tui.CtrlP.AsEvent()]; !prs {

View File

@ -907,8 +907,12 @@ func (t *Terminal) UpdateProgress(progress float32) {
func (t *Terminal) UpdateList(merger *Merger, reset bool) { func (t *Terminal) UpdateList(merger *Merger, reset bool) {
t.mutex.Lock() t.mutex.Lock()
var prevIndex int32 = -1 var prevIndex int32 = -1
if !reset && t.track && t.merger.Length() > 0 { if !reset && t.track {
prevIndex = t.merger.Get(t.cy).item.Index() if t.merger.Length() > 0 {
prevIndex = t.merger.Get(t.cy).item.Index()
} else if merger.Length() > 0 {
prevIndex = merger.First().item.Index()
}
} }
t.progress = 100 t.progress = 100
t.merger = merger t.merger = merger