Skip to content

Commit

Permalink
Add --with-shell for shelling out with different command and flags (#…
Browse files Browse the repository at this point in the history
…3746)

Close #3732
  • Loading branch information
junegunn committed Apr 27, 2024
1 parent b86a967 commit a4391ae
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 128 deletions.
13 changes: 13 additions & 0 deletions CHANGELOG.md
Expand Up @@ -3,10 +3,23 @@ CHANGELOG

0.51.0
------
- Added `--with-shell` option to start child processes with a custom shell command and flags
```sh
gem list | fzf --with-shell 'ruby -e' \
--preview 'pp Gem::Specification.find_by_name({1})' \
--bind 'ctrl-o:execute-silent:
spec = Gem::Specification.find_by_name({1})
[spec.homepage, *spec.metadata.filter { _1.end_with?("uri") }.values].uniq.each do
system "open", _1
end
'
```
- Added `change-multi` action for dynamically changing `--multi` option
- `change-multi` - enable multi-select mode with no limit
- `change-multi(NUM)` - enable multi-select mode with a limit
- `change-multi(0)` - disable multi-select mode
- `become` action is now supported on Windows
- Unlike in *nix, this does not use `execve(2)`. Instead it spawns a new process and waits for it to finish, so the exact behavior may differ.
- Bug fixes and improvements

0.50.0
Expand Down
14 changes: 12 additions & 2 deletions man/man1/fzf.1
Expand Up @@ -818,6 +818,16 @@ the finder only after the input stream is complete.
e.g. \fBfzf --multi | fzf --sync\fR
.RE
.TP
.B "--with-shell=STR"
Shell command and flags to start child processes with. On *nix Systems, the
default value is \fB$SHELL -c\fR if \fB$SHELL\fR is set, otherwise \fBsh -c\fR.
On Windows, the default value is \fBcmd /v:on/s/c\fR when \fB$SHELL\fR is not
set.

.RS
e.g. \fBgem list | fzf --with-shell 'ruby -e' --preview 'pp Gem::Specification.find_by_name({1})'\fR
.RE
.TP
.B "--listen[=[ADDR:]PORT]" "--listen-unsafe[=[ADDR:]PORT]"
Start HTTP server and listen on the given address. It allows external processes
to send actions to perform via POST method.
Expand Down Expand Up @@ -932,6 +942,8 @@ you need to protect against DNS rebinding and privilege escalation attacks.
.br
.BR 2 " Error"
.br
.BR 127 " Invalid shell command for \fBbecome\fR action"
.br
.BR 130 " Interrupted with \fBCTRL-C\fR or \fBESC\fR"

.SH FIELD INDEX EXPRESSION
Expand Down Expand Up @@ -1441,8 +1453,6 @@ call.

\fBfzf --bind "enter:become(vim {})"\fR

\fBbecome(...)\fR is not supported on Windows.

.SS RELOAD INPUT

\fBreload(...)\fR action is used to dynamically update the input list
Expand Down
9 changes: 6 additions & 3 deletions src/core.go
Expand Up @@ -121,13 +121,16 @@ func Run(opts *Options, version string, revision string) {
})
}

// Process executor
executor := util.NewExecutor(opts.WithShell)

// Reader
streamingFilter := opts.Filter != nil && !sort && !opts.Tac && !opts.Sync
var reader *Reader
if !streamingFilter {
reader = NewReader(func(data []byte) bool {
return chunkList.Push(data)
}, eventBox, opts.ReadZero, opts.Filter == nil)
}, eventBox, executor, opts.ReadZero, opts.Filter == nil)
go reader.ReadSource(opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip)
}

