Add replace-query action

replace-query action replaces the query string with the current
selection. If the selection is too long, it will be truncated.

If the line contains meta-characters of fzf search syntax, it is
possible that the line is no longer included in the updated result.

e.g.

  echo '!hello' | fzf --bind ctrl-v:replace-query

Close #1137
This commit is contained in:
Junegunn Choi 2017-12-01 13:08:55 +09:00
parent 1e8e1d3c9d
commit c20954f020
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
4 changed files with 18 additions and 0 deletions

View File

@ -525,6 +525,7 @@ triggered whenever the query string is changed.
\fBpreview-page-up\fR \fBpreview-page-up\fR
\fBprevious-history\fR (\fIctrl-p\fR on \fB--history\fR) \fBprevious-history\fR (\fIctrl-p\fR on \fB--history\fR)
\fBprint-query\fR (print query and exit) \fBprint-query\fR (print query and exit)
\fBreplace-query\fR (replace query string with the current selection)
\fBselect-all\fR \fBselect-all\fR
\fBtoggle\fR (\fIright-click\fR) \fBtoggle\fR (\fIright-click\fR)
\fBtoggle-all\fR \fBtoggle-all\fR

View File

@ -664,6 +664,8 @@ func parseKeymap(keymap map[int][]action, str string) {
appendAction(actAccept) appendAction(actAccept)
case "print-query": case "print-query":
appendAction(actPrintQuery) appendAction(actPrintQuery)
case "replace-query":
appendAction(actReplaceQuery)
case "backward-char": case "backward-char":
appendAction(actBackwardChar) appendAction(actBackwardChar)
case "backward-delete-char": case "backward-delete-char":

View File

@ -203,6 +203,7 @@ const (
actJump actJump
actJumpAccept actJumpAccept
actPrintQuery actPrintQuery
actReplaceQuery
actToggleSort actToggleSort
actTogglePreview actTogglePreview
actTogglePreviewWrap actTogglePreviewWrap
@ -1568,6 +1569,11 @@ func (t *Terminal) Loop() {
} }
case actPrintQuery: case actPrintQuery:
req(reqPrintQuery) req(reqPrintQuery)
case actReplaceQuery:
if t.cy < t.merger.Length() {
t.input = t.merger.Get(t.cy).item.text.ToRunes()
t.cx = len(t.input)
}
case actAbort: case actAbort:
req(reqQuit) req(reqQuit)
case actDeleteChar: case actDeleteChar:

View File

@ -769,6 +769,15 @@ class TestGoFZF < TestBase
assert_equal %w[print-my-query], readonce.split($INPUT_RECORD_SEPARATOR) assert_equal %w[print-my-query], readonce.split($INPUT_RECORD_SEPARATOR)
end end
def test_bind_replace_query
tmux.send_keys "seq 1 1000 | #{fzf '--print-query --bind=ctrl-j:replace-query'}", :Enter
tmux.send_keys '1'
tmux.until { |lines| lines[-2].end_with? '272/1000' }
tmux.send_keys 'C-k', 'C-j'
tmux.until { |lines| lines[-2].end_with? '29/1000' }
tmux.until { |lines| lines[-1].end_with? '> 10' }
end
def test_long_line def test_long_line
data = '.' * 256 * 1024 data = '.' * 256 * 1024
File.open(tempname, 'w') do |f| File.open(tempname, 'w') do |f|