fix: remove env var fallback for COLUMNS/LINES (SBCL strips them)

SBCL unconditionally strips COLUMNS and LINES from the environment,
so posix-getenv always returns nil for those names. stty size is
the reliable cross-platform fallback for terminal dimensions.
This commit is contained in:
2026-05-14 13:46:13 -04:00
parent 4fa7e98b80
commit 9b472e281f
2 changed files with 48 additions and 60 deletions

View File

@@ -34,35 +34,30 @@
(values (sb-alien:deref winsize 1)
(sb-alien:deref winsize 0)))
(sb-alien:free-alien winsize))))
;; stty size — reliable across all shells and terminals.
(ignore-errors
(let* ((cstr (sb-ext:posix-getenv "COLUMNS"))
(rstr (sb-ext:posix-getenv "LINES"))
(cols (when cstr (parse-integer cstr :junk-allowed t)))
(rows (when rstr (parse-integer rstr :junk-allowed t))))
(unless (and cols rows)
(let* ((out (uiop:run-program '("stty" "size")
:output :string
:ignore-error-status t))
(parts (and out (uiop:split-string
(string-trim '(#\newline #\space) out)))))
(when (and parts (= (length parts) 2))
(let ((stty-rows (parse-integer (first parts) :junk-allowed t))
(stty-cols (parse-integer (second parts) :junk-allowed t)))
(when (and stty-rows stty-cols (> stty-rows 0) (> stty-cols 0))
(unless cols (setf cols stty-cols))
(unless rows (setf rows stty-rows)))))))
(unless (and cols rows)
(let ((out (uiop:run-program '("sh" "-c" "tput cols 2>/dev/null; tput lines 2>/dev/null")
:output :string
:ignore-error-status t)))
(when out
(let* ((parts (uiop:split-string (string-trim '(#\newline #\space) out)))
(a (when parts (parse-integer (first parts) :junk-allowed t)))
(b (when (cdr parts) (parse-integer (second parts) :junk-allowed t))))
(when a (setf cols a))
(when b (setf rows b))))))
(when (and cols rows (> cols 0) (> rows 0))
(values cols rows))))
(let* ((out (uiop:run-program '("stty" "size")
:output :string
:ignore-error-status t))
(parts (and out (uiop:split-string
(string-trim '(#\newline #\space) out)))))
(when (and parts (= (length parts) 2))
(let ((rows (parse-integer (first parts) :junk-allowed t))
(cols (parse-integer (second parts) :junk-allowed t)))
(when (and rows cols (> rows 0) (> cols 0))
(values cols rows))))))
;; tput — final shell fallback
(ignore-errors
(let* ((out (uiop:run-program '("sh" "-c" "tput cols 2>/dev/null; tput lines 2>/dev/null")
:output :string
:ignore-error-status t))
(parts (and out (uiop:split-string
(string-trim '(#\newline #\space) out)))))
(when (and parts (= (length parts) 2))
(let ((cols (parse-integer (first parts) :junk-allowed t))
(rows (parse-integer (second parts) :junk-allowed t)))
(when (and rows cols (> rows 0) (> cols 0))
(values cols rows))))))
(values 80 24)))
(defmethod backend-write ((b simple-backend) string)