okami.mom/ultraviolet
git clone https://git.nkp.pet/okami.mom/ultraviolet
e6a488b
upstream
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. 14C <- chan os.Signal 15 16f term.File 17sig chan os.Signal 18m 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 { 24if f == nil { 25panic ("no file set" ) 26 } 27sig := make (chan os.Signal ) 28return & SizeNotifier { 29f :f , 30sig :sig , 31C :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 { 38return n .start () 39} 40 41// Stop stops the notifier and cleans up resources. 42func (n * SizeNotifier )Stop ()error { 43return n .stop () 44} 45 46// GetWindowSize returns the current size of the terminal window. 47func (n * SizeNotifier )GetWindowSize () (cells Size ,pixels Size ,err error ) { 48return n .getWindowSize () 49} 50 51// GetSize returns the current cell size of the terminal window. 52func (n * SizeNotifier )GetSize () (width ,height int ,err error ) { 53n .m .Lock () 54defer n .m .Unlock () 55 56width ,height ,err = term .GetSize (n .f .Fd ()) 57if err != nil { 58return 0 ,0 ,err //nolint:wrapcheck 59 } 60 61return width ,height ,nil 62}