From 9ae00e113d1d60ff28b490bc8b896b3c775c4463 Mon Sep 17 00:00:00 2001 From: GitHub Copilot Date: Sat, 24 Jan 2026 16:39:29 +0000 Subject: [PATCH] Sync pyte screen to actual PTY size before screenshots Query PTY size with TIOCGWINSZ and resize pyte screen to match. Fixes tmux status bar wrapping when terminal was resized externally. --- src/textual_webterm/terminal_session.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/textual_webterm/terminal_session.py b/src/textual_webterm/terminal_session.py index abc231e..4bbe889 100644 --- a/src/textual_webterm/terminal_session.py +++ b/src/textual_webterm/terminal_session.py @@ -105,6 +105,27 @@ class TerminalSession(Session): assert self.master_fd is not None fcntl.ioctl(self.master_fd, termios.TIOCSWINSZ, buf) + def _get_terminal_size(self) -> tuple[int, int]: + """Get actual PTY size. Returns (width, height).""" + assert self.master_fd is not None + buf = array.array("h", [0, 0, 0, 0]) + fcntl.ioctl(self.master_fd, termios.TIOCGWINSZ, buf) + return (buf[1], buf[0]) # cols, rows + + async def _sync_pyte_to_pty(self) -> None: + """Sync pyte screen size to actual PTY size.""" + if self.master_fd is None: + return + loop = asyncio.get_running_loop() + width, height = await loop.run_in_executor(None, self._get_terminal_size) + async with self._screen_lock: + if self._screen.columns != width or self._screen.lines != height: + log.debug("Syncing pyte screen from %dx%d to %dx%d", + self._screen.columns, self._screen.lines, width, height) + self._screen.resize(height, width) + self._last_width = width + self._last_height = height + async def set_terminal_size(self, width: int, height: int) -> None: # Track the size for reconnection self._last_width = width @@ -174,6 +195,9 @@ class TerminalSession(Session): - buffer: list of rows, each containing character data with styling - has_changes: True if screen has changed since last call """ + # Sync pyte to actual PTY size before reading state + await self._sync_pyte_to_pty() + async with self._screen_lock: width = self._screen.columns height = self._screen.lines