terminal_unix.go

 1//go:build !windows
 2
 3package view
 4
 5import "golang.org/x/sys/unix"
 6
 7// getCellHeightFromFd attempts to get the terminal cell height from a file descriptor.
 8// Returns 0 if it fails or if pixel dimensions are not available.
 9func getCellHeightFromFd(fd int) int {
10	ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
11	if err != nil {
12		return 0
13	}
14
15	// ws.Row = number of character rows
16	// ws.Ypixel = height in pixels
17	// Some terminals don't report pixel dimensions (return 0)
18	if ws.Row > 0 && ws.Ypixel > 0 {
19		cellHeight := int(ws.Ypixel) / int(ws.Row)
20		if cellHeight > 0 {
21			debugImageProtocol("terminal cell height: %d pixels (rows=%d, ypixel=%d, fd=%d)", cellHeight, ws.Row, ws.Ypixel, fd)
22			return cellHeight
23		}
24	}
25
26	// Terminal reported dimensions but no pixel info - this is common
27	if ws.Row > 0 && ws.Ypixel == 0 {
28		debugImageProtocol("terminal fd=%d has rows=%d but no pixel info (ypixel=0)", fd, ws.Row)
29	}
30
31	return 0
32}