m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-08 11:23:47 -05:00

Go to the closest match when disabling raw mode

This commit is contained in:
Junegunn Choi
2025-10-08 01:40:05 +09:00
parent 9ace1351ff
commit ed7becfb47
2 changed files with 53 additions and 16 deletions

View File

@@ -300,7 +300,6 @@ type Terminal struct {
subWordNext string subWordNext string
cx int cx int
cy int cy int
lastMatchingIndex int32
offset int offset int
xoffset int xoffset int
yanked []rune yanked []rune
@@ -996,7 +995,6 @@ func NewTerminal(opts *Options, eventBox *util.EventBox, executor *util.Executor
subWordNext: subWordNext, subWordNext: subWordNext,
cx: len(input), cx: len(input),
cy: 0, cy: 0,
lastMatchingIndex: minItem.Index(),
offset: 0, offset: 0,
xoffset: 0, xoffset: 0,
yanked: []rune{}, yanked: []rune{},
@@ -3225,9 +3223,6 @@ func (t *Terminal) printList() {
if itemCount < count { if itemCount < count {
item := t.merger.Get(itemCount + t.offset) item := t.merger.Get(itemCount + t.offset)
current := itemCount == t.cy-t.offset current := itemCount == t.cy-t.offset
if current && (!t.raw || t.isItemMatch(item.item)) {
t.lastMatchingIndex = item.Index()
}
line = t.printItem(item, line, maxy, itemCount, current, barRange) line = t.printItem(item, line, maxy, itemCount, current, barRange)
} else if !t.prevLines[line].empty { } else if !t.prevLines[line].empty {
t.renderEmptyLine(line, barRange) t.renderEmptyLine(line, barRange)
@@ -6122,26 +6117,50 @@ func (t *Terminal) Loop() error {
req(reqList) req(reqList)
case actToggleRaw, actEnableRaw, actDisableRaw: case actToggleRaw, actEnableRaw, actDisableRaw:
prevRaw := t.raw prevRaw := t.raw
newRaw := t.raw
switch a.t { switch a.t {
case actEnableRaw: case actEnableRaw:
t.raw = true newRaw = true
case actDisableRaw: case actDisableRaw:
t.raw = false newRaw = false
case actToggleRaw: case actToggleRaw:
t.raw = !t.raw newRaw = !t.raw
} }
if prevRaw == t.raw { if prevRaw == newRaw {
break break
} }
prevPos := t.cy - t.offset prevPos := t.cy - t.offset
prevIndex := t.currentIndex() prevIndex := t.currentIndex()
if t.raw { if newRaw {
// Build matchMap if not available // Build matchMap if not available
if len(t.matchMap) == 0 { if len(t.matchMap) == 0 {
t.matchMap = t.resultMerger.ToMap() t.matchMap = t.resultMerger.ToMap()
} }
t.merger = t.passMerger t.merger = t.passMerger
} else { } else {
// Find the closest matching item
if !t.isCurrentItemMatch() && t.resultMerger.Length() > 1 {
distance := 0
Loop:
for {
distance++
checks := 0
for _, cy := range []int{t.cy + distance, t.cy - distance} {
if cy >= 0 && cy < t.merger.Length() {
checks++
item := t.merger.Get(cy).item
if t.isItemMatch(item) {
prevIndex = item.Index()
break Loop
}
}
}
if checks == 0 {
break
}
}
}
t.merger = t.resultMerger t.merger = t.resultMerger
// Need to remove non-matching items from the selection // Need to remove non-matching items from the selection
@@ -6150,15 +6169,11 @@ func (t *Terminal) Loop() error {
req(reqInfo) req(reqInfo)
} }
} }
t.raw = newRaw
// Try to retain position // Try to retain position
if prevIndex != minItem.Index() { if prevIndex != minItem.Index() {
i := t.merger.FindIndex(prevIndex) t.cy = util.Max(0, t.merger.FindIndex(prevIndex))
if i >= 0 {
t.cy = i
} else {
t.cy = t.merger.FindIndex(t.lastMatchingIndex)
}
t.offset = t.cy - prevPos t.offset = t.cy - prevPos
} }

View File

@@ -79,5 +79,27 @@ class TestRaw < TestInteractive
tmux.send_keys :Space tmux.send_keys :Space
tmux.until { assert_includes it, '[[]] 11' } tmux.until { assert_includes it, '[[]] 11' }
tmux.send_keys 'C-u', '5'
tmux.until { assert_includes it, '> 5' }
tmux.send_keys 'C-x', 'C-p', 'C-p'
tmux.until do
assert_includes it, '> 25'
assert_includes it, '▖ 24'
end
tmux.send_keys 'C-x'
tmux.until do
assert_includes it, '> 25'
assert_includes it, '▌ 15'
end
# 35 is the closest match in raw mode
tmux.send_keys 'C-x', :Up, :Up, :Up, :Up, :Up, :Up, 'C-x'
tmux.until do
assert_includes it, '> 35'
assert_includes it, '▌ 25'
end
end end
end end