chore: make input options mutually exclusive

This commit is contained in:
Christian Rocha
2023-05-05 14:55:25 -04:00
parent 25022e9789
commit fcc805f3da
3 changed files with 56 additions and 16 deletions

32
tea.go
View File

@@ -60,6 +60,24 @@ type Cmd func() Msg
type handlers []chan struct{}
type inputType int
const (
defaultInput inputType = iota
ttyInput
customInput
)
// String implements the stringer interface for [inputType]. It is inteded to
// be used in testing.
func (i inputType) String() string {
return [...]string{
"default input",
"tty input",
"custom input",
}[i]
}
// Options to customize the program during its initialization. These are
// generally set with ProgramOptions.
//
@@ -74,8 +92,6 @@ const (
withAltScreen startupOptions = 1 << iota
withMouseCellMotion
withMouseAllMotion
withInputTTY
withCustomInput
withANSICompressor
withoutSignalHandler
@@ -94,6 +110,8 @@ type Program struct {
// treated as bits. These options can be set via various ProgramOptions.
startupOptions startupOptions
inputType inputType
ctx context.Context
cancel context.CancelFunc
@@ -141,7 +159,6 @@ type QuitMsg struct{}
func NewProgram(model Model, opts ...ProgramOption) *Program {
p := &Program{
initialModel: model,
input: os.Stdin,
msgs: make(chan Msg),
}
@@ -371,8 +388,11 @@ func (p *Program) Run() (Model, error) {
defer p.cancel()
switch {
case p.startupOptions.has(withInputTTY):
switch p.inputType {
case defaultInput:
p.input = os.Stdin
case ttyInput:
// Open a new TTY, by request
f, err := openInputTTY()
if err != nil {
@@ -381,7 +401,7 @@ func (p *Program) Run() (Model, error) {
defer f.Close() //nolint:errcheck
p.input = f
case !p.startupOptions.has(withCustomInput):
case customInput:
// If the user hasn't set a custom input, and input's not a terminal,
// open a TTY so we can capture input as normal. This will allow things
// to "just work" in cases where data was piped or redirected into this