Improve query modification prevention in input-less mode

fzf would restore the original query in input-less mode after executing
a chain of actions.

This commit changes the behavior so that the restoration
happens after each action to allow something like
'show-input+change-query(...)+hide-input'.

Fix #4326
This commit is contained in:
Junegunn Choi 2025-03-26 10:34:52 +09:00
parent f43e82f17f
commit 4a0ab6c926
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
2 changed files with 31 additions and 6 deletions

View File

@ -4934,6 +4934,14 @@ func (t *Terminal) Loop() error {
return true
}
doAction = func(a *action) bool {
// Keep track of the current query before the action is executed,
// so we can restore it when the input section is hidden (--no-input).
// * By doing this, we don't have to add a conditional branch to each
// query modifying action.
// * We restore the query after each action instead of after a set of
// actions to allow changing the query even when the input is hidden
// e.g. fzf --no-input --bind 'space:show-input+change-query(foo)+hide-input'
currentInput := t.input
Action:
switch a.t {
case actIgnore, actStart, actClick:
@ -6025,6 +6033,13 @@ func (t *Terminal) Loop() error {
if !processExecution(a.t) {
t.lastAction = a.t
}
if t.inputless {
// Always just discard the change
t.input = currentInput
t.cx = len(t.input)
beof = false
}
return true
}
@ -6045,12 +6060,7 @@ func (t *Terminal) Loop() error {
} else if !doActions(actions) {
continue
}
if t.inputless {
// Always just discard the change
t.input = previousInput
t.cx = len(t.input)
beof = false
} else {
if !t.inputless {
t.truncateQuery()
}
queryChanged = queryChanged || t.pasting == nil && string(previousInput) != string(t.input)

View File

@ -1866,4 +1866,19 @@ class TestCore < TestInteractive
assert_includes lines, '> 555'
end
end
def test_no_input_change_query
tmux.send_keys %(seq 1000 | #{FZF} --multi --query 999 --no-input --bind 'enter:show-input+change-query(555)+hide-input,space:change-query(555)+select'), :Enter
tmux.until { |lines| assert_includes lines, '> 999' }
tmux.send_keys :Space
tmux.until do |lines|
assert_includes lines, '>>999'
refute_includes lines, '> 555'
end
tmux.send_keys :Enter
tmux.until do |lines|
refute_includes lines, '>>999'
assert_includes lines, '> 555'
end
end
end