From 592e7fe4be7cf976df353e177c61c5b2df8541bb Mon Sep 17 00:00:00 2001 From: Joe Lim <50560759+joelim-work@users.noreply.github.com> Date: Tue, 4 Jul 2023 00:31:45 +1000 Subject: [PATCH] Improve windows shell handling (#1309) * Improve windows shell handling * Add shell in command line * Fix shellopts bug * Add quote string * Refactor `exportFiles` function * Add defaults for Windows * Revert "Add defaults for Windows" Accidental commit This reverts commit 5ff32650feb6928f6226d9e40e9042b32de602af. * Separate handling for CMD vs PowerShell * Add case insensitive check --- main.go | 2 +- nav.go | 18 +++++++++++++++--- os.go | 15 ++------------- os_windows.go | 39 +++++++++++++++++---------------------- 4 files changed, 35 insertions(+), 39 deletions(-) diff --git a/main.go b/main.go index fa267e8e..22667c99 100644 --- a/main.go +++ b/main.go @@ -85,7 +85,7 @@ func exportEnvVars() { log.Printf("getting path to lf binary: %s", err) lfPath = "lf" } - os.Setenv("lf", lfPath) + os.Setenv("lf", quoteString(lfPath)) } // used by exportOpts below diff --git a/nav.go b/nav.go index d4912378..9322596c 100644 --- a/nav.go +++ b/nav.go @@ -655,12 +655,24 @@ func (nav *nav) exportFiles() { var currFile string if curr, err := nav.currFile(); err == nil { - currFile = curr.path + currFile = quoteString(curr.path) } - currSelections := nav.currSelections() + var selections []string + for _, selection := range nav.currSelections() { + selections = append(selections, quoteString(selection)) + } + currSelections := strings.Join(selections, gOpts.filesep) - exportFiles(currFile, currSelections, nav.currDir().path) + os.Setenv("f", currFile) + os.Setenv("fs", currSelections) + os.Setenv("PWD", quoteString(nav.currDir().path)) + + if len(selections) == 0 { + os.Setenv("fx", currFile) + } else { + os.Setenv("fx", currSelections) + } } func (nav *nav) dirPreviewLoop(ui *ui) { diff --git a/os.go b/os.go index 8fbd3338..51385abe 100644 --- a/os.go +++ b/os.go @@ -224,17 +224,6 @@ func errCrossDevice(err error) bool { return err.(*os.LinkError).Err.(unix.Errno) == unix.EXDEV } -func exportFiles(f string, fs []string, pwd string) { - envFile := f - envFiles := strings.Join(fs, gOpts.filesep) - - os.Setenv("f", envFile) - os.Setenv("fs", envFiles) - os.Setenv("PWD", pwd) - - if len(fs) == 0 { - os.Setenv("fx", envFile) - } else { - os.Setenv("fx", envFiles) - } +func quoteString(s string) string { + return s } diff --git a/os_windows.go b/os_windows.go index ff9ac4bd..ea8bd0a6 100644 --- a/os_windows.go +++ b/os_windows.go @@ -101,10 +101,23 @@ func detachedCommand(name string, arg ...string) *exec.Cmd { } func shellCommand(s string, args []string) *exec.Cmd { + // Windows CMD requires special handling to deal with quoted arguments + if strings.ToLower(gOpts.shell) == "cmd" { + var builder strings.Builder + builder.WriteString(s) + for _, arg := range args { + fmt.Fprintf(&builder, ` "%s"`, arg) + } + shellOpts := strings.Join(gOpts.shellopts, " ") + cmdline := fmt.Sprintf(`%s %s %s "%s"`, gOpts.shell, shellOpts, gOpts.shellflag, builder.String()) + + cmd := exec.Command(gOpts.shell) + cmd.SysProcAttr = &windows.SysProcAttr{CmdLine: cmdline} + return cmd + } + args = append([]string{gOpts.shellflag, s}, args...) - args = append(gOpts.shellopts, args...) - return exec.Command(gOpts.shell, args...) } @@ -168,24 +181,6 @@ func errCrossDevice(err error) bool { return err.(*os.LinkError).Err.(windows.Errno) == 17 } -func exportFiles(f string, fs []string, pwd string) { - envFile := fmt.Sprintf(`"%s"`, f) - - var quotedFiles []string - for _, f := range fs { - quotedFiles = append(quotedFiles, fmt.Sprintf(`"%s"`, f)) - } - envFiles := strings.Join(quotedFiles, gOpts.filesep) - - envPWD := fmt.Sprintf(`"%s"`, pwd) - - os.Setenv("f", envFile) - os.Setenv("fs", envFiles) - os.Setenv("PWD", envPWD) - - if len(fs) == 0 { - os.Setenv("fx", envFile) - } else { - os.Setenv("fx", envFiles) - } +func quoteString(s string) string { + return fmt.Sprintf(`"%s"`, s) }