2022-07-29 09:21:27 -04:00
|
|
|
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || aix
|
|
|
|
// +build darwin dragonfly freebsd linux netbsd openbsd solaris aix
|
2020-06-17 12:27:16 -04:00
|
|
|
|
|
|
|
package tea
|
|
|
|
|
|
|
|
import (
|
2021-09-28 13:30:11 -04:00
|
|
|
"context"
|
2020-06-17 12:27:16 -04:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
|
2021-04-29 08:24:25 -04:00
|
|
|
"golang.org/x/term"
|
2020-06-17 12:27:16 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// listenForResize sends messages (or errors) when the terminal resizes.
|
|
|
|
// Argument output should be the file descriptor for the terminal; usually
|
|
|
|
// os.Stdout.
|
2021-09-28 13:30:11 -04:00
|
|
|
func listenForResize(ctx context.Context, output *os.File, msgs chan Msg, errs chan error, done chan struct{}) {
|
2020-08-19 17:59:12 -04:00
|
|
|
sig := make(chan os.Signal, 1)
|
2020-06-17 12:27:16 -04:00
|
|
|
signal.Notify(sig, syscall.SIGWINCH)
|
2021-09-28 13:30:11 -04:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
signal.Stop(sig)
|
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2020-06-17 12:27:16 -04:00
|
|
|
for {
|
2021-09-28 13:30:11 -04:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case <-sig:
|
|
|
|
}
|
|
|
|
|
2021-04-29 08:24:25 -04:00
|
|
|
w, h, err := term.GetSize(int(output.Fd()))
|
2020-06-17 12:27:16 -04:00
|
|
|
if err != nil {
|
|
|
|
errs <- err
|
|
|
|
}
|
2021-09-28 13:30:11 -04:00
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return
|
|
|
|
case msgs <- WindowSizeMsg{w, h}:
|
|
|
|
}
|
2020-06-17 12:27:16 -04:00
|
|
|
}
|
|
|
|
}
|