gitstack

grindlemire/go-tui code browser

821 B Go 41 lines 2026-03-18 · 60730a0 raw
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
//go:build !windows

package tui

import (
	"os"
	"os/signal"
	"syscall"
)

// registerResizeSignal sets up a SIGWINCH handler to dispatch resize events
// when the terminal window is resized. Returns a cleanup function.
func (a *App) registerResizeSignal() func() {
	resizeCh := make(chan os.Signal, 10)
	signal.Notify(resizeCh, syscall.SIGWINCH)

	go func() {
		for {
			select {
			case <-resizeCh:
				w, h := a.terminal.Size()
				ev := ResizeEvent{Width: w, Height: h}
				// Wake blocking reader so the resize event is processed promptly
				if interruptible, ok := a.reader.(InterruptibleReader); ok {
					interruptible.Interrupt()
				}
				select {
				case a.inputEvents <- ev:
				case <-a.stopCh:
					return
				}
			case <-a.stopCh:
				return
			}
		}
	}()

	return func() {
		signal.Stop(resizeCh)
	}
}