Add transform-query(...) action
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Test case authored by @SpicyLemon Close #1930 Close #2465 Close #2559 Close #2509 (e.g. fzf --bind 'space:transform-query:printf %s%s {q} {}')
authored by Junegunn Choi · committed by Junegunn Choi · parent 4dbe456 · Browse files · 5 files·+51 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+51 added−7 removed
- tests:+9 added−0 removed
- other:+42 added−7 removed
Provenance
- Commit
36d2bb3- Tree
1a5573e- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
4dbe456
5 files changed+51 added−7 removed
modified:man/man1/fzf.1+1 added
1021 unmodified lines1022 \fBtoggle-preview-wrap\fR1023 \fBtoggle-search\fR (toggle search functionality)1024 \fBtoggle-sort\fR1025 \fBtoggle+up\fR \fIbtab (shift-tab)\fR1026+ \fBtransform-query(...)\fR (transform query string using an external command)1027 \fBunbind(...)\fR (unbind bindings)1028 \fBunix-line-discard\fR \fIctrl-u\fR1029 \fBunix-word-rubout\fR \fIctrl-w\fR1030 \fBup\fR \fIctrl-k ctrl-p up\fR136 unmodified linesmodified:src/options.go+3 added-1 removed
888 unmodified lines889 )890 891 func init() {892 executeRegexp = regexp.MustCompile(893- `(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-query|change-prompt|change-preview-window|change-preview|(?:re|un)bind|pos|put)`)893+ `(?si)[:+](execute(?:-multi|-silent)?|reload|preview|change-query|change-prompt|change-preview-window|change-preview|(?:re|un)bind|pos|put|transform-query)`)894 splitRegexp = regexp.MustCompile("[,:]+")895 actionNameRegexp = regexp.MustCompile("(?i)^[a-z-]+")896 }897 308 unmodified lines1206 case "execute-multi":1207 return actExecuteMulti1208 case "put":1209 return actPut1210+ case "transform-query":1211+ return actTransformQuery1212 }1213 return actIgnore1214 }1215 761 unmodified linesmodified:src/terminal.go+21 added-5 removed
306 unmodified lines307 actReplaceQuery308 actToggleSort309 actTogglePreview310 actTogglePreviewWrap311+ actTransformQuery312 actPreview313 actChangePreview314 actChangePreviewWindow315 actPreviewTop1698 unmodified lines2014 t.tui.Refresh()2015 t.printAll()2016 }2017 2017-func (t *Terminal) executeCommand(template string, forcePlus bool, background bool) {2018+func (t *Terminal) executeCommand(template string, forcePlus bool, background bool, captureFirstLine bool) string {2019+ line := ""2020 valid, list := t.buildPlusList(template, forcePlus)2021 if !valid {2020- return2022+ return line2023 }2024 command := t.replacePlaceholder(template, forcePlus, string(t.input), list)2025 cmd := util.ExecCommand(command, false)2026 t.executing.Set(true)7 unmodified lines2034 t.redraw(true)2035 t.refresh()2036 } else {2037 t.tui.Pause(false)2038+ if captureFirstLine {2039+ out, _ := cmd.StdoutPipe()2040+ reader := bufio.NewReader(out)2041+ cmd.Start()2042+ line, _ = reader.ReadString('\n')2043+ line = strings.TrimRight(line, "\r\n")2044+ cmd.Wait()2045+ } else {2036- cmd.Run()2046+ cmd.Run()2047+ }2048 t.tui.Resume(false, false)2049 }2050 t.executing.Set(false)2051 cleanTemporaryFiles()2052+ return line2053 }2054 2055 func (t *Terminal) hasPreviewer() bool {2056 return t.previewBox != nil564 unmodified lines2621 doAction = func(a *action) bool {2622 switch a.t {2623 case actIgnore:2624 case actExecute, actExecuteSilent:2613- t.executeCommand(a.a, false, a.t == actExecuteSilent)2625+ t.executeCommand(a.a, false, a.t == actExecuteSilent, false)2626 case actExecuteMulti:2615- t.executeCommand(a.a, true, false)2627+ t.executeCommand(a.a, true, false, false)2628 case actInvalid:2629 t.mutex.Unlock()2630 return false2631 case actTogglePreview:14 unmodified lines2646 // Reset preview version so that full redraw occurs2647 t.previewed.version = 02648 req(reqPreviewRefresh)2649 }2650+ case actTransformQuery:2651+ query := t.executeCommand(a.a, false, true, true)2652+ t.input = []rune(query)2653+ t.cx = len(t.input)2654 case actToggleSort:2655 t.sort = !t.sort2656 changed = true2657 case actPreviewTop:574 unmodified linesmodified:test/test_go.rb+9 added
1795 unmodified lines1796 tmux.send_keys :Space, 'baz'1797 tmux.until { |lines| assert_equal '> foobarbaz', lines.last }1798 end1799 1800+ def test_transform_query1801+ tmux.send_keys %{#{FZF} --bind 'ctrl-r:transform-query(rev <<< {q}),ctrl-u:transform-query: tr "[:lower:]" "[:upper:]" <<< {q}' --query bar}, :Enter1802+ tmux.until { |lines| assert_equal '> bar', lines[-1] }1803+ tmux.send_keys 'C-r'1804+ tmux.until { |lines| assert_equal '> rab', lines[-1] }1805+ tmux.send_keys 'C-u'1806+ tmux.until { |lines| assert_equal '> RAB', lines[-1] }1807+ end1808+1809 def test_clear_selection1810 tmux.send_keys %(seq 100 | #{FZF} --multi --bind space:clear-selection), :Enter1811 tmux.until { |lines| assert_equal 100, lines.match_count }1812 tmux.send_keys :Tab1150 unmodified linesmodified:CHANGELOG.md+17 added-1 removed
29 unmodified lines30 31 # Both actions respect --layout option32 seq 10 | fzf --multi --bind ctrl-n:next-selected,ctrl-p:prev-selected --layout reverse33 ```34-- Added `change-query(...)` action34+- Added `change-query(...)` action that simply changes the query string to the35+ given static string. This can be useful when used with `--listen`.36+ ```sh37+ curl localhost:6266 -d "change-query:$(date)"38+ ```39+- Added `transform-query(...)` action for transforming the query string using40+ an external command41+ ```sh42+ # Press space to convert the query to uppercase letters43+ fzf --bind 'space:transform-query(tr [:lower:] [:upper:] <<< {q})'44+45+ # Bind it to 'change' event for automatic conversion46+ fzf --bind 'change:transform-query(tr [:lower:] [:upper:] <<< {q})'47+48+ # Can only type numbers49+ fzf --bind 'change:transform-query(sed 's/[^0-9]//g' <<< {q})'50+ ```51 - `put` action can optionally take an argument string52 ```sh53 # a will put 'alpha' on the prompt, ctrl-b will put 'bravo'54 fzf --bind 'a:put+put(lpha),ctrl-b:put(bravo)'1400 unmodified lines