summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--blessings/__init__.py12
1 files changed, 10 insertions, 2 deletions
diff --git a/blessings/__init__.py b/blessings/__init__.py
index b135e01..bee547e 100644
--- a/blessings/__init__.py
+++ b/blessings/__init__.py
@@ -217,7 +217,10 @@ class Terminal(object):
return self._height_and_width()[1]
def _height_and_width(self):
- """Return a tuple of (terminal height, terminal width)."""
+ """Return a tuple of (terminal height, terminal width),
+ using TIOCGWINSZ (Terminal I/O-Control: Get Window Size),
+ falling back to environment variables (LINES, COLUMNS),
+ and 25 x 80 when otherwise unavailable."""
# tigetnum('lines') and tigetnum('cols') update only if we call
# setupterm() again.
for descriptor in self._init_descriptor, sys.__stdout__:
@@ -225,8 +228,13 @@ class Terminal(object):
return struct.unpack(
'hhhh', ioctl(descriptor, TIOCGWINSZ, '\000' * 8))[0:2]
except IOError:
+ # when the output stream or init descriptor is not a tty, such
+ # as when when stdout is piped to another program, fe. tee(1),
+ # these ioctls will raise IOError
pass
- return None, None # Should never get here
+ lines, cols = (int(environ.get('LINES', '24')),
+ int(environ.get('COLUMNS', '80')))
+ return lines, cols
@contextmanager
def location(self, x=None, y=None):