From f9de5bcc0cd0df360d7ff7e13ce284a89fde02a9 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Thu, 15 Oct 2015 18:29:16 +0300 Subject: Add terminal size query to public interface --- NEWS | 3 +++ ttystatus/messager.py | 4 ++++ ttystatus/status.py | 4 ++++ ttystatus/tty.py | 11 ++++++----- 4 files changed, 17 insertions(+), 5 deletions(-) diff --git a/NEWS b/NEWS index 4f025c0..3365fab 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,9 @@ Version 0.30, released UNRELEASED TABs) in widget values. This avoids TABs making values longer than expected, and terminals getting confused by control sequences. +* New method `ttystatus.TerminalStatus.get_terminal_size` returns + terminal width and height. + Version 0.29, released 2015-10-10 --------------------------------- diff --git a/ttystatus/messager.py b/ttystatus/messager.py index 9f81690..121ac4d 100644 --- a/ttystatus/messager.py +++ b/ttystatus/messager.py @@ -73,6 +73,10 @@ class Messager(object): # This is a wrapper around time.time(), for testing. return time.time() + def get_terminal_size(self): + '''Return terminal width, height.''' + return self._terminal.get_size() + def get_max_line_length(self): return self._area.get_max_line_length() diff --git a/ttystatus/status.py b/ttystatus/status.py index 148b8ff..df45ebc 100644 --- a/ttystatus/status.py +++ b/ttystatus/status.py @@ -37,6 +37,10 @@ class TerminalStatus(object): period=period, _terminal=_terminal) self.clear() + def get_terminal_size(self): # pragma: no cover + '''Return terminal width, height.''' + return self._m.get_terminal_size() + def add(self, widget): '''Add a new widget to the status display.''' if not self._widget_rows: diff --git a/ttystatus/tty.py b/ttystatus/tty.py index 31a9036..db0962a 100644 --- a/ttystatus/tty.py +++ b/ttystatus/tty.py @@ -62,27 +62,28 @@ class PhysicalTerminal(object): assert self._el is not None return self._cr + self._el - def get_width(self): - '''Return width of terminal in characters. + def get_size(self): + '''Return width, height of terminal in characters, rows. - If this fails, assume 80. + If this fails, assume 80 by 24. Borrowed and adapted from bzrlib. ''' width = 80 + height = 24 if self._terminal is not None: try: s = struct.pack('HHHH', 0, 0, 0, 0) x = fcntl.ioctl( self._terminal.fileno(), termios.TIOCGWINSZ, s) - width = struct.unpack('HHHH', x)[1] + height, width = struct.unpack('HHHH', x)[:2] except IOError: pass - return width + return width, height def write(self, raw_data): '''Write raw data to terminal. -- cgit v1.2.1