Skip to content

Commit 83d90f7

Browse files
committed
Fix ncurses resize by seeding PTY size via stty, not env vars
Setting LINES/COLUMNS in the child environment was freezing ncurses apps like htop at their start-up size: ncurses treats those env vars as an explicit user override of the kernel-reported window size, so the TIOCGWINSZ updates that SIGWINCH triggers were ignored and the app kept drawing at the original dimensions after a window resize. Child processes launched from inside the ncurses app also inherited the stale values. Drop LINES/COLUMNS from the prepared environment and instead bake the initial size into the shell wrapper via `stty rows N columns N` before exec. The kernel PTY winsize is now the single source of truth, set-process-window-size continues to handle live resizes, and ncurses apps pick up the new size normally on SIGWINCH. Adds a regression test that spawns a real /bin/sh through ghostel--start-process, asserts the stty wrapper contains the pinned rows/columns, and asserts the prepared environment has no LINES or COLUMNS entries. Fixes #67
1 parent 144c9ba commit 83d90f7

2 files changed

Lines changed: 40 additions & 4 deletions

File tree

ghostel.el

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2175,7 +2175,9 @@ on the remote host."
21752175
(t "erase '^?' iutf8")))
21762176
(shell-command
21772177
(list "/bin/sh" "-c"
2178-
(concat "stty " stty-flags " 2>/dev/null; "
2178+
(concat "stty " stty-flags
2179+
(format " rows %d columns %d" height width)
2180+
" 2>/dev/null; "
21792181
"printf '\\033[H\\033[2J'; exec "
21802182
(shell-quote-argument shell)
21812183
(and shell-args
@@ -2187,9 +2189,7 @@ on the remote host."
21872189
(list
21882190
"INSIDE_EMACS=ghostel"
21892191
"TERM=xterm-256color"
2190-
"COLORTERM=truecolor"
2191-
(format "COLUMNS=%d" width)
2192-
(format "LINES=%d" height))
2192+
"COLORTERM=truecolor")
21932193
(unless remote-p
21942194
(list (format "EMACS_GHOSTEL_PATH=%s" ghostel-dir)))
21952195
integration-env

test/ghostel-test.el

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,6 +2093,42 @@ rendered by `ghostel--delayed-redraw'. This is the exact real-world path."
20932093
(ghostel-shell "/bin/zsh"))
20942094
(should (equal "/bin/zsh" (ghostel--get-shell)))))
20952095

2096+
(ert-deftest ghostel-test-start-process-sets-size-via-stty-not-env ()
2097+
"Initial terminal size must be baked into the `stty' wrapper, not
2098+
into `LINES'/`COLUMNS' env vars. Setting those env vars freezes
2099+
ncurses apps like htop at start-up size and breaks live resize."
2100+
(let ((captured-env nil)
2101+
(orig-make-process (symbol-function #'make-process)))
2102+
(cl-letf (((symbol-function #'window-body-height)
2103+
(lambda (&optional _w) 43))
2104+
((symbol-function #'window-max-chars-per-line)
2105+
(lambda (&optional _w) 137))
2106+
((symbol-function #'make-process)
2107+
(lambda (&rest plist)
2108+
(setq captured-env process-environment)
2109+
(apply orig-make-process plist))))
2110+
(with-temp-buffer
2111+
(let* ((process-environment '("PATH=/usr/bin:/bin" "HOME=/tmp"))
2112+
(ghostel-shell "/bin/sh")
2113+
(ghostel-shell-integration nil)
2114+
(default-directory "/tmp/")
2115+
(proc (ghostel--start-process)))
2116+
(unwind-protect
2117+
(let ((cmd (process-command proc)))
2118+
(should (equal #'ghostel--window-adjust-process-window-size
2119+
(process-get proc 'adjust-window-size-function)))
2120+
(should (equal '("/bin/sh" "-c") (seq-take cmd 2)))
2121+
(should (string-match-p "stty .* rows 43 columns 137"
2122+
(nth 2 cmd)))
2123+
(should-not (seq-some (lambda (s) (string-prefix-p "LINES=" s))
2124+
captured-env))
2125+
(should-not (seq-some (lambda (s) (string-prefix-p "COLUMNS=" s))
2126+
captured-env))
2127+
(should (member "TERM=xterm-256color" captured-env))
2128+
(should (member "COLORTERM=truecolor" captured-env)))
2129+
(when (process-live-p proc)
2130+
(delete-process proc))))))))
2131+
20962132
;; -----------------------------------------------------------------------
20972133
;; Tests: window resize
20982134
;; -----------------------------------------------------------------------

0 commit comments

Comments
 (0)