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:
Christian Muehlhaeuser
2022-10-15 04:50:01 +02:00
parent 9bcfc026a2
commit 80f44c9384
4 changed files with 44 additions and 37 deletions

View File

@@ -4,18 +4,15 @@
package tea
import (
"context"
"os"
"os/signal"
"syscall"
"golang.org/x/term"
)
// listenForResize sends messages (or errors) when the terminal resizes.
// Argument output should be the file descriptor for the terminal; usually
// os.Stdout.
func listenForResize(ctx context.Context, output *os.File, msgs chan Msg, errs chan error, done chan struct{}) {
func (p *Program) listenForResize(done chan struct{}) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGWINCH)
@@ -26,20 +23,11 @@ func listenForResize(ctx context.Context, output *os.File, msgs chan Msg, errs c
for {
select {
case <-ctx.Done():
case <-p.ctx.Done():
return
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}:
}
p.checkResize()
}
}