summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2015-05-01 13:13:45 +0300
committerLars Wirzenius <liw@liw.fi>2015-05-01 13:13:45 +0300
commit74f313ed31852c17698074b4c6a58a57b56b1f75 (patch)
tree0fe102324a554e831d8579abf4a498499afbc25b
parente9344ac5f34c6cbbd6a5f19c4770062e24783b10 (diff)
downloadttystatus-74f313ed31852c17698074b4c6a58a57b56b1f75.tar.gz
Refactor _get_terminal_width for clarity
Also, pylint needs to be shut up.
-rw-r--r--ttystatus/messager.py28
1 files changed, 13 insertions, 15 deletions
diff --git a/ttystatus/messager.py b/ttystatus/messager.py
index 108c3e8..cfab8ad 100644
--- a/ttystatus/messager.py
+++ b/ttystatus/messager.py
@@ -61,23 +61,21 @@ class Messager(object):
'''
- default_width = 80
+ width = 80
if self._fake_width:
if hasattr(self, 'width'):
- return self.width
- return default_width
- if self.output is None:
- return default_width
- try:
- s = struct.pack('HHHH', 0, 0, 0, 0)
- x = fcntl.ioctl(self.output.fileno(), termios.TIOCGWINSZ, s)
- return struct.unpack('HHHH', x)[1]
- except IOError:
- return default_width
- except AttributeError:
- if not hasattr(self.output, 'fileno'):
- return default_width
- raise
+ width = self.width
+ elif self.output is not None:
+ # StringIO might not have fileno. We use StringIO for tests.
+ fileno = getattr(self.output, 'fileno', None)
+ if fileno is not None:
+ try:
+ s = struct.pack('HHHH', 0, 0, 0, 0)
+ x = fcntl.ioctl(fileno(), termios.TIOCGWINSZ, s)
+ width = struct.unpack('HHHH', x)[1]
+ except IOError:
+ pass
+ return width
def update_width(self): # pragma: no cover
new_width = self._get_terminal_width()