execute: Open separate handles to /dev/tty (in, out, err)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
# This will no longer cause 'Vim: Warning: Output is not to a terminal' fzf --bind 'enter:execute:vim {}' > /tmp/foo
authored by Junegunn Choi · committed by Junegunn Choi · parent d4216b0 · Browse files · 9 files·+80 added−66 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
9 files+80 added−66 removed
- other:+80 added−66 removed
Provenance
- Commit
3dee877- Tree
1c39e76- Author
Junegunn Choi <junegunn.c@gmail.com>- Committer
Junegunn Choi <junegunn.c@gmail.com>- Parent
d4216b0
9 files changed+80 added−66 removed
modified:src/tui/light_unix.go+4 added-34 removed
5 unmodified lines6 "errors"7 "os"8 "os/exec"9 "strings"10- "sync"10 "syscall"11 12 "github.com/junegunn/fzf/src/util"13 "golang.org/x/sys/unix"14 "golang.org/x/term"15 )16 18-var (19- tty string20- ttyin *os.File21- ttyout *os.File22- mutex sync.Mutex23-)24-17 func IsLightRendererSupported() bool {18 return true19 }20 23 unmodified lines44 return nil45 }46 47 func (r *LightRenderer) closePlatform() {56- // NOOP48+ r.ttyout.Close()49 }50 51 func openTty(mode int) (*os.File, error) {52 in, err := os.OpenFile(consoleDevice, mode, 0)53 if err != nil {62- if len(tty) == 0 {63- tty = ttyname()54+ tty := ttyname()64- }55 if len(tty) > 0 {56 if in, err := os.OpenFile(tty, mode, 0); err == nil {57 return in, nil58 }3 unmodified lines62 return in, nil63 }64 65 func openTtyIn() (*os.File, error) {76- mutex.Lock()77- defer mutex.Unlock()78-79- if ttyin != nil {80- return ttyin, nil81- }82- in, err := openTty(syscall.O_RDONLY)66+ return openTty(syscall.O_RDONLY)83- if err == nil {84- ttyin = in85- }86- return in, err67 }68 69 func openTtyOut() (*os.File, error) {90- mutex.Lock()91- defer mutex.Unlock()92-93- if ttyout != nil {94- return ttyout, nil95- }96- out, err := openTty(syscall.O_WRONLY)70+ return openTty(syscall.O_WRONLY)97- if err == nil {98- ttyout = out99- }100- return out, err71 }72 73 func (r *LightRenderer) setupTerminal() {74 term.MakeRaw(r.fd())57 unmodified linesmodified:src/tui/light.go+2 added-6 removed
125 unmodified lines126 fg Color127 bg Color128 }129 130-func NewLightRenderer(theme *ColorTheme, forceBlack bool, mouse bool, tabstop int, clearOnExit bool, fullscreen bool, maxHeightFunc func(int) int) (Renderer, error) {131- in, err := openTtyIn()132- if err != nil {133- return nil, err134- }130+func NewLightRenderer(ttyin *os.File, theme *ColorTheme, forceBlack bool, mouse bool, tabstop int, clearOnExit bool, fullscreen bool, maxHeightFunc func(int) int) (Renderer, error) {131 out, err := openTtyOut()132 if err != nil {133 out = os.Stderr134 }2 unmodified lines137 theme: theme,138 forceBlack: forceBlack,139 mouse: mouse,140 clearOnExit: clearOnExit,145- ttyin: in,141+ ttyin: ttyin,142 ttyout: out,143 yoffset: 0,144 tabstop: tabstop,145 fullscreen: fullscreen,1012 unmodified linesmodified:src/tui/tcell_test.go+2 added-1 removed
1 unmodified line2 3 package tui4 5 import (6+ "os"7 "testing"8 9 "github.com/gdamore/tcell/v2"10 "github.com/junegunn/fzf/src/util"9 unmodified lines20 }21 22 // Test the handling of the tcell keyboard events.23 func TestGetCharEventKey(t *testing.T) {23- if util.ToTty() {24+ if util.IsTty(os.Stdout) {25 // This test is skipped when output goes to terminal, because it causes26 // some glitches:27 // - output lines may not start at the beginning of a row which makes28 // the output unreadable367 unmodified linesmodified:src/tui/ttyname_unix.go+18 added-7 removed
2 unmodified lines3 package tui4 5 import (6 "os"7+ "sync/atomic"8 "syscall"9 )10 11 var devPrefixes = [...]string{"/dev/pts/", "/dev/"}12 13+var tty atomic.Value14+15 func ttyname() string {16+ if cached := tty.Load(); cached != nil {17+ return cached.(string)18+ }19+20 var stderr syscall.Stat_t21 if syscall.Fstat(2, &stderr) != nil {22 return ""23 }9 unmodified lines33 if err != nil {34 continue35 }36 if stat, ok := info.Sys().(*syscall.Stat_t); ok && stat.Rdev == stderr.Rdev {30- return prefix + file.Name()37+ value := prefix + file.Name()38+ tty.Store(value)39+ return value40 }41 }42 }43 return ""44 }45 37-// TtyIn returns terminal device to be used as STDIN, falls back to os.Stdin38-func TtyIn() *os.File {39- if in, err := openTtyIn(); err == nil {40- return in41- }42- return os.Stdin46+// TtyIn returns terminal device to read user input47+func TtyIn() (*os.File, error) {48+ return openTtyIn()49 }50+51+// TtyIn returns terminal device to write to52+func TtyOut() (*os.File, error) {53+ return openTtyOut()54+}modified:src/tui/ttyname_windows.go+10 added-3 removed
1 //go:build windows2 3 package tui4 5-import "os"5+import (6+ "os"7+)8 9 func ttyname() string {10 return ""11 }12 13 // TtyIn on Windows returns os.Stdin12-func TtyIn() *os.File {13- return os.Stdin14+func TtyIn() (*os.File, error) {15+ return os.Stdin, nil16 }17+18+// TtyIn on Windows returns nil19+func TtyOut() (*os.File, error) {20+ return nil, nil21+}modified:src/util/util.go+3 added-8 removed
136 unmodified lines137 }138 return val139 }140 141-// IsTty returns true if stdin is a terminal142-func IsTty() bool {143- fd := os.Stdin.Fd()141+// IsTty returns true if the file is a terminal142+func IsTty(file *os.File) bool {143+ fd := file.Fd()144 return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)145 }146 147-// ToTty returns true if stdout is a terminal148-func ToTty() bool {149- return isatty.IsTerminal(os.Stdout.Fd())150-}151-147 // Once returns a function that returns the specified boolean value only once148 func Once(nextResponse bool) func() bool {149 state := nextResponse150 return func() bool {68 unmodified linesmodified:src/proxy.go+6 added-2 removed
58 unmodified lines59 }()60 61 var command string62 commandPrefix += ` --proxy-script "$0"`63- if opts.Input == nil && util.IsTty() {63+ if opts.Input == nil && util.IsTty(os.Stdin) {64 command = fmt.Sprintf(`%s > %q`, commandPrefix, output)65 } else {66 input, err := fifo("proxy-input")67 if err != nil {62 unmodified lines130 if len(elems) > 1 {131 env = elems[1:]132 }133 executor := util.NewExecutor(opts.WithShell)134+ ttyin, err := tui.TtyIn()135+ if err != nil {136+ return ExitError, err137+ }134- executor.Become(tui.TtyIn(), env, command)138+ executor.Become(ttyin, env, command)139 }140 return code, err141 }142 }3 unmodified linesmodified:src/reader.go+1 added-1 removed
111 unmodified lines112 r.startEventPoller()113 var success bool114 if inputChan != nil {115 success = r.readChannel(inputChan)116- } else if util.IsTty() {116+ } else if util.IsTty(os.Stdin) {117 cmd := os.Getenv("FZF_DEFAULT_COMMAND")118 if len(cmd) == 0 {119 success = r.readFiles(root, opts, ignores)120 } else {158 unmodified linesmodified:src/terminal.go+34 added-4 removed
49 unmodified lines50 var whiteSuffix *regexp.Regexp51 var offsetComponentRegex *regexp.Regexp52 var offsetTrimCharsRegex *regexp.Regexp53 var passThroughRegex *regexp.Regexp54+var ttyin *os.File55 56 const clearCode string = "\x1b[2J"57 58 // Number of maximum focus events to process synchronously632 unmodified lines691 }692 var renderer tui.Renderer693 fullscreen := !opts.Height.auto && (opts.Height.size == 0 || opts.Height.percent && opts.Height.size == 100)694 var err error695+ // Reuse ttyin if available to avoid having multiple file descriptors open696+ // when you run fzf multiple times in your Go program. Closing it is known to697+ // cause problems with 'become' action and invalid terminal state after exit.698+ if ttyin == nil {699+ if ttyin, err = tui.TtyIn(); err != nil {700+ return nil, err701+ }702+ }703 if fullscreen {704 if tui.HasFullscreenRenderer() {705 renderer = tui.NewFullscreenRenderer(opts.Theme, opts.Black, opts.Mouse)706 } else {698- renderer, err = tui.NewLightRenderer(opts.Theme, opts.Black, opts.Mouse, opts.Tabstop, opts.ClearOnExit,707+ renderer, err = tui.NewLightRenderer(ttyin, opts.Theme, opts.Black, opts.Mouse, opts.Tabstop, opts.ClearOnExit,708 true, func(h int) int { return h })709 }710 } else {711 maxHeightFunc := func(termHeight int) int {7 unmodified lines719 }720 effectiveMinHeight += borderLines(opts.BorderShape)721 return util.Min(termHeight, util.Max(evaluateHeight(opts, termHeight), effectiveMinHeight))722 }714- renderer, err = tui.NewLightRenderer(opts.Theme, opts.Black, opts.Mouse, opts.Tabstop, opts.ClearOnExit, false, maxHeightFunc)723+ renderer, err = tui.NewLightRenderer(ttyin, opts.Theme, opts.Black, opts.Mouse, opts.Tabstop, opts.ClearOnExit, false, maxHeightFunc)724 }725 if err != nil {726 return nil, err727 }98 unmodified lines826 serverInputChan: make(chan []*action, 100),827 serverOutputChan: make(chan string),828 eventChan: make(chan tui.Event, 6), // (load + result + zero|one) | (focus) | (resize) | (GetChar)829 tui: renderer,821- ttyin: tui.TtyIn(),830+ ttyin: ttyin,831 initFunc: func() error { return renderer.Init() },832 executing: util.NewAtomicBool(false),833 lastAction: actStart,834 lastFocus: minItem.Index()}2047 unmodified lines2882 cmd := t.executor.ExecCommand(command, false)2883 cmd.Env = t.environ()2884 t.executing.Set(true)2885 if !background {2886+ // Open a separate handle for tty input2887+ if in, _ := tui.TtyIn(); in != nil {2877- cmd.Stdin = t.ttyin2888+ cmd.Stdin = in2889+ if in != os.Stdin {2890+ defer in.Close()2891+ }2892+ }2893+2894 cmd.Stdout = os.Stdout2895+ if !util.IsTty(os.Stdout) {2896+ if out, _ := tui.TtyOut(); out != nil {2897+ cmd.Stdout = out2898+ defer out.Close()2899+ }2900+ }2901+2902 cmd.Stderr = os.Stderr2903+ if !util.IsTty(os.Stderr) {2904+ if out, _ := tui.TtyOut(); out != nil {2905+ cmd.Stderr = out2906+ defer out.Close()2907+ }2908+ }2909+2910 t.tui.Pause(true)2911 cmd.Run()2912 t.tui.Resume(true, false)2913 t.redraw()1722 unmodified lines