mirror of
https://github.com/junegunn/fzf.git
synced 2025-11-18 16:45:38 -05:00
Refactor tui.TtyIn()
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"github.com/junegunn/fzf/src/util"
|
||||
@@ -18,6 +19,7 @@ var (
|
||||
tty string
|
||||
ttyin *os.File
|
||||
ttyout *os.File
|
||||
mutex sync.Mutex
|
||||
)
|
||||
|
||||
func IsLightRendererSupported() bool {
|
||||
@@ -71,6 +73,9 @@ func openTty(mode int) (*os.File, error) {
|
||||
}
|
||||
|
||||
func openTtyIn() (*os.File, error) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
if ttyin != nil {
|
||||
return ttyin, nil
|
||||
}
|
||||
@@ -82,6 +87,9 @@ func openTtyIn() (*os.File, error) {
|
||||
}
|
||||
|
||||
func openTtyOut() (*os.File, error) {
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
if ttyout != nil {
|
||||
return ttyout, nil
|
||||
}
|
||||
|
||||
@@ -36,15 +36,8 @@ func ttyname() string {
|
||||
|
||||
// TtyIn returns terminal device to be used as STDIN, falls back to os.Stdin
|
||||
func TtyIn() *os.File {
|
||||
in, err := os.OpenFile(consoleDevice, syscall.O_RDONLY, 0)
|
||||
if err != nil {
|
||||
tty := ttyname()
|
||||
if len(tty) > 0 {
|
||||
if in, err := os.OpenFile(tty, syscall.O_RDONLY, 0); err == nil {
|
||||
return in
|
||||
}
|
||||
}
|
||||
return os.Stdin
|
||||
if in, err := openTtyIn(); err == nil {
|
||||
return in
|
||||
}
|
||||
return in
|
||||
return os.Stdin
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user