2
0
mirror of https://github.com/junegunn/fzf synced 2024-11-10 13:10:44 +00:00
fzf/src/tui/ttyname_unix.go

55 lines
923 B
Go
Raw Normal View History

2022-03-29 12:20:33 +00:00
//go:build !windows
package tui
import (
"os"
"sync/atomic"
"syscall"
)
var devPrefixes = [...]string{"/dev/pts/", "/dev/"}
var tty atomic.Value
func ttyname() string {
if cached := tty.Load(); cached != nil {
return cached.(string)
}
var stderr syscall.Stat_t
if syscall.Fstat(2, &stderr) != nil {
return ""
}
for _, prefix := range devPrefixes {
2023-07-16 08:14:22 +00:00
files, err := os.ReadDir(prefix)
if err != nil {
continue
}
for _, file := range files {
2023-07-16 08:14:22 +00:00
info, err := file.Info()
if err != nil {
continue
}
if stat, ok := info.Sys().(*syscall.Stat_t); ok && stat.Rdev == stderr.Rdev {
value := prefix + file.Name()
tty.Store(value)
return value
}
}
}
return ""
}
// TtyIn returns terminal device to read user input
func TtyIn() (*os.File, error) {
return openTtyIn()
}
// TtyIn returns terminal device to write to
func TtyOut() (*os.File, error) {
return openTtyOut()
}