signals_unix.go 651 B

123456789101112131415161718192021222324252627282930313233
  1. //go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris || aix
  2. // +build darwin dragonfly freebsd linux netbsd openbsd solaris aix
  3. package tea
  4. import (
  5. "os"
  6. "os/signal"
  7. "syscall"
  8. )
  9. // listenForResize sends messages (or errors) when the terminal resizes.
  10. // Argument output should be the file descriptor for the terminal; usually
  11. // os.Stdout.
  12. func (p *Program) listenForResize(done chan struct{}) {
  13. sig := make(chan os.Signal, 1)
  14. signal.Notify(sig, syscall.SIGWINCH)
  15. defer func() {
  16. signal.Stop(sig)
  17. close(done)
  18. }()
  19. for {
  20. select {
  21. case <-p.ctx.Done():
  22. return
  23. case <-sig:
  24. }
  25. p.checkResize()
  26. }
  27. }