From f019739464590d23b5fac4a8aa499c3229afa411 Mon Sep 17 00:00:00 2001
From: Honza Mach <honza.mach.ml@gmail.com>
Date: Sat, 30 Apr 2022 18:39:01 +0200
Subject: [PATCH] Reconstructing lost commits

---
 pydgets/widgets.py | 39 +++++++++++++++++++++++++++------------
 setup.py           |  2 +-
 2 files changed, 28 insertions(+), 13 deletions(-)

diff --git a/pydgets/widgets.py b/pydgets/widgets.py
index 5c47ceb..a1fae84 100644
--- a/pydgets/widgets.py
+++ b/pydgets/widgets.py
@@ -288,24 +288,39 @@ TREE_STYLES = {
 
 #-------------------------------------------------------------------------------
 
-def terminal_size():
+def is_terminal(fdesc):
     """
-    Detect the current size of terminal window as a numer of rows and columns.
+    Check, if we are connected to any terminal-like device.
     """
     try:
-        (rows, columns) = os.popen('stty size', 'r').read().split()
-        rows    = int(rows)
-        columns = int(columns)
-        return (columns, rows)
-
-    # Currently ignore any errors and return some reasonable default values.
-    # Errors may occur, when the library is used in non-terminal application
-    # like daemon.
+        if not fdesc:
+            return False
+        return os.isatty(fdesc.fileno())
     except:
-        pass
+        return False
+
+# Detect the terminal automatically after library initialization.
+IS_TERMINAL = is_terminal(sys.stdout)
+
+def terminal_size():
+    """
+    Detect the current size of terminal window as a numer of rows and columns.
+    """
+    if IS_TERMINAL:
+        try:
+            (rows, columns) = os.popen('stty size', 'r').read().split()
+            rows    = int(rows)
+            columns = int(columns)
+            return (columns, rows)
+
+        # Currently ignore any errors and return some reasonable default values.
+        # Errors may occur, when the library is used in non-terminal application
+        # like daemon.
+        except:
+            pass
     return (80, 24)
 
-# Detect the terminal size automatically after library initialization
+# Detect the terminal size automatically after library initialization.
 (TERMINAL_WIDTH, TERMINAL_HEIGHT) = terminal_size()
 
 #-------------------------------------------------------------------------------
diff --git a/setup.py b/setup.py
index d368ac5..dddf393 100644
--- a/setup.py
+++ b/setup.py
@@ -23,7 +23,7 @@ with open(path.join(here, 'README.rst'), encoding='utf-8') as f:
 
 setup(
     name = 'pydgets',
-    version = '0.6',
+    version = '0.9',
     description = 'Console widget library for Python 3',
     long_description = long_description,
     classifiers = [
-- 
GitLab