Skip to content
Myr

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

9 files+80 added−66 removed

modified:src/tui/light_unix.go+4 added-34 removed
5 unmodified lines
6 "errors"7 "os"8 "os/exec"9 "strings"10 "sync"10 "syscall"1112 "github.com/junegunn/fzf/src/util"13 "golang.org/x/sys/unix"14 "golang.org/x/term"15)1618var (19 tty string20 ttyin *os.File21 ttyout *os.File22 mutex sync.Mutex23)2417func IsLightRendererSupported() bool {18 return true19}20
23 unmodified lines
44 return nil45}4647func (r *LightRenderer) closePlatform() {56 // NOOP48 r.ttyout.Close()49}5051func 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 lines
62 return in, nil63}6465func openTtyIn() (*os.File, error) {76 mutex.Lock()77 defer mutex.Unlock()7879 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}6869func openTtyOut() (*os.File, error) {90 mutex.Lock()91 defer mutex.Unlock()9293 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}7273func (r *LightRenderer) setupTerminal() {74 term.MakeRaw(r.fd())
57 unmodified lines
modified:src/tui/light.go+2 added-6 removed
125 unmodified lines
126 fg Color127 bg Color128}129130func 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 }130func 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 lines
137 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 lines
modified:src/tui/tcell_test.go+2 added-1 removed
1 unmodified line
23package tui45import (6 "os"7 "testing"89 "github.com/gdamore/tcell/v2"10 "github.com/junegunn/fzf/src/util"
9 unmodified lines
20}2122// Test the handling of the tcell keyboard events.23func 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 unreadable
367 unmodified lines
modified:src/tui/ttyname_unix.go+18 added-7 removed
2 unmodified lines
3package tui45import (6 "os"7 "sync/atomic"8 "syscall"9)1011var devPrefixes = [...]string{"/dev/pts/", "/dev/"}1213var tty atomic.Value1415func ttyname() string {16 if cached := tty.Load(); cached != nil {17 return cached.(string)18 }1920 var stderr syscall.Stat_t21 if syscall.Fstat(2, &stderr) != nil {22 return ""23 }
9 unmodified lines
33 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}4537// TtyIn returns terminal device to be used as STDIN, falls back to os.Stdin38func TtyIn() *os.File {39 if in, err := openTtyIn(); err == nil {40 return in41 }42 return os.Stdin46// TtyIn returns terminal device to read user input47func TtyIn() (*os.File, error) {48 return openTtyIn()49}5051// TtyIn returns terminal device to write to52func TtyOut() (*os.File, error) {53 return openTtyOut()54}
modified:src/tui/ttyname_windows.go+10 added-3 removed
1//go:build windows23package tui45import "os"5import (6 "os"7)89func ttyname() string {10 return ""11}1213// TtyIn on Windows returns os.Stdin12func TtyIn() *os.File {13 return os.Stdin14func TtyIn() (*os.File, error) {15 return os.Stdin, nil16}1718// TtyIn on Windows returns nil19func TtyOut() (*os.File, error) {20 return nil, nil21}
modified:src/util/util.go+3 added-8 removed
136 unmodified lines
137 }138 return val139}140141// IsTty returns true if stdin is a terminal142func IsTty() bool {143 fd := os.Stdin.Fd()141// IsTty returns true if the file is a terminal142func IsTty(file *os.File) bool {143 fd := file.Fd()144 return isatty.IsTerminal(fd) || isatty.IsCygwinTerminal(fd)145}146147// ToTty returns true if stdout is a terminal148func ToTty() bool {149 return isatty.IsTerminal(os.Stdout.Fd())150}151147// Once returns a function that returns the specified boolean value only once148func Once(nextResponse bool) func() bool {149 state := nextResponse150 return func() bool {
68 unmodified lines
modified:src/proxy.go+6 added-2 removed
58 unmodified lines
59 }()6061 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 lines
130 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 lines
modified:src/reader.go+1 added-1 removed
111 unmodified lines
112 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 lines
modified:src/terminal.go+34 added-4 removed
49 unmodified lines
50var whiteSuffix *regexp.Regexp51var offsetComponentRegex *regexp.Regexp52var offsetTrimCharsRegex *regexp.Regexp53var passThroughRegex *regexp.Regexp54var ttyin *os.File5556const clearCode string = "\x1b[2J"5758// Number of maximum focus events to process synchronously
632 unmodified lines
691 }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 lines
719 }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 lines
826 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 lines
2882 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 }28932894 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 }29012902 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 }29092910 t.tui.Pause(true)2911 cmd.Run()2912 t.tui.Resume(true, false)2913 t.redraw()
1722 unmodified lines