forked from charmbracelet/bubbletea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signals_unix.go
45 lines (37 loc) · 876 Bytes
/
signals_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
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{}) {
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGWINCH)
defer func() {
signal.Stop(sig)
close(done)
}()
for {
select {
case <-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}:
}
}
}