summaryrefslogtreecommitdiff
path: root/internal/cli/termwidth_unix.go
blob: 9d1b47d470f9951eb151ba9bb52bc34c122ae11f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
//go:build unix

package cli

import (
	"os"
	"syscall"
	"unsafe"
)

// ttyWidth returns f's terminal column width via the TIOCGWINSZ ioctl, or 0
// if f isn't a terminal (redirected to a file/pipe, as in tests) or the
// ioctl fails.
func ttyWidth(f *os.File) int {
	var ws struct {
		Row, Col, Xpixel, Ypixel uint16
	}
	_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&ws)))
	if errno != 0 || ws.Col == 0 {
		return 0
	}
	return int(ws.Col)
}