From 5ac6a72cf9be60addc5872a869669f86e08c7e36 Mon Sep 17 00:00:00 2001 From: Rose Hogenson Date: Fri, 1 Sep 2023 13:02:19 -0700 Subject: Detect the terminal size with a syscall. This will only work on Linux. --- mandelbrot.go | 28 +++++++++++++++++++++++----- 1 file changed, 23 insertions(+), 5 deletions(-) diff --git a/mandelbrot.go b/mandelbrot.go index 482850a..686625b 100644 --- a/mandelbrot.go +++ b/mandelbrot.go @@ -2,8 +2,23 @@ package main import ( "fmt" + "os" + "syscall" + "unsafe" ) +func terminalSize() (cols int, lines int, err error) { + const tiocgwinsz uintptr = 21523 + + ts := new(struct { + wsRow, wsCol, wsXPixel, wsYPixel uint16 + }) + if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, 1, tiocgwinsz, uintptr(unsafe.Pointer(ts))); err != 0 { + return 0, 0, err + } + return int(ts.wsCol), int(ts.wsRow), nil +} + func mandelbrot(x0, y0 float64) int { var x, y, x2, y2 float64 for i := 0; i < 1000; i++ { @@ -24,13 +39,16 @@ func main() { xRange = 2.47 yMin = -1.12 yRange = 2.24 + ) - width = 80 - height = 25 + width, height, err := terminalSize() + if err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } - xStep = xRange / float64(width) - yStep = yRange / float64(height) - ) + xStep := xRange / float64(width) + yStep := yRange / float64(height) y := yMin for i := 0; i < height; i++ { -- cgit v1.3.1