diff options
author | Erik Rose <grinch@grinchcentral.com> | 2013-11-05 20:48:48 -0500 |
---|---|---|
committer | Erik Rose <grinch@grinchcentral.com> | 2013-11-05 20:48:48 -0500 |
commit | e104550cddacda3aaf7a86f0d07491e398f1ccc8 (patch) | |
tree | bd134a481a6bcacb170a7a7b4091a6c580569f1a | |
parent | 536396a69c7e6c2f36e4961e6ab67d78444e14de (diff) | |
parent | 776f2725c0fa1e357f96776f1bc1ee0ea4543f11 (diff) | |
download | blessings-e104550cddacda3aaf7a86f0d07491e398f1ccc8.tar.gz |
Fall back to env vars to find height and width. Make the height-and-width test run. Close #46.
-rw-r--r-- | README.rst | 2 | ||||
-rw-r--r-- | blessings/__init__.py | 16 | ||||
-rw-r--r-- | blessings/tests.py | 6 |
3 files changed, 19 insertions, 5 deletions
@@ -439,6 +439,8 @@ Version History * Make ``is_a_tty`` a read-only property, like ``does_styling``. Writing to it never would have done anything constructive. * Add ``fullscreen()`` and ``hidden_cursor()`` to the auto-generated docs. + * Fall back to ``LINES`` and ``COLUMNS`` environment vars to find height and + width. (jquast) 1.5.1 * Clean up fabfile, removing the redundant ``test`` command. diff --git a/blessings/__init__.py b/blessings/__init__.py index b135e01..4506869 100644 --- a/blessings/__init__.py +++ b/blessings/__init__.py @@ -217,7 +217,13 @@ 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). + + Start by trying TIOCGWINSZ (Terminal I/O-Control: Get Window Size), + falling back to environment variables (LINES, COLUMNS), and returning + (None, None) if those are unavailable or invalid. + + """ # tigetnum('lines') and tigetnum('cols') update only if we call # setupterm() again. for descriptor in self._init_descriptor, sys.__stdout__: @@ -225,8 +231,14 @@ 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 + try: + return int(environ.get('LINES')), int(environ.get('COLUMNS')) + except TypeError: + return None, None @contextmanager def location(self, x=None, y=None): diff --git a/blessings/tests.py b/blessings/tests.py index 7dda746..168c396 100644 --- a/blessings/tests.py +++ b/blessings/tests.py @@ -68,11 +68,11 @@ def test_parametrization(): eq_(TestTerminal().cup(3, 4), unicode_parm('cup', 3, 4)) -def height_and_width(): +def test_height_and_width(): """Assert that ``height_and_width()`` returns ints.""" t = TestTerminal() # kind shouldn't matter. - assert isinstance(int, t.height) - assert isinstance(int, t.width) + assert isinstance(t.height, int) + assert isinstance(t.width, int) def test_stream_attr(): |