Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: properly detect terminal size after exec #499

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 1 addition & 12 deletions signals_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import (
"os"
"os/signal"
"syscall"

"golang.org/x/term"
)

// listenForResize sends messages (or errors) when the terminal resizes.
Expand All @@ -31,15 +29,6 @@ func listenForResize(ctx context.Context, output *os.File, msgs chan Msg, errs c
case <-sig:
}

w, h, err := term.GetSize(int(output.Fd()))
if err != nil {
errs <- err
}

select {
case <-ctx.Done():
return
case msgs <- WindowSizeMsg{w, h}:
}
checkResize(ctx, output, msgs, errs)
}
}
43 changes: 30 additions & 13 deletions tea.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,17 +310,7 @@ func (p *Program) StartReturningModel() (Model, error) {

if f, ok := p.output.TTY().(*os.File); ok && isatty.IsTerminal(f.Fd()) {
// Get the initial terminal size and send it to the program.
go func() {
w, h, err := term.GetSize(int(f.Fd()))
if err != nil {
p.errs <- err
}

select {
case <-p.ctx.Done():
case p.msgs <- WindowSizeMsg{w, h}:
}
}()
go checkResize(p.ctx, f, p.msgs, p.errs)

// Listen for window resizes.
go listenForResize(p.ctx, f, p.msgs, p.errs, resizeLoopDone)
Expand Down Expand Up @@ -451,7 +441,10 @@ func (p *Program) Start() error {
// If the program is not running this this will be a no-op, so it's safe to
// send messages if the program is unstarted, or has exited.
func (p *Program) Send(msg Msg) {
p.msgs <- msg
select {
case <-p.ctx.Done():
case p.msgs <- msg:
}
}

// Quit is a convenience function for quitting Bubble Tea programs. Use it
Expand Down Expand Up @@ -525,11 +518,35 @@ func (p *Program) RestoreTerminal() error {
p.EnterAltScreen()
}

go p.Send(repaintMsg{})
go func() {
// If the output is a terminal, it may have been resized while
// another process was at the foreground, in which case we may not
// have received SIGWINCH. Detect any size change now and
// propagate the new size as needed.
if f, ok := p.output.TTY().(*os.File); ok && isatty.IsTerminal(f.Fd()) {
checkResize(p.ctx, f, p.msgs, p.errs)
}
p.Send(repaintMsg{})
}()

return nil
}

// checkResize detects the current size of the output and informs
// the program via a WindowSizeMsg.
func checkResize(ctx context.Context, output *os.File, msgs chan Msg, errs chan error) {
w, h, err := term.GetSize(int(output.Fd()))
if err != nil {
errs <- err
return
}

select {
case <-ctx.Done():
case msgs <- WindowSizeMsg{w, h}:
}
}

// Println prints above the Program. This output is unmanaged by the program
// and will persist across renders by the Program.
//
Expand Down