summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2015-10-15 18:29:16 +0300
committerLars Wirzenius <liw@liw.fi>2015-10-15 18:30:25 +0300
commitf9de5bcc0cd0df360d7ff7e13ce284a89fde02a9 (patch)
treeb7b4f3fe13d0ba5f7affc484350f0d85865277c3
parent9fd6ba3d79e2e3f5a1a022cf334528deeb43f793 (diff)
downloadttystatus-f9de5bcc0cd0df360d7ff7e13ce284a89fde02a9.tar.gz
Add terminal size query to public interface
-rw-r--r--NEWS3
-rw-r--r--ttystatus/messager.py4
-rw-r--r--ttystatus/status.py4
-rw-r--r--ttystatus/tty.py11
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.