Skip to content
Myr

Add become(...) action that replaces current fzf process

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Close #3159

authored by Junegunn Choi · committed by Junegunn Choi · parent f7447ae · Browse files · 5 files+56 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+56 added−7 removed

  • tests:+7 added−0 removed
  • other:+49 added−7 removed

5 files+56 added−7 removed

modified:man/man1/fzf.1+9 added
997 unmodified lines
998 \fBbackward-delete-char\fR \fIctrl-h bspace\fR999 \fBbackward-delete-char/eof\fR (same as \fBbackward-delete-char\fR except aborts fzf if query is empty)1000 \fBbackward-kill-word\fR \fIalt-bs\fR1001 \fBbackward-word\fR \fIalt-b shift-left\fR1002 \fBbecome(...)\fR (replace fzf process with the specified command; see below for the details)1003 \fBbeginning-of-line\fR \fIctrl-a home\fR1004 \fBcancel\fR (clear query string if not empty, abort fzf otherwise)1005 \fBchange-border-label(...)\fR (change \fB--border-label\fR to the given string)1006 \fBchange-preview(...)\fR (change \fB--preview\fR option)
136 unmodified lines
1143On *nix systems, fzf runs the command with \fB$SHELL -c\fR if \fBSHELL\fR is1144set, otherwise with \fBsh -c\fR, so in this case make sure that the command is1145POSIX-compliant.11461147\fBbecome(...)\fR action is similar to \fBexecute(...)\fR, but it replaces the1148current fzf process with the specifed command using \fBexecve(2)\fR system1149call.11501151 \fBfzf --bind "enter:become(vim {})"\fR11521153\fBbecome(...)\fR is not supported on Windows.11541155.SS RELOAD INPUT11561157\fBreload(...)\fR action is used to dynamically update the input list1158without restarting fzf. It takes the same command template with placeholder
77 unmodified lines
modified:src/options.go+8 added-1 removed
8 unmodified lines
9 "unicode"1011 "github.com/junegunn/fzf/src/algo"12 "github.com/junegunn/fzf/src/tui"13 "github.com/junegunn/fzf/src/util"1415 "github.com/mattn/go-runewidth"16 "github.com/mattn/go-shellwords"17)
903 unmodified lines
921)922923func init() {924 executeRegexp = regexp.MustCompile(924 `(?si)[:+](execute(?:-multi|-silent)?|reload(?:-sync)?|preview|(?:change|transform)-(?:query|prompt|border-label|preview-label)|change-preview-window|change-preview|(?:re|un)bind|pos|put)`)925 `(?si)[:+](become|execute(?:-multi|-silent)?|reload(?:-sync)?|preview|(?:change|transform)-(?:query|prompt|border-label|preview-label)|change-preview-window|change-preview|(?:re|un)bind|pos|put)`)926 splitRegexp = regexp.MustCompile("[,:]+")927 actionNameRegexp = regexp.MustCompile("(?i)^[a-z-]+")928}929
241 unmodified lines
1171 actionArg = spec[offset+1 : len(spec)-1]1172 actions = append(actions, &action{t: t, a: actionArg})1173 }1174 switch t {1175 case actBecome:1176 if util.IsWindows() {1177 exit("become action is not supported on Windows")1178 }1179 case actUnbind, actRebind:1180 parseKeyChordsImpl(actionArg, spec[0:offset]+" target required", exit)1181 case actChangePreviewWindow:1182 opts := previewOpts{}
44 unmodified lines
1227 }12281229 prefix := actionNameRegexp.FindString(str)1230 switch prefix {1231 case "become":1232 return actBecome1233 case "reload":1234 return actReload1235 case "reload-sync":1236 return actReloadSync
818 unmodified lines
modified:src/terminal.go+23 added-6 removed
4 unmodified lines
5 "fmt"6 "io/ioutil"7 "math"8 "os"9 "os/exec"10 "os/signal"11 "regexp"12 "sort"13 "strconv"
373 unmodified lines
387 actSelect388 actDeselect389 actUnbind390 actRebind391 actBecome392)393394type placeholderFlags struct {395 plus bool
1842 unmodified lines
2238}22392240func (t *Terminal) executeCommand(template string, forcePlus bool, background bool, captureFirstLine bool) string {2241 line := ""2240 valid, list := t.buildPlusList(template, forcePlus, false)2242 valid, list := t.buildPlusList(template, forcePlus)2243 // captureFirstLine is used for transform-{prompt,query} and we don't want to2244 // return an empty string in those cases2245 if !valid && !captureFirstLine {2246 return line
51 unmodified lines
2298 }2299 return nil2300}23012300func (t *Terminal) buildPlusList(template string, forcePlus bool, forceEvaluation bool) (bool, []*Item) {2302func (t *Terminal) buildPlusList(template string, forcePlus bool) (bool, []*Item) {2303 current := t.currentItem()2304 slot, plus, query := hasPreviewFlags(template)2303 if !forceEvaluation && !(!slot || query || (forcePlus || plus) && len(t.selected) > 0) {2305 if !(!slot || query || (forcePlus || plus) && len(t.selected) > 0) {2306 return current != nil, []*Item{current, current}2307 }23082309 // We would still want to update preview window even if there is no match if
316 unmodified lines
2626 }26272628 refreshPreview := func(command string) {2629 if len(command) > 0 && t.canPreview() {2628 _, list := t.buildPlusList(command, false, false)2630 _, list := t.buildPlusList(command, false)2631 t.cancelPreview()2632 t.previewBox.Set(reqPreviewEnqueue, previewRequest{command, t.pwindow, t.evaluateScrollOffset(), list})2633 }2634 }
226 unmodified lines
2861 }2862 doAction = func(a *action) bool {2863 switch a.t {2864 case actIgnore:2865 case actBecome:2866 _, list := t.buildPlusList(a.a, false)2867 command := t.replacePlaceholder(a.a, false, string(t.input), list)2868 shell := os.Getenv("SHELL")2869 if len(shell) == 0 {2870 shell = "sh"2871 }2872 shellPath, err := exec.LookPath(shell)2873 if err == nil {2874 t.tui.Close()2875 if t.history != nil {2876 t.history.append(string(t.input))2877 }2878 syscall.Exec(shellPath, []string{shell, "-c", command}, os.Environ())2879 }2880 case actExecute, actExecuteSilent:2881 t.executeCommand(a.a, false, a.t == actExecuteSilent, false)2882 case actExecuteMulti:2883 t.executeCommand(a.a, true, false, false)
13 unmodified lines
2897 if act {2898 t.activePreviewOpts.Toggle()2899 updatePreviewWindow(false)2900 if t.canPreview() {2884 valid, list := t.buildPlusList(t.previewOpts.command, false, false)2901 valid, list := t.buildPlusList(t.previewOpts.command, false)2902 if valid {2903 t.cancelPreview()2904 t.previewBox.Set(reqPreviewEnqueue,2905 previewRequest{t.previewOpts.command, t.pwindow, t.evaluateScrollOffset(), list})
470 unmodified lines
3376 }3377 case actReload, actReloadSync:3378 t.failed = nil33793363 valid, list := t.buildPlusList(a.a, false, false)3380 valid, list := t.buildPlusList(a.a, false)3381 if !valid {3382 // We run the command even when there's no match3383 // 1. If the template doesn't have any slots3384 // 2. If the template has {q}
205 unmodified lines
modified:test/test_go.rb+7 added
2641 unmodified lines
2642 tmux.until { |lines| refute_includes lines, '/1/1/' }2643 tmux.send_keys :Space2644 tmux.until { |lines| assert_includes lines, '/1/1/' }2645 end26462647 def test_become2648 tmux.send_keys "seq 10 | #{FZF} --bind 'enter:become:seq 100 | #{FZF}'", :Enter2649 tmux.until { |lines| assert_equal 10, lines.item_count }2650 tmux.send_keys :Enter2651 tmux.until { |lines| assert_equal 100, lines.item_count }2652 end2653end26542655module TestShell2656 def setup
481 unmodified lines
modified:CHANGELOG.md+9 added
2 unmodified lines
340.38.05------6- New actions7 - `become(...)` - Replace the current fzf process with the specified8 command using `execve(2)` system call. This action enables a simpler9 alternative to using `--expect` and checking the output in the wrapping10 script.11 ```sh12 # Open selected files in different editors13 fzf --multi --bind 'enter:become($EDITOR {+}),ctrl-n:become(nano {+})'14 ```15 - This action is not supported on Windows16 - `show-preview`17 - `hide-preview`18- Bug fixes19 - `--preview-window 0,hidden` should not execute the preview command until
1541 unmodified lines