This is how one can get a (POSIX) terminal size with a syscall in go:
func getTermDim() (width, height int, err error) {
var termDim [4]uint16
if _, _, err := syscall.Syscall6(syscall.SYS_IOCTL, uintptr(0), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(&termDim)), 0, 0, 0); err != 0 {
return -1, -1, err
}
return int(termDim[1]), int(termDim[0]), nil
}
Now, the same thing, calling stty with os/exec:
func getTermDim() (width, height int, err error) {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
var termDim []byte
if termDim, err = cmd.Output(); err != nil {
return
}
fmt.Sscan(string(termDim), &height, &width)
return
}
In practice, the first solution can get pretty heavy and unreadable, when one has to put a terminal in raw mode, set up options etc. When one is used to stty (e.g. in shell scripts), the second solution is so much easier!
So my question is: what are the benefits of using the first solution? It is speed? Is it that we cannot rely on the stty command to be installed on the host machine? Anything else I don't think of?
In a nutshell, what is the "risk" or the "cost" of using stty vs a syscall?
ioctl
interface (in Go-style, of course), and thengetTermDim()
would call that one. – Quitclaimstty
does and hence calling it instead or doing what it's supposed to do directly is just cruft. For those used to shell scripting callingstty
is supposedly more logical :-) – Quitclaimstty
's output and rely on it to be stable across different possible platforms (which I doubt). – QuitclaimSIGWINCH
signal. It might make sense to handle it as well. – Quitclaim