2021-09-07 16:04:09 -04:00
|
|
|
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
|
2020-01-25 01:15:29 -05:00
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
|
|
|
|
|
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"
|
|
|
|
|
|
|
|
"github.com/containerd/console"
|
|
|
|
)
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
return p.console.Close()
|
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
|
|
|
|
}
|