Add WithInputTTY ProgramOption

This commit is contained in:
Christian Rocha 2021-07-29 15:43:03 -04:00
parent e87344b791
commit 9826251f08
2 changed files with 21 additions and 0 deletions

View File

@ -28,6 +28,13 @@ func WithInput(input io.Reader) ProgramOption {
} }
} }
// WithInputTTY open a new TTY for input (or console input device on Windows).
func WithInputTTY() ProgramOption {
return func(p *Program) {
p.startupOptions |= withInputTTY
}
}
// WithoutCatchPanics disables the panic catching that Bubble Tea does by // WithoutCatchPanics disables the panic catching that Bubble Tea does by
// default. If panic catching is disabled the terminal will be in a fairly // default. If panic catching is disabled the terminal will be in a fairly
// unusable state after a panic because Bubble Tea will not perform its usual // unusable state after a panic because Bubble Tea will not perform its usual

14
tea.go
View File

@ -58,11 +58,16 @@ type Cmd func() Msg
// The options here are treated as bits. // The options here are treated as bits.
type startupOptions byte type startupOptions byte
func (s startupOptions) has(option startupOptions) bool {
return s&option != 0
}
// Available startup options. // Available startup options.
const ( const (
withAltScreen startupOptions = 1 << iota withAltScreen startupOptions = 1 << iota
withMouseCellMotion withMouseCellMotion
withMouseAllMotion withMouseAllMotion
withInputTTY
) )
// inputStatus indicates the current state of the input. By default, input is // inputStatus indicates the current state of the input. By default, input is
@ -297,6 +302,15 @@ func (p *Program) Start() error {
p.outputIsTTY = isatty.IsTerminal(f.Fd()) p.outputIsTTY = isatty.IsTerminal(f.Fd())
} }
// Open a new TTY, by request
if p.startupOptions.has(withInputTTY) {
f, err := openInputTTY()
if err != nil {
return err
}
p.input = f
}
// Is input a terminal? // Is input a terminal?
if f, ok := p.input.(*os.File); ok { if f, ok := p.input.(*os.File); ok {
p.inputIsTTY = isatty.IsTerminal(f.Fd()) p.inputIsTTY = isatty.IsTerminal(f.Fd())