2018-08-07 16:31:52 -04:00
|
|
|
// +build !windows
|
|
|
|
|
2018-08-19 00:06:14 -04:00
|
|
|
package common
|
2018-08-07 16:31:52 -04:00
|
|
|
|
|
|
|
// Imports for determining terminal information across platforms
|
|
|
|
import (
|
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
// posix api
|
2018-08-19 00:06:14 -04:00
|
|
|
func platformGetTerminalDimensions() (width, height int, err error) {
|
|
|
|
|
|
|
|
// grab the handle to stdin
|
|
|
|
// XXX: in some cases, packer closes stdin, so the following can't be guaranteed
|
|
|
|
/*
|
|
|
|
tty := os.Stdin
|
|
|
|
*/
|
2018-08-07 16:31:52 -04:00
|
|
|
|
|
|
|
// open up a handle to the current tty
|
|
|
|
tty, err := os.Open("/dev/tty")
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
defer tty.Close()
|
|
|
|
|
|
|
|
// convert the handle into a file descriptor
|
|
|
|
fd := int(tty.Fd())
|
|
|
|
|
|
|
|
// use it to make an Ioctl
|
|
|
|
ws, err := unix.IoctlGetWinsize(fd, unix.TIOCGWINSZ)
|
|
|
|
if err != nil {
|
|
|
|
return 0, 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// return the width and height
|
|
|
|
return int(ws.Col), int(ws.Row), nil
|
|
|
|
}
|