okami.mom/ultraviolet

git clone https://git.nkp.pet/okami.mom/ultraviolet

Ayman BagabasMerge pull request #52 from charmbracelet/refae6a488b

main
1.4 KiB62 linesraw
1package uv
2
3import (
4	"os"
5	"sync"
6
7	"github.com/charmbracelet/x/term"
8)
9
10// SizeNotifier represents a notifier that listens for window size
11// changes using the SIGWINCH signal and notifies the given channel.
12type SizeNotifier struct {
13	// Channel that receives terminal size change notifications.
14	C <-chan os.Signal
15
16	f   term.File
17	sig chan os.Signal
18	m   sync.Mutex
19}
20
21// NewSizeNotifier creates a new [SizeNotifier] that listens for window size
22// changes on the given TTY file through SIGWINCH signals.
23func NewSizeNotifier(f term.File) *SizeNotifier {
24	if f == nil {
25		panic("no file set")
26	}
27	sig := make(chan os.Signal)
28	return &SizeNotifier{
29		f:   f,
30		sig: sig,
31		C:   sig,
32	}
33}
34
35// Start starts listening for window size changes and notifies [SizeNotifier.C]
36// about any changes.
37func (n *SizeNotifier) Start() error {
38	return n.start()
39}
40
41// Stop stops the notifier and cleans up resources.
42func (n *SizeNotifier) Stop() error {
43	return n.stop()
44}
45
46// GetWindowSize returns the current size of the terminal window.
47func (n *SizeNotifier) GetWindowSize() (cells Size, pixels Size, err error) {
48	return n.getWindowSize()
49}
50
51// GetSize returns the current cell size of the terminal window.
52func (n *SizeNotifier) GetSize() (width, height int, err error) {
53	n.m.Lock()
54	defer n.m.Unlock()
55
56	width, height, err = term.GetSize(n.f.Fd())
57	if err != nil {
58		return 0, 0, err //nolint:wrapcheck
59	}
60
61	return width, height, nil
62}