summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRose Hogenson <rosehogenson@posteo.net>2023-09-01 13:02:19 -0700
committerRose Hogenson <rosehogenson@posteo.net>2023-09-01 13:02:19 -0700
commit5ac6a72cf9be60addc5872a869669f86e08c7e36 (patch)
tree7962477183b40b5ce9fae0f007a24ce399617dde
parenta80df3e4a66a0a36df5c1b78b94c8835ff82e366 (diff)
downloadmandelbrot-5ac6a72cf9be60addc5872a869669f86e08c7e36.tar.zst
Detect the terminal size with a syscall.
This will only work on Linux.
-rw-r--r--mandelbrot.go28
1 files 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++ {