Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bash alias command substitution window resize bug #5417

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
30 changes: 22 additions & 8 deletions src/textual/drivers/linux_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
lap1nou marked this conversation as resolved.
Show resolved Hide resolved

return width, height

def _enable_mouse_support(self) -> None:
Expand Down
30 changes: 22 additions & 8 deletions src/textual/drivers/linux_inline_driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down