From d42ef3ef276e778764b1a091cf6a0650b7b59c4a Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 02:14:30 +0100 Subject: [PATCH 01/18] Update linux_driver.py --- src/textual/drivers/linux_driver.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/textual/drivers/linux_driver.py b/src/textual/drivers/linux_driver.py index 7e04f4cf26..bc85c46c43 100644 --- a/src/textual/drivers/linux_driver.py +++ b/src/textual/drivers/linux_driver.py @@ -102,19 +102,26 @@ 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: int | None = 0 try: - width, height = shutil.get_terminal_size() - except (AttributeError, ValueError, OSError): + height = int(os.environ['LINES']) + except (KeyError, ValueError): + height: int | None = 0 + + if width <= 0 or height <= 0: try: - width, height = shutil.get_terminal_size() + width, height = os.get_terminal_size(self._file.fileno()) except (AttributeError, ValueError, OSError): - pass - width = width or 80 - height = height or 25 + try: + width, height = os.get_terminal_size(self._file.fileno()) + except (AttributeError, ValueError, OSError): + pass + width = width or 80 + height = height or 25 return width, height def _enable_mouse_support(self) -> None: From 22cbe9d215f3a15f553fd77d52d353ddd13bb1c2 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 02:15:31 +0100 Subject: [PATCH 02/18] Update headless_driver.py --- src/textual/drivers/headless_driver.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/textual/drivers/headless_driver.py b/src/textual/drivers/headless_driver.py index 60e5f8bb0b..838492b721 100644 --- a/src/textual/drivers/headless_driver.py +++ b/src/textual/drivers/headless_driver.py @@ -18,19 +18,26 @@ def is_headless(self) -> bool: def _get_terminal_size(self) -> tuple[int, int]: if self._size is not None: return self._size - width: int | None = 80 - height: int | None = 25 - import shutil + try: + width = int(os.environ['COLUMNS']) + except (KeyError, ValueError): + width: int | None = 0 try: - width, height = shutil.get_terminal_size() - except (AttributeError, ValueError, OSError): + height = int(os.environ['LINES']) + except (KeyError, ValueError): + height: int | None = 0 + + if width <= 0 or height <= 0: try: - width, height = shutil.get_terminal_size() + width, height = os.get_terminal_size(self._file.fileno()) except (AttributeError, ValueError, OSError): - pass - width = width or 80 - height = height or 25 + try: + width, height = os.get_terminal_size(self._file.fileno()) + except (AttributeError, ValueError, OSError): + pass + width = width or 80 + height = height or 25 return width, height def write(self, data: str) -> None: From cbed5cf5432e0f1ec0ff78b78d76e0dc6a3f2f01 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 02:15:50 +0100 Subject: [PATCH 03/18] Update linux_inline_driver.py --- src/textual/drivers/linux_inline_driver.py | 25 ++++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/textual/drivers/linux_inline_driver.py b/src/textual/drivers/linux_inline_driver.py index 3fc62df9de..9945305165 100644 --- a/src/textual/drivers/linux_inline_driver.py +++ b/src/textual/drivers/linux_inline_driver.py @@ -61,19 +61,26 @@ 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: int | None = 0 try: - width, height = shutil.get_terminal_size() - except (AttributeError, ValueError, OSError): + height = int(os.environ['LINES']) + except (KeyError, ValueError): + height: int | None = 0 + + if width <= 0 or height <= 0: try: - width, height = shutil.get_terminal_size() + width, height = os.get_terminal_size(self._file.fileno()) except (AttributeError, ValueError, OSError): - pass - width = width or 80 - height = height or 25 + try: + width, height = os.get_terminal_size(self._file.fileno()) + except (AttributeError, ValueError, OSError): + pass + width = width or 80 + height = height or 25 return width, height def _enable_mouse_support(self) -> None: From abace9726256b52119b3f05e3b43641929182b41 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 02:39:22 +0100 Subject: [PATCH 04/18] Update headless_driver.py --- src/textual/drivers/headless_driver.py | 25 +++++++++---------------- 1 file changed, 9 insertions(+), 16 deletions(-) diff --git a/src/textual/drivers/headless_driver.py b/src/textual/drivers/headless_driver.py index 838492b721..60e5f8bb0b 100644 --- a/src/textual/drivers/headless_driver.py +++ b/src/textual/drivers/headless_driver.py @@ -18,26 +18,19 @@ def is_headless(self) -> bool: def _get_terminal_size(self) -> tuple[int, int]: if self._size is not None: return self._size - try: - width = int(os.environ['COLUMNS']) - except (KeyError, ValueError): - width: int | None = 0 + width: int | None = 80 + height: int | None = 25 + import shutil try: - height = int(os.environ['LINES']) - except (KeyError, ValueError): - height: int | None = 0 - - if width <= 0 or height <= 0: + width, height = shutil.get_terminal_size() + except (AttributeError, ValueError, OSError): try: - width, height = os.get_terminal_size(self._file.fileno()) + width, height = shutil.get_terminal_size() except (AttributeError, ValueError, OSError): - try: - width, height = os.get_terminal_size(self._file.fileno()) - except (AttributeError, ValueError, OSError): - pass - width = width or 80 - height = height or 25 + pass + width = width or 80 + height = height or 25 return width, height def write(self, data: str) -> None: From c9a9c876db101425be8f8741789b87fe9383ebc6 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 03:01:18 +0100 Subject: [PATCH 05/18] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b56e467c0c..42e98a9c78 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 window resize within a bash command substitution https://github.com/Textualize/textual/pull/5417 ### Added From 53ffd227d2384b4463ce835b02032161b35a166b Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 15:18:00 +0100 Subject: [PATCH 06/18] Update linux_driver.py --- src/textual/drivers/linux_driver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/textual/drivers/linux_driver.py b/src/textual/drivers/linux_driver.py index bc85c46c43..0985cc8cdc 100644 --- a/src/textual/drivers/linux_driver.py +++ b/src/textual/drivers/linux_driver.py @@ -105,12 +105,12 @@ def _get_terminal_size(self) -> tuple[int, int]: try: width = int(os.environ['COLUMNS']) except (KeyError, ValueError): - width: int | None = 0 + width = 0 try: height = int(os.environ['LINES']) except (KeyError, ValueError): - height: int | None = 0 + height = 0 if width <= 0 or height <= 0: try: From f4e848aa6a03861a36db1c5345e1182e3e021629 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 15:18:23 +0100 Subject: [PATCH 07/18] Update linux_inline_driver.py --- src/textual/drivers/linux_inline_driver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/textual/drivers/linux_inline_driver.py b/src/textual/drivers/linux_inline_driver.py index 9945305165..66ebd59e5f 100644 --- a/src/textual/drivers/linux_inline_driver.py +++ b/src/textual/drivers/linux_inline_driver.py @@ -64,12 +64,12 @@ def _get_terminal_size(self) -> tuple[int, int]: try: width = int(os.environ['COLUMNS']) except (KeyError, ValueError): - width: int | None = 0 + width = 0 try: height = int(os.environ['LINES']) except (KeyError, ValueError): - height: int | None = 0 + height = 0 if width <= 0 or height <= 0: try: From 91d02b18ec3dbf1ab66738933d4b7bd83986f592 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 15:18:50 +0100 Subject: [PATCH 08/18] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42e98a9c78..fe50a71fa4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,7 +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 window resize within a bash command substitution https://github.com/Textualize/textual/pull/5417 +- 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 From 8d0919e4dccb385f3bce8ed52dbb6b63a979601c Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 17:16:21 +0100 Subject: [PATCH 09/18] Update linux_driver.py --- src/textual/drivers/linux_driver.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/textual/drivers/linux_driver.py b/src/textual/drivers/linux_driver.py index 0985cc8cdc..3ca2b5dc23 100644 --- a/src/textual/drivers/linux_driver.py +++ b/src/textual/drivers/linux_driver.py @@ -114,14 +114,17 @@ def _get_terminal_size(self) -> tuple[int, int]: if width <= 0 or height <= 0: try: - width, height = os.get_terminal_size(self._file.fileno()) + size = os.get_terminal_size(self._file.fileno()) except (AttributeError, ValueError, OSError): try: - width, height = os.get_terminal_size(self._file.fileno()) + size = os.get_terminal_size(self._file.fileno()) except (AttributeError, ValueError, OSError): pass - width = width or 80 - height = height or 25 + + if columns <= 0: + width = size.columns or 80 + if lines <= 0: + height = size.lines or 25 return width, height def _enable_mouse_support(self) -> None: From c5d7d71d2bcaa11d83c5fb02f5f683fe69d14538 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 17:16:55 +0100 Subject: [PATCH 10/18] Update linux_inline_driver.py --- src/textual/drivers/linux_inline_driver.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/textual/drivers/linux_inline_driver.py b/src/textual/drivers/linux_inline_driver.py index 66ebd59e5f..bd1d4c6130 100644 --- a/src/textual/drivers/linux_inline_driver.py +++ b/src/textual/drivers/linux_inline_driver.py @@ -79,8 +79,11 @@ def _get_terminal_size(self) -> tuple[int, int]: width, height = os.get_terminal_size(self._file.fileno()) except (AttributeError, ValueError, OSError): pass - width = width or 80 - height = height or 25 + + if columns <= 0: + width = size.columns or 80 + if lines <= 0: + height = size.lines or 25 return width, height def _enable_mouse_support(self) -> None: From e43b27ef2357a883bc3e6e5976010251763b1225 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 18:46:38 +0100 Subject: [PATCH 11/18] Update linux_inline_driver.py --- src/textual/drivers/linux_inline_driver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/textual/drivers/linux_inline_driver.py b/src/textual/drivers/linux_inline_driver.py index bd1d4c6130..2dd364790e 100644 --- a/src/textual/drivers/linux_inline_driver.py +++ b/src/textual/drivers/linux_inline_driver.py @@ -80,9 +80,9 @@ def _get_terminal_size(self) -> tuple[int, int]: except (AttributeError, ValueError, OSError): pass - if columns <= 0: + if width <= 0: width = size.columns or 80 - if lines <= 0: + if height <= 0: height = size.lines or 25 return width, height From c17785e2323128395b48dc2add4971e7af0ca13d Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 18:46:55 +0100 Subject: [PATCH 12/18] Update linux_driver.py --- src/textual/drivers/linux_driver.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/textual/drivers/linux_driver.py b/src/textual/drivers/linux_driver.py index 3ca2b5dc23..88440d9935 100644 --- a/src/textual/drivers/linux_driver.py +++ b/src/textual/drivers/linux_driver.py @@ -121,9 +121,9 @@ def _get_terminal_size(self) -> tuple[int, int]: except (AttributeError, ValueError, OSError): pass - if columns <= 0: + if width <= 0: width = size.columns or 80 - if lines <= 0: + if height <= 0: height = size.lines or 25 return width, height From 853e573b6effd7b85b87ad3475242d326012b5dd Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 18:55:41 +0100 Subject: [PATCH 13/18] Update linux_driver.py --- src/textual/drivers/linux_driver.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/textual/drivers/linux_driver.py b/src/textual/drivers/linux_driver.py index 88440d9935..88fedd880e 100644 --- a/src/textual/drivers/linux_driver.py +++ b/src/textual/drivers/linux_driver.py @@ -121,10 +121,12 @@ def _get_terminal_size(self) -> tuple[int, int]: except (AttributeError, ValueError, OSError): pass - if width <= 0: - width = size.columns or 80 - if height <= 0: - height = size.lines or 25 + try: + width = size.columns + height = size.lines + except NameError: + width = 80 + height = 25 return width, height def _enable_mouse_support(self) -> None: From 76bdcb30e906d0f38b0a9e0b95656e48b0c31dee Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 19:24:00 +0100 Subject: [PATCH 14/18] Update linux_driver.py --- src/textual/drivers/linux_driver.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/textual/drivers/linux_driver.py b/src/textual/drivers/linux_driver.py index 88fedd880e..8383be51bf 100644 --- a/src/textual/drivers/linux_driver.py +++ b/src/textual/drivers/linux_driver.py @@ -116,10 +116,7 @@ def _get_terminal_size(self) -> tuple[int, int]: try: size = os.get_terminal_size(self._file.fileno()) except (AttributeError, ValueError, OSError): - try: - size = os.get_terminal_size(self._file.fileno()) - except (AttributeError, ValueError, OSError): - pass + pass try: width = size.columns From 83f43847eaac9a2fb55b2f609e5ab8c924b86d91 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 19:27:16 +0100 Subject: [PATCH 15/18] Update linux_inline_driver.py --- src/textual/drivers/linux_inline_driver.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/textual/drivers/linux_inline_driver.py b/src/textual/drivers/linux_inline_driver.py index 2dd364790e..b2064ebe7b 100644 --- a/src/textual/drivers/linux_inline_driver.py +++ b/src/textual/drivers/linux_inline_driver.py @@ -75,15 +75,14 @@ def _get_terminal_size(self) -> tuple[int, int]: try: width, height = os.get_terminal_size(self._file.fileno()) except (AttributeError, ValueError, OSError): - try: - width, height = os.get_terminal_size(self._file.fileno()) - except (AttributeError, ValueError, OSError): - pass + pass - if width <= 0: - width = size.columns or 80 - if height <= 0: - height = size.lines or 25 + try: + width = size.columns + height = size.lines + except NameError: + width = 80 + height = 25 return width, height def _enable_mouse_support(self) -> None: From fd6090be681a688d429daae14e634ea4d820ad1a Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 19:47:58 +0100 Subject: [PATCH 16/18] Update linux_driver.py --- src/textual/drivers/linux_driver.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/textual/drivers/linux_driver.py b/src/textual/drivers/linux_driver.py index 8383be51bf..d9e3425a72 100644 --- a/src/textual/drivers/linux_driver.py +++ b/src/textual/drivers/linux_driver.py @@ -113,18 +113,23 @@ def _get_terminal_size(self) -> tuple[int, int]: 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: 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 - try: - width = size.columns - height = size.lines - except NameError: + # Default to the de facto standard terminal size: + if width <= 0: width = 80 + if height <= 0: height = 25 - return width, height + + return width, height def _enable_mouse_support(self) -> None: """Enable reporting of mouse events.""" From a25e84fe96d88efadee5387dbb9a03b73704a2de Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 21:19:55 +0100 Subject: [PATCH 17/18] Update linux_inline_driver.py --- src/textual/drivers/linux_inline_driver.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/textual/drivers/linux_inline_driver.py b/src/textual/drivers/linux_inline_driver.py index b2064ebe7b..cfab4c7fea 100644 --- a/src/textual/drivers/linux_inline_driver.py +++ b/src/textual/drivers/linux_inline_driver.py @@ -72,17 +72,22 @@ def _get_terminal_size(self) -> tuple[int, int]: 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 = os.get_terminal_size(self._file.fileno()) + 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 - try: - width = size.columns - height = size.lines - except NameError: + # 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: From 5b10319b35bddb22e42d58cbd907be443ae78057 Mon Sep 17 00:00:00 2001 From: lapinou Date: Sat, 21 Dec 2024 22:27:33 +0100 Subject: [PATCH 18/18] Update linux_driver.py --- src/textual/drivers/linux_driver.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/textual/drivers/linux_driver.py b/src/textual/drivers/linux_driver.py index d9e3425a72..9aa9dff63c 100644 --- a/src/textual/drivers/linux_driver.py +++ b/src/textual/drivers/linux_driver.py @@ -129,7 +129,7 @@ def _get_terminal_size(self) -> tuple[int, int]: if height <= 0: height = 25 - return width, height + return width, height def _enable_mouse_support(self) -> None: """Enable reporting of mouse events."""