blob: 85402ded73124bc83a1787677992e5ef560d5f0e [file] [log] [blame]
Todd Wang092395e2014-10-29 16:15:27 -07001package textutil
2
3import (
4 "syscall"
5 "unsafe"
6)
7
8// TerminalSize returns the dimensions of the terminal, if it's available from
9// the OS, otherwise returns an error.
10func TerminalSize() (row, col int, _ error) {
11 // Try getting the terminal size from stdout, stderr and stdin respectively.
12 // We try each of these in turn because the mechanism we're using fails if any
13 // of the fds is redirected on the command line. E.g. "tool | less" redirects
14 // the stdout of tool to the stdin of less, and will mean tool cannot retrieve
15 // the terminal size from stdout.
16 //
17 // TODO(toddw): This probably only works on some linux / unix variants; add
18 // build tags and support different platforms.
19 if row, col, err := terminalSize(syscall.Stdout); err == nil {
20 return row, col, err
21 }
22 if row, col, err := terminalSize(syscall.Stderr); err == nil {
23 return row, col, err
24 }
25 return terminalSize(syscall.Stdin)
26}
27
28func terminalSize(fd int) (int, int, error) {
29 var ws winsize
30 if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&ws))); err != 0 {
31 return 0, 0, err
32 }
33 return int(ws.row), int(ws.col), nil
34}
35
36// winsize must correspond to the struct defined in "sys/ioctl.h". Do not
37// export this struct; it's a platform-specific implementation detail.
38type winsize struct {
39 row, col, xpixel, ypixel uint16
40}