Skip to content

Instantly share code, notes, and snippets.

@steinelu
Last active November 22, 2024 14:46
Show Gist options
  • Save steinelu/aa9a5f402b584bc967eb216e054ceefb to your computer and use it in GitHub Desktop.
Save steinelu/aa9a5f402b584bc967eb216e054ceefb to your computer and use it in GitHub Desktop.
Get Terminal size in golang
func consoleSize() (int, int) {
cmd := exec.Command("stty", "size")
cmd.Stdin = os.Stdin
out, err := cmd.Output()
if err != nil {
log.Fatal(err)
}
s := string(out)
s = strings.TrimSpace(s)
sArr := strings.Split(s, " ")
heigth, err := strconv.Atoi(sArr[0])
if err != nil {
log.Fatal(err)
}
width, err := strconv.Atoi(sArr[1])
if err != nil {
log.Fatal(err)
}
return heigth, width
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment