github.com/jaredpalmer/terraform@v1.1.0-alpha20210908.0.20210911170307-88705c943a03/internal/helper/wrappedreadline/wrappedreadline_unix.go (about) 1 //go:build darwin || dragonfly || freebsd || (linux && !appengine) || netbsd || openbsd 2 // +build darwin dragonfly freebsd linux,!appengine netbsd openbsd 3 4 package wrappedreadline 5 6 import ( 7 "syscall" 8 "unsafe" 9 10 "github.com/hashicorp/terraform/internal/helper/wrappedstreams" 11 ) 12 13 // getWidth impl for Unix 14 func getWidth() int { 15 stdoutFd := int(wrappedstreams.Stdout().Fd()) 16 stderrFd := int(wrappedstreams.Stderr().Fd()) 17 18 w := getWidthFd(stdoutFd) 19 if w < 0 { 20 w = getWidthFd(stderrFd) 21 } 22 23 return w 24 } 25 26 type winsize struct { 27 Row uint16 28 Col uint16 29 Xpixel uint16 30 Ypixel uint16 31 } 32 33 // get width of the terminal 34 func getWidthFd(stdoutFd int) int { 35 ws := &winsize{} 36 retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL, 37 uintptr(stdoutFd), 38 uintptr(syscall.TIOCGWINSZ), 39 uintptr(unsafe.Pointer(ws))) 40 41 if int(retCode) == -1 { 42 _ = errno 43 return -1 44 } 45 46 return int(ws.Col) 47 }