Fix {q} in preview window affected by 'search' action

This commit is contained in:
Junegunn Choi 2025-02-18 10:08:47 +09:00
parent 01d9d9c8c8
commit f975b40236
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 20 additions and 4 deletions

View File

@ -638,6 +638,7 @@ type previewRequest struct {
scrollOffset int scrollOffset int
list []*Item list []*Item
env []string env []string
query string
} }
type previewResult struct { type previewResult struct {
@ -4385,6 +4386,7 @@ func (t *Terminal) Loop() error {
var items []*Item var items []*Item
var commandTemplate string var commandTemplate string
var env []string var env []string
var query string
initialOffset := 0 initialOffset := 0
t.previewBox.Wait(func(events *util.Events) { t.previewBox.Wait(func(events *util.Events) {
for req, value := range *events { for req, value := range *events {
@ -4398,6 +4400,7 @@ func (t *Terminal) Loop() error {
initialOffset = request.scrollOffset initialOffset = request.scrollOffset
items = request.list items = request.list
env = request.env env = request.env
query = request.query
} }
} }
events.Clear() events.Clear()
@ -4411,8 +4414,7 @@ func (t *Terminal) Loop() error {
version++ version++
// We don't display preview window if no match // We don't display preview window if no match
if items[0] != nil { if items[0] != nil {
_, query := t.Input() command, tempFiles := t.replacePlaceholder(commandTemplate, false, query, items)
command, tempFiles := t.replacePlaceholder(commandTemplate, false, string(query), items)
cmd := t.executor.ExecCommand(command, true) cmd := t.executor.ExecCommand(command, true)
cmd.Env = env cmd.Env = env
@ -4540,7 +4542,7 @@ func (t *Terminal) Loop() error {
if len(command) > 0 && t.canPreview() { if len(command) > 0 && t.canPreview() {
_, list := t.buildPlusList(command, false) _, list := t.buildPlusList(command, false)
t.cancelPreview() t.cancelPreview()
t.previewBox.Set(reqPreviewEnqueue, previewRequest{command, t.evaluateScrollOffset(), list, t.environForPreview()}) t.previewBox.Set(reqPreviewEnqueue, previewRequest{command, t.evaluateScrollOffset(), list, t.environForPreview(), string(t.input)})
} }
} }
@ -4972,7 +4974,7 @@ func (t *Terminal) Loop() error {
if valid { if valid {
t.cancelPreview() t.cancelPreview()
t.previewBox.Set(reqPreviewEnqueue, t.previewBox.Set(reqPreviewEnqueue,
previewRequest{t.previewOpts.command, t.evaluateScrollOffset(), list, t.environForPreview()}) previewRequest{t.previewOpts.command, t.evaluateScrollOffset(), list, t.environForPreview(), string(t.input)})
} }
} else { } else {
// Discard the preview content so that it won't accidentally appear // Discard the preview content so that it won't accidentally appear

View File

@ -544,4 +544,18 @@ class TestPreview < TestInteractive
tmux.send_keys :Up tmux.send_keys :Up
tmux.until { |lines| assert_includes lines, '> 2' } tmux.until { |lines| assert_includes lines, '> 2' }
end end
def test_preview_query_should_not_be_affected_by_search
tmux.send_keys "seq 1 | #{FZF} --bind 'change:transform-search(echo {q:1})' --preview 'echo [{q}/{}]'", :Enter
tmux.until { |lines| assert_equal 1, lines.match_count }
tmux.send_keys '1'
tmux.until { |lines| assert lines.any_include?('[1/1]') }
tmux.send_keys :Space
tmux.until { |lines| assert lines.any_include?('[1 /1]') }
tmux.send_keys '2'
tmux.until do |lines|
assert lines.any_include?('[1 2/1]')
assert_equal 1, lines.match_count
end
end
end end