mirror of
https://github.com/taigrr/bubbletea.git
synced 2026-04-02 02:59:09 -07:00
fix: detect terminal size after exec
Based on @knz's work in #499, but slightly supersedes this change. A little more coupling in the resize handling, but a lot less code & logic repetition. Co-authored-by: Raphael 'kena' Poss <knz@thaumogen.net>
This commit is contained in:
33
tty.go
33
tty.go
@@ -3,9 +3,12 @@ package tea
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
isatty "github.com/mattn/go-isatty"
|
||||
"github.com/muesli/cancelreader"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
func (p *Program) initTerminal() error {
|
||||
@@ -76,7 +79,10 @@ func (p *Program) readLoop() {
|
||||
msgs, err := readInputs(p.cancelReader)
|
||||
if err != nil {
|
||||
if !errors.Is(err, io.EOF) && !errors.Is(err, cancelreader.ErrCanceled) {
|
||||
p.errs <- err
|
||||
select {
|
||||
case <-p.ctx.Done():
|
||||
case p.errs <- err:
|
||||
}
|
||||
}
|
||||
|
||||
return
|
||||
@@ -98,3 +104,28 @@ func (p *Program) waitForReadLoop() {
|
||||
// though it was not able to cancel the read.
|
||||
}
|
||||
}
|
||||
|
||||
// checkResize detects the current size of the output and informs the program
|
||||
// via a WindowSizeMsg.
|
||||
func (p *Program) checkResize() {
|
||||
f, ok := p.output.TTY().(*os.File)
|
||||
if !ok || !isatty.IsTerminal(f.Fd()) {
|
||||
// can't query window size
|
||||
return
|
||||
}
|
||||
|
||||
w, h, err := term.GetSize(int(f.Fd()))
|
||||
if err != nil {
|
||||
select {
|
||||
case <-p.ctx.Done():
|
||||
case p.errs <- err:
|
||||
}
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
p.Send(WindowSizeMsg{
|
||||
Width: w,
|
||||
Height: h,
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user