Skip to content
Myr

Respect SHELL env var on Windows (#2641)

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

This makes fzf respect SHELL environment variable on Windows, like it does on *nix, whenever defined. Close #2638

authored by Rashil Gandhi · committed by GitHub · parent 97ae8af · Browse files · 2 files+53 added−13 removed
This commit

State

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

Diff stats

2 files+53 added−13 removed

  • other:+53 added−13 removed

2 files+53 added−13 removed

modified:src/util/util_windows.go+32 added-7 removed
4 unmodified lines
5import (6 "fmt"7 "os"8 "os/exec"9 "strings"10 "syscall"11)1212// ExecCommand executes the given command with cmd13// ExecCommand executes the given command with $SHELL14func ExecCommand(command string, setpgid bool) *exec.Cmd {15 shell := os.Getenv("SHELL")16 if len(shell) == 0 {17 shell = "cmd"18 } else if strings.Contains(shell, "/") {19 out, err := exec.Command("cygpath", "-w", shell).Output()20 if err == nil {21 shell = strings.Trim(string(out), "\n")22 }23 }14 return ExecCommandWith("cmd", command, setpgid)24 return ExecCommandWith(shell, command, setpgid)25}2617// ExecCommandWith executes the given command with cmd. _shell parameter is18// ignored on Windows.27// ExecCommandWith executes the given command with the specified shell28// FIXME: setpgid is unused. We set it in the Unix implementation so that we29// can kill preview process with its child processes at once.21func ExecCommandWith(_shell string, command string, setpgid bool) *exec.Cmd {22 cmd := exec.Command("cmd")30// NOTE: For "powershell", we should ideally set output encoding to UTF8,31// but it is left as is now because no adverse effect has been observed.32func ExecCommandWith(shell string, command string, setpgid bool) *exec.Cmd {33 var commandline string34 if strings.Contains(shell, "cmd") {35 commandline = fmt.Sprintf(` /v:on/s/c "%s"`, command)36 } else if strings.Contains(shell, "pwsh") || strings.Contains(shell, "powershell") {37 commandline = fmt.Sprintf(` -NoProfile -Command "& { %s }"`, command)38 }39 if len(commandline) == 0 {40 cmd := exec.Command(shell, "-c", command)41 cmd.SysProcAttr = &syscall.SysProcAttr{42 HideWindow: false,43 CreationFlags: 0,44 }45 return cmd46 }47 cmd := exec.Command(shell)48 cmd.SysProcAttr = &syscall.SysProcAttr{49 HideWindow: false,25 CmdLine: fmt.Sprintf(` /v:on/s/c "%s"`, command),50 CmdLine: commandline,51 CreationFlags: 0,52 }53 return cmd54}
20 unmodified lines
modified:src/terminal_windows.go+21 added-6 removed
19 unmodified lines
20 // NOOP21}2223func quoteEntry(entry string) string {24 shell := os.Getenv("SHELL")25 if len(shell) == 0 {26 shell = "cmd"27 }2829 if strings.Contains(shell, "cmd") {30 // backslash escaping is done here for applications31 // (see ripgrep test case in terminal_test.go#TestWindowsCommands)32 escaped := strings.Replace(entry, `\`, `\\`, -1)24 escaped := strings.Replace(entry, `\`, `\\`, -1)25 escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"`26 r, _ := regexp.Compile(`[&|<>()@^%!"]`)27 return r.ReplaceAllStringFunc(escaped, func(match string) string {28 return "^" + match29 })33 escaped = `"` + strings.Replace(escaped, `"`, `\"`, -1) + `"`34 // caret is the escape character for cmd shell35 r, _ := regexp.Compile(`[&|<>()@^%!"]`)36 return r.ReplaceAllStringFunc(escaped, func(match string) string {37 return "^" + match38 })39 } else if strings.Contains(shell, "pwsh") || strings.Contains(shell, "powershell") {40 escaped := strings.Replace(entry, `"`, `""`, -1)41 return "'" + strings.Replace(escaped, "'", "''", -1) + "'"42 } else {43 return "'" + strings.Replace(entry, "'", "'\\''", -1) + "'"44 }45}