summaryrefslogtreecommitdiff
path: root/src/proxy_windows.go
diff options
context:
space:
mode:
authorJunegunn Choi <junegunn.c@gmail.com>2024-05-20 17:06:44 +0900
committerJunegunn Choi <junegunn.c@gmail.com>2024-05-20 18:24:14 +0900
commit573df524fed1c493ce7d8ea893f06ab90f2ca18a (patch)
tree0bd1185bf827de5860aaf6a4944c2fd1c8ed69ef /src/proxy_windows.go
parentaee417c46a2f6d2aa87ea3fcc799fdc7bc830dfe (diff)
downloadfzf-573df524fed1c493ce7d8ea893f06ab90f2ca18a.tar.gz
Use winpty to launch fzf in Git bash (mintty)
Close #3806 Known limitation: * --height cannot be used
Diffstat (limited to 'src/proxy_windows.go')
-rw-r--r--src/proxy_windows.go81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/proxy_windows.go b/src/proxy_windows.go
new file mode 100644
index 00000000..a957da8a
--- /dev/null
+++ b/src/proxy_windows.go
@@ -0,0 +1,81 @@
+//go:build windows
+
+package fzf
+
+import (
+ "fmt"
+ "io"
+ "os/exec"
+ "strconv"
+ "strings"
+ "sync/atomic"
+)
+
+var shPath atomic.Value
+
+func sh() (string, error) {
+ if cached := shPath.Load(); cached != nil {
+ return cached.(string), nil
+ }
+
+ cmd := exec.Command("cygpath", "-w", "/usr/bin/sh")
+ bytes, err := cmd.Output()
+ if err != nil {
+ return "", err
+ }
+
+ sh := strings.TrimSpace(string(bytes))
+ shPath.Store(sh)
+ return sh, nil
+}
+
+func mkfifo(path string, mode uint32) (string, error) {
+ m := strconv.FormatUint(uint64(mode), 8)
+ sh, err := sh()
+ if err != nil {
+ return path, err
+ }
+ cmd := exec.Command(sh, "-c", fmt.Sprintf(`command mkfifo -m %s %q`, m, path))
+ if err := cmd.Run(); err != nil {
+ return path, err
+ }
+ return path + ".lnk", nil
+}
+
+func withOutputPipe(output string, task func(io.ReadCloser)) error {
+ sh, err := sh()
+ if err != nil {
+ return err
+ }
+ cmd := exec.Command(sh, "-c", fmt.Sprintf(`command cat %q`, output))
+ outputFile, err := cmd.StdoutPipe()
+ if err != nil {
+ return err
+ }
+ if err := cmd.Start(); err != nil {
+ return err
+ }
+
+ task(outputFile)
+ cmd.Wait()
+ return nil
+}
+
+func withInputPipe(input string, task func(io.WriteCloser)) error {
+ sh, err := sh()
+ if err != nil {
+ return err
+ }
+ cmd := exec.Command(sh, "-c", fmt.Sprintf(`command cat - > %q`, input))
+ inputFile, err := cmd.StdinPipe()
+ if err != nil {
+ return err
+ }
+ if err := cmd.Start(); err != nil {
+ return err
+ }
+ task(inputFile)
+ inputFile.Close()
+ cmd.Wait()
+ return nil
+}