2022-07-29 09:21:27 -04:00
|
|
|
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || aix
|
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris aix
|
2020-01-25 01:15:29 -05:00
|
|
|
|
2020-05-25 19:26:40 -04:00
|
|
|
package tea
|
2020-01-25 01:15:29 -05:00
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
import (
|
2023-07-06 16:20:36 -04:00
|
|
|
"fmt"
|
2021-02-26 18:38:52 -05:00
|
|
|
"os"
|
|
|
|
|
2024-01-09 17:20:23 -05:00
|
|
|
"golang.org/x/term"
|
2021-02-26 18:38:52 -05:00
|
|
|
)
|
|
|
|
|
2024-01-09 17:20:23 -05:00
|
|
|
func (p *Program) initInput() (err error) {
|
|
|
|
// Check if input is a terminal
|
|
|
|
if f, ok := p.input.(*os.File); ok && term.IsTerminal(int(f.Fd())) {
|
|
|
|
p.tty = f
|
|
|
|
p.ttyState, err = term.MakeRaw(int(p.tty.Fd()))
|
2021-07-29 16:47:13 -04:00
|
|
|
if err != nil {
|
2024-01-09 17:20:23 -05:00
|
|
|
return fmt.Errorf("error entering raw mode: %w", err)
|
2021-07-29 16:47:13 -04:00
|
|
|
}
|
2021-02-26 18:38:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func openInputTTY() (*os.File, error) {
|
|
|
|
f, err := os.Open("/dev/tty")
|
|
|
|
if err != nil {
|
2023-07-06 16:20:36 -04:00
|
|
|
return nil, fmt.Errorf("could not open a new TTY: %w", err)
|
2021-02-26 18:38:52 -05:00
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|