Read input regardless of whether or not it's a TTY

This commit also contains some refactors:

* Refactor away inputStatus type
* Refactor away program.inputIsTTY member
* Simplify how we setup and restore input when it's a TTY
This commit is contained in:
Christian Rocha
2021-07-29 16:47:13 -04:00
parent 9826251f08
commit 9f9b3aea96
5 changed files with 32 additions and 83 deletions

View File

@@ -3,7 +3,6 @@
package tea
import (
"errors"
"io"
"os"
@@ -11,39 +10,25 @@ import (
)
func (p *Program) initInput() error {
if !p.inputIsTTY {
return nil
// 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
}
// If input's a TTY this should always succeed.
f, ok := p.input.(*os.File)
if !ok {
return errInputIsNotAFile
}
c, err := console.ConsoleFromFile(f)
if err != nil {
return nil
}
p.console = c
return nil
}
// On unix systems, RestoreInput closes any TTYs we opened for input. Note that
// 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.
// 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.
func (p *Program) restoreInput() error {
if p.inputStatus == managedInput {
f, ok := p.input.(*os.File)
if !ok {
return errors.New("could not close input")
}
err := f.Close()
if err != nil {
return err
}
if p.console != nil {
return p.console.Close()
}
return nil
}