github.phpd.cn/hashicorp/packer@v1.3.2/common/terminal_posix.go (about)

     1  // +build !windows
     2  
     3  package common
     4  
     5  // Imports for determining terminal information across platforms
     6  import (
     7  	"golang.org/x/sys/unix"
     8  	"os"
     9  )
    10  
    11  // posix api
    12  func platformGetTerminalDimensions() (width, height int, err error) {
    13  
    14  	// grab the handle to stdin
    15  	// XXX: in some cases, packer closes stdin, so the following can't be guaranteed
    16  	/*
    17  		tty := os.Stdin
    18  	*/
    19  
    20  	// open up a handle to the current tty
    21  	tty, err := os.Open("/dev/tty")
    22  	if err != nil {
    23  		return 0, 0, err
    24  	}
    25  	defer tty.Close()
    26  
    27  	// convert the handle into a file descriptor
    28  	fd := int(tty.Fd())
    29  
    30  	// use it to make an Ioctl
    31  	ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
    32  	if err != nil {
    33  		return 0, 0, err
    34  	}
    35  
    36  	// return the width and height
    37  	return int(ws.Col), int(ws.Row), nil
    38  }