Expand Down Expand Up @@ -178,7 +181,7 @@ func Run(opts *Options, version string, revision string) {
mutex.Unlock()
}
return false
}, eventBox, opts.ReadZero, false)
}, eventBox, executor, opts.ReadZero, false)
reader.ReadSource(opts.WalkerRoot, opts.WalkerOpts, opts.WalkerSkip)
} else {
eventBox.Unwatch(EvtReadNew)
Expand Down Expand Up @@ -209,7 +212,7 @@ func Run(opts *Options, version string, revision string) {
go matcher.Loop()

// Terminal I/O
terminal := NewTerminal(opts, eventBox)
terminal := NewTerminal(opts, eventBox, executor)
maxFit := 0 // Maximum number of items that can fit on screen
padHeight := 0
heightUnknown := opts.Height.auto
Expand Down
10 changes: 6 additions & 4 deletions src/options.go
Expand Up @@ -120,6 +120,7 @@ const usage = `usage: fzf [options]
--read0 Read input delimited by ASCII NUL characters
--print0 Print output delimited by ASCII NUL characters
--sync Synchronous search for multi-staged filtering
--with-shell=STR Shell command and flags to start child processes with
--listen[=[ADDR:]PORT] Start HTTP server to receive actions (POST /)
(To allow remote process execution, use --listen-unsafe)
--version Display version information and exit
Expand Down Expand Up @@ -356,6 +357,7 @@ type Options struct {
Unicode bool
Ambidouble bool
Tabstop int
WithShell string
ListenAddr *listenAddress
Unsafe bool
ClearOnExit bool
Expand Down Expand Up @@ -1327,10 +1329,6 @@ func parseActionList(masked string, original string, prevActions []*action, putA
actions = append(actions, &action{t: t, a: actionArg})
}
switch t {
case actBecome:
if util.IsWindows() {
exit("become action is not supported on Windows")
}
case actUnbind, actRebind:
parseKeyChordsImpl(actionArg, spec[0:offset]+" target required", exit)
case actChangePreviewWindow:
Expand Down Expand Up @@ -1957,6 +1955,8 @@ func parseOptions(opts *Options, allArgs []string) {
nextString(allArgs, &i, "padding required (TRBL / TB,RL / T,RL,B / T,R,B,L)"))
case "--tabstop":
opts.Tabstop = nextInt(allArgs, &i, "tab stop required")
case "--with-shell":
opts.WithShell = nextString(allArgs, &i, "shell command and flags required")
case "--listen", "--listen-unsafe":
given, str := optionalNextString(allArgs, &i)
addr := defaultListenAddr
Expand Down Expand Up @@ -2073,6 +2073,8 @@ func parseOptions(opts *Options, allArgs []string) {
opts.Padding = parseMargin("padding", value)
} else if match, value := optString(arg, "--tabstop="); match {
opts.Tabstop = atoi(value)
} else if match, value := optString(arg, "--with-shell="); match {
opts.WithShell = value
} else if match, value := optString(arg, "--listen="); match {
addr, err := parseListenAddress(value)
if err != nil {
Expand Down
7 changes: 4 additions & 3 deletions src/reader.go
Expand Up @@ -18,6 +18,7 @@ import (
// Reader reads from command or standard input
type Reader struct {
pusher func([]byte) bool
executor *util.Executor
eventBox *util.EventBox
delimNil bool
event int32
Expand All @@ -30,8 +31,8 @@ type Reader struct {
}

// NewReader returns new Reader object
func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, delimNil bool, wait bool) *Reader {
return &Reader{pusher, eventBox, delimNil, int32(EvtReady), make(chan bool, 1), sync.Mutex{}, nil, nil, false, wait}
func NewReader(pusher func([]byte) bool, eventBox *util.EventBox, executor *util.Executor, delimNil bool, wait bool) *Reader {
return &Reader{pusher, executor, eventBox, delimNil, int32(EvtReady), make(chan bool, 1), sync.Mutex{}, nil, nil, false, wait}
}

func (r *Reader) startEventPoller() {
Expand Down Expand Up @@ -242,7 +243,7 @@ func (r *Reader) readFromCommand(command string, environ []string) bool {
r.mutex.Lock()
r.killed = false
r.command = &command
r.exec = util.ExecCommand(command, true)
r.exec = r.executor.ExecCommand(command, true)
if environ != nil {
r.exec.Env = environ
}
Expand Down
3 changes: 2 additions & 1 deletion src/reader_test.go
Expand Up @@ -10,9 +10,10 @@ import (
func TestReadFromCommand(t *testing.T) {
strs := []string{}
eb := util.NewEventBox()
exec := util.NewExecutor("")
reader := NewReader(
func(s []byte) bool { strs = append(strs, string(s)); return true },
eb, false, true)
eb, exec, false, true)

reader.startEventPoller()

Expand Down
53 changes: 25 additions & 28 deletions src/terminal.go
Expand Up @@ -7,7 +7,6 @@ import (
"io"
"math"
"os"
"os/exec"
"os/signal"
"regexp"
"sort"
Expand Down Expand Up @@ -245,6 +244,7 @@ type Terminal struct {
listenUnsafe bool
borderShape tui.BorderShape
cleanExit bool
executor *util.Executor
paused bool
border tui.Window
window tui.Window
Expand Down Expand Up @@ -640,7 +640,7 @@ func evaluateHeight(opts *Options, termHeight int) int {
}

// NewTerminal returns new Terminal object
func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
func NewTerminal(opts *Options, eventBox *util.EventBox, executor *util.Executor) *Terminal {
input := trimQuery(opts.Query)
var delay time.Duration
if opts.Tac {
Expand Down Expand Up @@ -736,6 +736,7 @@ func NewTerminal(opts *Options, eventBox *util.EventBox) *Terminal {
previewLabel: nil,
previewLabelOpts: opts.PreviewLabel,
cleanExit: opts.ClearOnExit,
executor: executor,
paused: opts.Phony,
cycle: opts.Cycle,
headerVisible: true,
Expand Down Expand Up @@ -2522,6 +2523,7 @@ type replacePlaceholderParams struct {
allItems []*Item
lastAction actionType
prompt string
executor *util.Executor
}

func (t *Terminal) replacePlaceholder(template string, forcePlus bool, input string, list []*Item) string {
Expand All @@ -2535,6 +2537,7 @@ func (t *Terminal) replacePlaceholder(template string, forcePlus bool, input str
allItems: list,
lastAction: t.lastAction,
prompt: t.promptString,
executor: t.executor,
})
}

Expand Down Expand Up @@ -2595,7 +2598,7 @@ func replacePlaceholder(params replacePlaceholderParams) string {
case escaped:
return match
case match == "{q}" || match == "{fzf:query}":
return quoteEntry(params.query)
return params.executor.QuoteEntry(params.query)
case match == "{}":
replace = func(item *Item) string {
switch {
Expand All @@ -2608,13 +2611,13 @@ func replacePlaceholder(params replacePlaceholderParams) string {
case flags.file:
return item.AsString(params.stripAnsi)
default:
return quoteEntry(item.AsString(params.stripAnsi))
return params.executor.QuoteEntry(item.AsString(params.stripAnsi))
}
}
case match == "{fzf:action}":
return params.lastAction.Name()
case match == "{fzf:prompt}":
return quoteEntry(params.prompt)
return params.executor.QuoteEntry(params.prompt)
default:
// token type and also failover (below)
rangeExpressions := strings.Split(match[1:len(match)-1], ",")
Expand Down Expand Up @@ -2648,7 +2651,7 @@ func replacePlaceholder(params replacePlaceholderParams) string {
str = strings.TrimSpace(str)
}
if !flags.file {
str = quoteEntry(str)
str = params.executor.QuoteEntry(str)
}
return str
}
Expand Down Expand Up @@ -2688,7 +2691,7 @@ func (t *Terminal) executeCommand(template string, forcePlus bool, background bo
return line
}
command := t.replacePlaceholder(template, forcePlus, string(t.input), list)
cmd := util.ExecCommand(command, false)
cmd := t.executor.ExecCommand(command, false)
cmd.Env = t.environ()
t.executing.Set(true)
if !background {
Expand Down Expand Up @@ -2965,7 +2968,7 @@ func (t *Terminal) Loop() {
if items[0] != nil {
_, query := t.Input()
command := t.replacePlaceholder(commandTemplate, false, string(query), items)
cmd := util.ExecCommand(command, true)
cmd := t.executor.ExecCommand(command, true)
env := t.environ()
if pwindowSize.Lines > 0 {
lines := fmt.Sprintf("LINES=%d", pwindowSize.Lines)
Expand Down Expand Up @@ -3372,27 +3375,21 @@ func (t *Terminal) Loop() {
valid, list := t.buildPlusList(a.a, false)
if valid {
command := t.replacePlaceholder(a.a, false, string(t.input), list)
shell := os.Getenv("SHELL")
if len(shell) == 0 {
shell = "sh"
}
shellPath, err := exec.LookPath(shell)
if err == nil {
t.tui.Close()
if t.history != nil {
t.history.append(string(t.input))
}
/*
FIXME: It is not at all clear why this is required.
The following command will report 'not a tty', unless we open
/dev/tty *twice* after closing the standard input for 'reload'
in Reader.terminate().
: | fzf --bind 'start:reload:ls' --bind 'enter:become:tty'
*/
tui.TtyIn()
util.SetStdin(tui.TtyIn())
syscall.Exec(shellPath, []string{shell, "-c", command}, os.Environ())
t.tui.Close()
if t.history != nil {
t.history.append(string(t.input))
}

/*
FIXME: It is not at all clear why this is required.
The following command will report 'not a tty', unless we open
/dev/tty *twice* after closing the standard input for 'reload'
in Reader.terminate().
while : | fzf --bind 'start:reload:ls' --bind 'load:become:tty'; do echo; done
*/
tui.TtyIn()
t.executor.Become(tui.TtyIn(), t.environ(), command)
}
case actExecute, actExecuteSilent:
t.executeCommand(a.a, false, a.t == actExecuteSilent, false, false)
Expand Down
4 changes: 3 additions & 1 deletion src/terminal_test.go
Expand Up @@ -23,6 +23,7 @@ func replacePlaceholderTest(template string, stripAnsi bool, delimiter Delimiter
allItems: allItems,
lastAction: actBackwardDeleteCharEof,
prompt: "prompt",
executor: util.NewExecutor(""),
})
}

Expand Down Expand Up @@ -244,6 +245,7 @@ func TestQuoteEntry(t *testing.T) {
unixStyle := quotes{``, `'`, `'\''`, `"`, `\`}
windowsStyle := quotes{`^`, `^"`, `'`, `\^"`, `\\`}
var effectiveStyle quotes
exec := util.NewExecutor("")

if util.IsWindows() {
effectiveStyle = windowsStyle
Expand Down Expand Up @@ -278,7 +280,7 @@ func TestQuoteEntry(t *testing.T) {
}

for input, expected := range tests {
escaped := quoteEntry(input)
escaped := exec.QuoteEntry(input)
expected = templateToString(expected, effectiveStyle)
if escaped != expected {
t.Errorf("Input: %s, expected: %s, actual %s", input, expected, escaped)
Expand Down
19 changes: 0 additions & 19 deletions src/terminal_unix.go
Expand Up @@ -5,26 +5,11 @@ package fzf
import (
"os"
"os/signal"
"strings"
"syscall"

"golang.org/x/sys/unix"
)

var escaper *strings.Replacer

func init() {
tokens := strings.Split(os.Getenv("SHELL"), "/")
if tokens[len(tokens)-1] == "fish" {
// https://fishshell.com/docs/current/language.html#quotes
// > The only meaningful escape sequences in single quotes are \', which
// > escapes a single quote and \\, which escapes the backslash symbol.
escaper = strings.NewReplacer("\\", "\\\\", "'", "\\'")
} else {
escaper = strings.NewReplacer("'", "'\\''")
}
}

func notifyOnResize(resizeChan chan<- os.Signal) {
signal.Notify(resizeChan, syscall.SIGWINCH)
}
Expand All @@ -41,7 +26,3 @@ func notifyStop(p *os.Process) {
func notifyOnCont(resizeChan chan<- os.Signal) {
signal.Notify(resizeChan, syscall.SIGCONT)
}

func quoteEntry(entry string) string {
return "'" + escaper.Replace(entry) + "'"
}

0 comments on commit a4391ae

Please sign in to comment.