github.com/hs0210/hashicorp-terraform@v0.11.12-beta1/helper/wrappedreadline/wrappedreadline_unix.go (about)

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