Simplify how we listen for window resizes

This commit is contained in:
Christian Rocha 2021-07-29 17:42:03 -04:00
parent 01dd88fd1d
commit 4bfad658ad
1 changed files with 5 additions and 16 deletions

19
tea.go
View File

@ -94,7 +94,6 @@ type Program struct {
// is on by default. // is on by default.
CatchPanics bool CatchPanics bool
outputIsTTY bool
console console.Console console console.Console
// Stores the original reference to stdin for cases where input is not a // Stores the original reference to stdin for cases where input is not a
@ -256,22 +255,11 @@ func (p *Program) Start() error {
var ( var (
cmds = make(chan Cmd) cmds = make(chan Cmd)
errs = make(chan error) errs = make(chan error)
// If output is a file (e.g. os.Stdout) then this will be set
// accordingly. Most of the time you should refer to p.outputIsTTY
// rather than do a nil check against the value here.
outputAsFile *os.File
) )
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
// Is output a terminal?
if f, ok := p.output.(*os.File); ok {
outputAsFile = f
p.outputIsTTY = isatty.IsTerminal(f.Fd())
}
switch { switch {
case p.startupOptions.has(withInputTTY): case p.startupOptions.has(withInputTTY):
// Open a new TTY, by request // Open a new TTY, by request
@ -383,10 +371,11 @@ func (p *Program) Start() error {
}() }()
} }
if p.outputIsTTY { // Is output a terminal?
if f, ok := p.output.(*os.File); ok {
// Get initial terminal size // Get initial terminal size
go func() { go func() {
w, h, err := term.GetSize(int(outputAsFile.Fd())) w, h, err := term.GetSize(int(f.Fd()))
if err != nil { if err != nil {
errs <- err errs <- err
} }
@ -394,7 +383,7 @@ func (p *Program) Start() error {
}() }()
// Listen for window resizes // Listen for window resizes
go listenForResize(outputAsFile, p.msgs, errs) go listenForResize(f, p.msgs, errs)
} }
// Process commands // Process commands