github.com/tiagovtristao/plz@v13.4.0+incompatible/src/cli/window.go (about) 1 package cli 2 3 import ( 4 "fmt" 5 "runtime" 6 "syscall" 7 "unsafe" 8 ) 9 10 // For calculating the size of the console window; this is pretty important when we're writing 11 // arbitrary-length log messages around the interactive display. 12 type winsize struct { 13 Row uint16 14 Col uint16 15 Xpixel uint16 16 Ypixel uint16 17 } 18 19 // WindowSize finds and returns the size of the console window as (rows, columns) 20 func WindowSize() (int, int, error) { 21 ws := winsize{} 22 if ret, _, errno := syscall.Syscall(syscall.SYS_IOCTL, 23 uintptr(syscall.Stderr), 24 uintptr(tiocgwinsz()), 25 uintptr(unsafe.Pointer(&ws)), 26 ); int(ret) == -1 { 27 return 25, 80, fmt.Errorf("error %d getting window size", int(errno)) 28 } 29 return int(ws.Row), int(ws.Col), nil 30 } 31 32 // tiocgwinsz returns the ioctl number corresponding to TIOCGWINSZ. 33 // We could determine this using cgo which would be more robust, but I'd really 34 // rather not invoke cgo for something as static as this. 35 func tiocgwinsz() int { 36 if runtime.GOOS == "linux" { 37 return 0x5413 38 } 39 return 1074295912 // OSX and FreeBSD. 40 }