fix: prefer env vars over ioctl when ioctl returns 80x24
ioctl on stdout's fd can return the default 80x24 even when the terminal is much larger (fd mismatch). The new logic: 1. Try ioctl — if it returns >80x24, trust it (correct at runtime). 2. If ioctl returned 80x24 (suspicious default), try COLUMNS/LINES from the shell environment instead. 3. If both fail, return whatever ioctl gave us (80x24). This fixes initial sizing on terminals where ioctl disagrees with the real TTY size, without breaking runtime SIGWINCH resize (which always re-queries ioctl, and that is correct after resize).
This commit is contained in:
@@ -27,26 +27,30 @@
|
||||
(values))
|
||||
|
||||
(defmethod backend-size ((b simple-backend))
|
||||
(or (ignore-errors
|
||||
(let* ((+tiocgwinsz+ 21523)
|
||||
(winsize (sb-alien:make-alien sb-alien:unsigned-short 4)))
|
||||
(unwind-protect
|
||||
(progn
|
||||
(sb-unix:unix-ioctl (sb-sys:fd-stream-fd
|
||||
(backend-output-stream b))
|
||||
+tiocgwinsz+
|
||||
(sb-alien:alien-sap winsize))
|
||||
(values (sb-alien:deref winsize 1)
|
||||
(sb-alien:deref winsize 0)))
|
||||
(sb-alien:free-alien winsize))))
|
||||
(ignore-errors
|
||||
(let ((cols (parse-integer (sb-ext:posix-getenv "COLUMNS")
|
||||
:junk-allowed t))
|
||||
(rows (parse-integer (sb-ext:posix-getenv "LINES")
|
||||
:junk-allowed t)))
|
||||
(when (and cols rows (> cols 0) (> rows 0))
|
||||
(values cols rows))))
|
||||
(values 80 24)))
|
||||
(multiple-value-bind (w h)
|
||||
(or (ignore-errors
|
||||
(let* ((+tiocgwinsz+ 21523)
|
||||
(winsize (sb-alien:make-alien sb-alien:unsigned-short 4)))
|
||||
(unwind-protect
|
||||
(progn
|
||||
(sb-unix:unix-ioctl (sb-sys:fd-stream-fd
|
||||
(backend-output-stream b))
|
||||
+tiocgwinsz+
|
||||
(sb-alien:alien-sap winsize))
|
||||
(values (sb-alien:deref winsize 1)
|
||||
(sb-alien:deref winsize 0)))
|
||||
(sb-alien:free-alien winsize))))
|
||||
(values 0 0))
|
||||
(if (and (> w 80) (> h 24))
|
||||
(values w h)
|
||||
(or (ignore-errors
|
||||
(let ((cols (parse-integer (sb-ext:posix-getenv "COLUMNS")
|
||||
:junk-allowed t))
|
||||
(rows (parse-integer (sb-ext:posix-getenv "LINES")
|
||||
:junk-allowed t)))
|
||||
(when (and cols rows (> cols 0) (> rows 0))
|
||||
(values cols rows))))
|
||||
(values w h)))))
|
||||
|
||||
(defmethod backend-write ((b simple-backend) string)
|
||||
(let ((stream (backend-output-stream b)))
|
||||
|
||||
Reference in New Issue
Block a user