Created
December 18, 2014 09:45
-
-
Save 1player/b7c85654cebb991f6c9c to your computer and use it in GitHub Desktop.
Get terminal size from Go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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