Skip to content

Instantly share code, notes, and snippets.

@1player
Created December 18, 2014 09:45
Show Gist options
  • Save 1player/b7c85654cebb991f6c9c to your computer and use it in GitHub Desktop.
Save 1player/b7c85654cebb991f6c9c to your computer and use it in GitHub Desktop.
Get terminal size from Go
package main
import (
"fmt"
"os"
"syscall"
"time"
"unsafe"
)
type Winsize struct {
Height uint16
Width uint16
x uint16
y uint16
}
func getTermSize() (uint16, uint16) {
ws := &Winsize{}
_, _, err := syscall.Syscall(syscall.SYS_IOCTL, os.Stdout.Fd(), uintptr(syscall.TIOCGWINSZ), uintptr(unsafe.Pointer(ws)))
if err != 0 {
return 0, 0
}
return ws.Width, ws.Height
}
func main() {
for range time.Tick(250 * time.Millisecond) {
w, h := getTermSize()
fmt.Printf("%dx%d\n", w, h)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment