feat: Add custom shell flag option for command execution

pull/3726/head
LangLangbart 2 months ago
parent e86b81bbf5
commit 6d1af4e769

@ -907,8 +907,18 @@ e.g. \fBfzf --fish | source\fR
.TP
.B FZF_DEFAULT_COMMAND
Default command to use when input is tty. On *nix systems, fzf runs the command
with \fB$SHELL -c\fR if \fBSHELL\fR is set, otherwise with \fBsh -c\fR, so in
this case make sure that the command is POSIX-compliant.
with \fB$SHELL $FZF_SHELL_FLAG\fR if \fBSHELL\fR and \fBFZF_SHELL_FLAGS\fR are set, otherwise with \fBsh -c\fR.
.TP
.B FZF_SHELL_FLAG
Custom flag to pass to the shell when executing commands. If not set, fzf defaults to \fB-c\fR.
.br
e.g.
\fBSHELL=$(which bun) FZF_SHELL_FLAG="--print" \\
fzf --listen \\
--bind 'start:reload:(await Bun.$`ls`.text()).trim()' \\
--preview 'await fetch(`http://127.0.0.1:${process.env.FZF_PORT}`)'\fR
.TP
.B FZF_DEFAULT_OPTS
Default options.

@ -3375,6 +3375,10 @@ func (t *Terminal) Loop() {
if len(shell) == 0 {
shell = "sh"
}
shellFlag := os.Getenv("FZF_SHELL_FLAG")
if len(shellFlag) == 0 {
shellFlag = "-c"
}
shellPath, err := exec.LookPath(shell)
if err == nil {
t.tui.Close()
@ -3390,7 +3394,7 @@ func (t *Terminal) Loop() {
*/
tui.TtyIn()
util.SetStdin(tui.TtyIn())
syscall.Exec(shellPath, []string{shell, "-c", command}, os.Environ())
syscall.Exec(shellPath, []string{shell, shellFlag, command}, os.Environ())
}
}
case actExecute, actExecuteSilent:

@ -10,18 +10,22 @@ import (
"golang.org/x/sys/unix"
)
// ExecCommand executes the given command with $SHELL
// ExecCommand executes the given command with $SHELL $FZF_SHELL_FLAG
func ExecCommand(command string, setpgid bool) *exec.Cmd {
shell := os.Getenv("SHELL")
if len(shell) == 0 {
shell = "sh"
}
return ExecCommandWith(shell, command, setpgid)
shellFlag := os.Getenv("FZF_SHELL_FLAG")
if len(shellFlag) == 0 {
shellFlag = "-c"
}
return ExecCommandWith(shell, shellFlag, command, setpgid)
}
// ExecCommandWith executes the given command with the specified shell
func ExecCommandWith(shell string, command string, setpgid bool) *exec.Cmd {
cmd := exec.Command(shell, "-c", command)
// ExecCommandWith executes the given command with the specified shell and flag
func ExecCommandWith(shell string, shellFlag string, command string, setpgid bool) *exec.Cmd {
cmd := exec.Command(shell, shellFlag, command)
if setpgid {
cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true}
}

Loading…
Cancel
Save