mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
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:
39
tty_unix.go
39
tty_unix.go
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user