summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLars Wirzenius <liw@liw.fi>2013-06-16 16:44:39 +0100
committerLars Wirzenius <liw@liw.fi>2013-06-16 16:44:39 +0100
commit0bca9f04aff010d213fe439efd6e59ca8be0c43f (patch)
tree7573d979a521c3feb845e842bc82da503fc7eca2
parentec4eb0025ef0a310bd6baed1b1bee3b2f596fdf0 (diff)
downloadttystatus-0bca9f04aff010d213fe439efd6e59ca8be0c43f.tar.gz
Stop using SIGWINCH
-rw-r--r--NEWS8
-rw-r--r--ttystatus/messager.py28
-rw-r--r--ttystatus/messager_tests.py2
3 files changed, 26 insertions, 12 deletions
diff --git a/NEWS b/NEWS
index 7a635df..9c6559a 100644
--- a/NEWS
+++ b/NEWS
@@ -1,6 +1,14 @@
NEWS file for ttystatus
=======================
+Version 0.23, released UNRELEASED
+---------------------------------
+
+* No longer use the SIGWINCH handler, since that causes problems when
+ applications do certain kinds of I/O and the signal interrupts it.
+ Libraries shouldn't install signal handlers like this. Instead,
+ query the terminal width whenever making updates.
+
Version 0.22, released 2013-03-12
---------------------------------
diff --git a/ttystatus/messager.py b/ttystatus/messager.py
index 116300e..3914013 100644
--- a/ttystatus/messager.py
+++ b/ttystatus/messager.py
@@ -15,7 +15,6 @@
import fcntl
-import signal
import struct
import sys
import termios
@@ -26,7 +25,8 @@ class Messager(object):
'''Write messages to the terminal.'''
- def __init__(self, output=None, period=None, open_tty=None):
+ def __init__(self, output=None, period=None, open_tty=None,
+ fake_width=False):
self._enabled = True
if output:
self.output = output
@@ -39,9 +39,8 @@ class Messager(object):
self._last_msg = '' # What did we write last?
self._last_time = 0 # When did we write last?
self._cached_msg = '' # Last message from user, to write() method.
+ self._fake_width = fake_width
self.set_width(self._get_terminal_width()) # Width of terminal
- signal.signal(signal.SIGWINCH, self._sigwinch_handler)
- signal.siginterrupt(signal.SIGWINCH, False)
def _open_tty(self): # pragma: no cover
return open('/dev/tty', 'w')
@@ -62,8 +61,12 @@ class Messager(object):
Borrowed and adapted from bzrlib.
'''
-
+
default_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:
@@ -77,12 +80,14 @@ class Messager(object):
return default_width
raise
- def _sigwinch_handler(self, signum, frame): # pragma: no cover
- # Clear the terminal from old stuff, using the old width.
- self.clear()
- # Get new width.
- self.set_width(self._get_terminal_width())
- self._overwrite(self._last_msg)
+ def update_width(self): # pragma: no cover
+ new_width = self._get_terminal_width()
+ if new_width != self.width:
+ # Clear the terminal from old stuff, using the old width.
+ self.clear()
+ # Get new width.
+ self.set_width(new_width)
+ self._overwrite(self._last_msg)
def _raw_write(self, string):
'''Write raw data if output is terminal.'''
@@ -107,6 +112,7 @@ class Messager(object):
def write(self, string):
'''Write raw data, always.'''
+ self.update_width()
string = string[:self.width]
self._overwrite(string)
self._last_time = self._now()
diff --git a/ttystatus/messager_tests.py b/ttystatus/messager_tests.py
index 02eaa15..40084d7 100644
--- a/ttystatus/messager_tests.py
+++ b/ttystatus/messager_tests.py
@@ -30,7 +30,7 @@ class MessagerTests(unittest.TestCase):
def setUp(self):
self.output = DummyTerminal()
- self.messager = ttystatus.Messager(output=self.output)
+ self.messager = ttystatus.Messager(output=self.output, fake_width=True)
def fast_time(self):
return self.messager._last_time + self.messager._period