Last active
November 22, 2024 14:46
-
-
Save steinelu/aa9a5f402b584bc967eb216e054ceefb to your computer and use it in GitHub Desktop.
Get Terminal size in golang
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
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