diff --git a/CHANGELOG.md b/CHANGELOG.md index b56e467c0c..fe50a71fa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ### Fixed - Fixed `Pilot.click` not working with `times` parameter https://github.com/Textualize/textual/pull/5398 +- Fixed terminal resize when standard output is not a tty: pipe (e.g. shell command substitution), regular file, /dev/null, etc https://github.com/Textualize/textual/pull/5417 ### Added diff --git a/src/textual/drivers/linux_driver.py b/src/textual/drivers/linux_driver.py index 7e04f4cf26..9aa9dff63c 100644 --- a/src/textual/drivers/linux_driver.py +++ b/src/textual/drivers/linux_driver.py @@ -102,19 +102,33 @@ def _get_terminal_size(self) -> tuple[int, int]: Returns: The size of the terminal as a tuple of (WIDTH, HEIGHT). """ - width: int | None = 80 - height: int | None = 25 - import shutil + try: + width = int(os.environ['COLUMNS']) + except (KeyError, ValueError): + width = 0 try: - width, height = shutil.get_terminal_size() - except (AttributeError, ValueError, OSError): + height = int(os.environ['LINES']) + except (KeyError, ValueError): + height = 0 + + if width <= 0 or height <= 0: + # LINES and COLUMNS do not fully define terminal size: query the tty to get its actual size: try: - width, height = shutil.get_terminal_size() + size = os.get_terminal_size(self._file.fileno()) + if width <= 0: + width = size.columns + if height <= 0: + height = size.lines except (AttributeError, ValueError, OSError): pass - width = width or 80 - height = height or 25 + + # Default to the de facto standard terminal size: + if width <= 0: + width = 80 + if height <= 0: + height = 25 + return width, height def _enable_mouse_support(self) -> None: diff --git a/src/textual/drivers/linux_inline_driver.py b/src/textual/drivers/linux_inline_driver.py index 3fc62df9de..cfab4c7fea 100644 --- a/src/textual/drivers/linux_inline_driver.py +++ b/src/textual/drivers/linux_inline_driver.py @@ -61,19 +61,33 @@ def _get_terminal_size(self) -> tuple[int, int]: Returns: The size of the terminal as a tuple of (WIDTH, HEIGHT). """ - width: int | None = 80 - height: int | None = 25 - import shutil + try: + width = int(os.environ['COLUMNS']) + except (KeyError, ValueError): + width = 0 try: - width, height = shutil.get_terminal_size() - except (AttributeError, ValueError, OSError): + height = int(os.environ['LINES']) + except (KeyError, ValueError): + height = 0 + + if width <= 0 or height <= 0: + # LINES and COLUMNS do not fully define terminal size: query the tty to get its actual size: try: - width, height = shutil.get_terminal_size() + size = os.get_terminal_size(self._file.fileno()) + if width <= 0: + width = size.columns + if height <= 0: + height = size.lines except (AttributeError, ValueError, OSError): pass - width = width or 80 - height = height or 25 + + # Default to the de facto standard terminal size: + if width <= 0: + width = 80 + if height <= 0: + height = 25 + return width, height def _enable_mouse_support(self) -> None: