gunk-dev/the-valley

winsize.go

source646a1ec1c1bamain's published tip history
453 bytes
1//go:build linux || darwin
2
3package main
4
5import (
6 "os"
7 "syscall"
8 "unsafe"
9)
10
11// windowColumns asks the terminal f is open on how many columns wide it
12// is. It reports false when f is no terminal.
13func windowColumns(f *os.File) (int, bool) {
14 var ws struct{ rows, cols, xpixels, ypixels uint16 }
15 _, _, errno := syscall.Syscall(syscall.SYS_IOCTL, f.Fd(), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&ws)))
16 return int(ws.cols), errno == 0
17}