2024-05-20 08:06:44 +00:00
|
|
|
//go:build windows
|
|
|
|
|
|
|
|
package fzf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-05-23 12:14:08 +00:00
|
|
|
"strings"
|
2024-05-23 09:41:13 +00:00
|
|
|
|
|
|
|
"github.com/junegunn/fzf/src/util"
|
2024-05-20 08:06:44 +00:00
|
|
|
)
|
|
|
|
|
2024-05-23 09:41:13 +00:00
|
|
|
func isMintty345() bool {
|
|
|
|
return util.CompareVersions(os.Getenv("TERM_PROGRAM_VERSION"), "3.4.5") >= 0
|
|
|
|
}
|
|
|
|
|
|
|
|
func needWinpty(opts *Options) bool {
|
|
|
|
if os.Getenv("TERM_PROGRAM") != "mintty" {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if isMintty345() {
|
|
|
|
/*
|
|
|
|
See: https://github.com/junegunn/fzf/issues/3809
|
|
|
|
|
2024-06-01 05:42:23 +00:00
|
|
|
"MSYS=enable_pcon" allows fzf to run properly on mintty 3.4.5 or later.
|
2024-05-23 09:41:13 +00:00
|
|
|
*/
|
2024-05-23 12:14:08 +00:00
|
|
|
if strings.Contains(os.Getenv("MSYS"), "enable_pcon") {
|
2024-05-23 09:41:13 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setting the environment variable here unfortunately doesn't help,
|
|
|
|
// so we need to start a child process with "MSYS=enable_pcon"
|
|
|
|
// os.Setenv("MSYS", "enable_pcon")
|
|
|
|
return true
|
|
|
|
}
|
2024-05-23 12:14:08 +00:00
|
|
|
if opts.NoWinpty {
|
2024-05-23 09:41:13 +00:00
|
|
|
return false
|
|
|
|
}
|
2024-05-23 12:14:08 +00:00
|
|
|
if _, err := exec.LookPath("winpty"); err != nil {
|
2024-05-23 09:41:13 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2024-05-20 08:06:44 +00:00
|
|
|
func runWinpty(args []string, opts *Options) (int, error) {
|
|
|
|
sh, err := sh()
|
|
|
|
if err != nil {
|
|
|
|
return ExitError, err
|
|
|
|
}
|
|
|
|
|
|
|
|
argStr := escapeSingleQuote(args[0])
|
|
|
|
for _, arg := range args[1:] {
|
|
|
|
argStr += " " + escapeSingleQuote(arg)
|
|
|
|
}
|
2024-06-01 05:42:23 +00:00
|
|
|
argStr += ` --no-winpty`
|
2024-05-20 08:06:44 +00:00
|
|
|
|
2024-05-23 09:41:13 +00:00
|
|
|
if isMintty345() {
|
|
|
|
return runProxy(argStr, func(temp string) *exec.Cmd {
|
|
|
|
cmd := exec.Command(sh, temp)
|
|
|
|
cmd.Env = append(os.Environ(), "MSYS=enable_pcon")
|
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd
|
|
|
|
}, opts, false)
|
|
|
|
}
|
|
|
|
|
2024-05-20 08:06:44 +00:00
|
|
|
return runProxy(argStr, func(temp string) *exec.Cmd {
|
|
|
|
cmd := exec.Command(sh, "-c", fmt.Sprintf(`winpty < /dev/tty > /dev/tty -- sh %q`, temp))
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
return cmd
|
|
|
|
}, opts, false)
|
|
|
|
}
|