Better logic in input checks

This commit is contained in:
Christian Rocha 2021-07-29 17:01:38 -04:00
parent 9f9b3aea96
commit 30fd7a8f20
1 changed files with 21 additions and 15 deletions

36
tea.go
View File

@ -272,28 +272,34 @@ func (p *Program) Start() error {
p.outputIsTTY = isatty.IsTerminal(f.Fd()) p.outputIsTTY = isatty.IsTerminal(f.Fd())
} }
// Open a new TTY, by request switch {
if p.startupOptions.has(withInputTTY) { case p.startupOptions.has(withInputTTY):
// Open a new TTY, by request
f, err := openInputTTY() f, err := openInputTTY()
if err != nil { if err != nil {
return err return err
} }
p.input = f p.input = f
}
// If input is not a terminal, and the user hasn't set a custom input, open case !p.startupOptions.has(withCustomInput):
// a TTY so we can capture input as normal. This will allow things to "just // If the user hasn't set a custom input, and input's not a terminal,
// work" in cases where data was piped or redirected into this application. // open a TTY so we can capture input as normal. This will allow things
if f, ok := p.input.(*os.File); ok { // to "just work" in cases where data was piped or redirected into this
inputIsTTY := isatty.IsTerminal(f.Fd()) // application.
f, isFile := p.input.(*os.File)
if !inputIsTTY && !p.startupOptions.has(withCustomInput) { if !isFile {
f, err := openInputTTY() break
if err != nil {
return err
}
p.input = f
} }
if isatty.IsTerminal(f.Fd()) {
break
}
f, err := openInputTTY()
if err != nil {
return err
}
p.input = f
} }
// Listen for SIGINT. Note that in most cases ^C will not send an // Listen for SIGINT. Note that in most cases ^C will not send an