Debian Codegraph @

src/winpty_windows.go

raw
1 //go:build windows
2
3 package fzf
4
5 import (
6 "fmt"
7 "os"
8 "os/exec"
9 "strings"
10
11 "github.com/junegunn/fzf/src/util"
12 )
13
14 func isMintty345() bool {
15 return util.CompareVersions(os.Getenv("TERM_PROGRAM_VERSION"), "3.4.5") >= 0
16 }
17
18 func needWinpty(opts *Options) bool {
19 if os.Getenv("TERM_PROGRAM") != "mintty" {
20 return false
21 }
22 if isMintty345() {
23 /*
24 See: https://github.com/junegunn/fzf/issues/3809
25
26 "MSYS=enable_pcon" allows fzf to run properly on mintty 3.4.5 or later.
27 */
28 if strings.Contains(os.Getenv("MSYS"), "enable_pcon") {
29 return false
30 }
31
32 // Setting the environment variable here unfortunately doesn't help,
33 // so we need to start a child process with "MSYS=enable_pcon"
34 // os.Setenv("MSYS", "enable_pcon")
35 return true
36 }
37 if opts.NoWinpty {
38 return false
39 }
40 if _, err := exec.LookPath("winpty"); err != nil {
41 return false
42 }
43 return true
44 }
45
46 func runWinpty(args []string, opts *Options) (int, error) {
47 argStr := escapeSingleQuote(args[0])
48 for _, arg := range args[1:] {
49 argStr += " " + escapeSingleQuote(arg)
50 }
51 argStr += ` --no-winpty`
52
53 if isMintty345() {
54 return runProxy(argStr, func(temp string, needBash bool) (*exec.Cmd, error) {
55 sh, err := sh(needBash)
56 if err != nil {
57 return nil, err
58 }
59
60 cmd := exec.Command(sh, temp)
61 cmd.Env = append(os.Environ(), "MSYS=enable_pcon")
62 cmd.Stdin = os.Stdin
63 cmd.Stdout = os.Stdout
64 cmd.Stderr = os.Stderr
65 return cmd, nil
66 }, opts, false)
67 }
68
69 return runProxy(argStr, func(temp string, needBash bool) (*exec.Cmd, error) {
70 sh, err := sh(needBash)
71 if err != nil {
72 return nil, err
73 }
74
75 cmd := exec.Command(sh, "-c", fmt.Sprintf(`winpty < /dev/tty > /dev/tty -- sh %q`, temp))
76 cmd.Stdout = os.Stdout
77 cmd.Stderr = os.Stderr
78 return cmd, nil
79 }, opts, false)
80 }