commit:     33dac2db0fbbe4bafc2343978f969a0f41f0d200
Author:     Zac Medico <zmedico <AT> gentoo <DOT> org>
AuthorDate: Wed Jan  1 00:26:51 2025 +0000
Commit:     Zac Medico <zmedico <AT> gentoo <DOT> org>
CommitDate: Wed Jan  1 00:32:21 2025 +0000
URL:        https://gitweb.gentoo.org/proj/portage.git/commit/?id=33dac2db

get_term_size: Use curses if stdin is not a tty

Use curses if stdin is not a tty, since stty fails in this case
with the following error:

stty: 'standard input': Inappropriate ioctl for device

Bug: https://bugs.gentoo.org/947282
Signed-off-by: Zac Medico <zmedico <AT> gentoo.org>

 lib/portage/output.py | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/lib/portage/output.py b/lib/portage/output.py
index 9c78a4c298..42eb5062cf 100644
--- a/lib/portage/output.py
+++ b/lib/portage/output.py
@@ -527,6 +527,18 @@ def get_term_size(fd=None):
     # returns stale values after terminal resize. Do not use 
curses.initscr().getmaxyx()
     # since that has unwanted side-effects, requiring a call to `stty sane` to 
restore a
     # sane state.
+    if not sys.stdin.isatty():
+        # Use curses if stdin is not a tty since stty fails in that case.
+        try:
+            import curses
+
+            try:
+                curses.setupterm(term=os.environ.get("TERM", "unknown"), 
fd=fd.fileno())
+                return curses.tigetnum("lines"), curses.tigetnum("cols")
+            except curses.error:
+                pass
+        except ImportError:
+            pass
 
     try:
         proc = subprocess.Popen(["stty", "size"], stdout=subprocess.PIPE, 
stderr=fd)

Reply via email to