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"
|
|
|
|
|
|
|
|
"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 {
|
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 {
|
2023-07-06 16:20:36 -04:00
|
|
|
if err := p.console.Reset(); err != nil {
|
|
|
|
return fmt.Errorf("error restoring console: %w", err)
|
|
|
|
}
|
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
|
|
|
|
}
|