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 (
|
|
|
|
"os"
|
|
|
|
|
2023-03-07 10:15:32 -05:00
|
|
|
"github.com/muesli/console"
|
2021-02-26 18:38:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func (p *Program) initInput() error {
|
2021-07-29 16:47:13 -04:00
|
|
|
// If input's a file, use console to manage it
|
|
|
|
if f, ok := p.input.(*os.File); ok {
|
|
|
|
c, err := console.ConsoleFromFile(f)
|
|
|
|
if err != nil {
|
2022-01-07 01:26:39 -05:00
|
|
|
return nil //nolint:nilerr // ignore error, this was just a test
|
2021-07-29 16:47:13 -04:00
|
|
|
}
|
|
|
|
p.console = c
|
2021-02-26 18:38:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// On unix systems, RestoreInput closes any TTYs we opened for input. Note that
|
2021-07-29 16:47:13 -04:00
|
|
|
// we don't do this on Windows as it causes the prompt to not be drawn until
|
|
|
|
// the terminal receives a keypress rather than appearing promptly after the
|
|
|
|
// program exits.
|
2021-02-26 18:38:52 -05:00
|
|
|
func (p *Program) restoreInput() error {
|
2021-07-29 16:47:13 -04:00
|
|
|
if p.console != nil {
|
2021-09-28 13:30:11 -04:00
|
|
|
return p.console.Reset()
|
2021-02-26 18:38:52 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func openInputTTY() (*os.File, error) {
|
|
|
|
f, err := os.Open("/dev/tty")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return f, nil
|
|
|
|
}
|