Refactor tui.TtyIn()

This commit is contained in:
Junegunn Choi 2024-05-15 00:28:56 +09:00
parent c4cc7891b4
commit 86d92c17c4
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627
3 changed files with 11 additions and 20 deletions

View File

@ -3471,16 +3471,6 @@ func (t *Terminal) Loop() error {
if t.history != nil { if t.history != nil {
t.history.append(string(t.input)) t.history.append(string(t.input))
} }
/*
FIXME: It is not at all clear why this is required.
The following command will report 'not a tty', unless we open
/dev/tty *twice* after closing the standard input for 'reload'
in Reader.terminate().
while : | fzf --bind 'start:reload:ls' --bind 'load:become:tty'; do echo; done
*/
tui.TtyIn()
t.executor.Become(tui.TtyIn(), t.environ(), command) t.executor.Become(tui.TtyIn(), t.environ(), command)
} }
case actExecute, actExecuteSilent: case actExecute, actExecuteSilent:

View File

@ -7,6 +7,7 @@ import (
"os" "os"
"os/exec" "os/exec"
"strings" "strings"
"sync"
"syscall" "syscall"
"github.com/junegunn/fzf/src/util" "github.com/junegunn/fzf/src/util"
@ -18,6 +19,7 @@ var (
tty string tty string
ttyin *os.File ttyin *os.File
ttyout *os.File ttyout *os.File
mutex sync.Mutex
) )
func IsLightRendererSupported() bool { func IsLightRendererSupported() bool {
@ -71,6 +73,9 @@ func openTty(mode int) (*os.File, error) {
} }
func openTtyIn() (*os.File, error) { func openTtyIn() (*os.File, error) {
mutex.Lock()
defer mutex.Unlock()
if ttyin != nil { if ttyin != nil {
return ttyin, nil return ttyin, nil
} }
@ -82,6 +87,9 @@ func openTtyIn() (*os.File, error) {
} }
func openTtyOut() (*os.File, error) { func openTtyOut() (*os.File, error) {
mutex.Lock()
defer mutex.Unlock()
if ttyout != nil { if ttyout != nil {
return ttyout, nil return ttyout, nil
} }

View File

@ -36,15 +36,8 @@ func ttyname() string {
// TtyIn returns terminal device to be used as STDIN, falls back to os.Stdin // TtyIn returns terminal device to be used as STDIN, falls back to os.Stdin
func TtyIn() *os.File { func TtyIn() *os.File {
in, err := os.OpenFile(consoleDevice, syscall.O_RDONLY, 0) if in, err := openTtyIn(); err == nil {
if err != nil { return in
tty := ttyname()
if len(tty) > 0 {
if in, err := os.OpenFile(tty, syscall.O_RDONLY, 0); err == nil {
return in
}
}
return os.Stdin
} }
return in return os.Stdin
} }