diff options
author | Lars Wirzenius <liw@liw.fi> | 2011-01-30 19:22:38 +0000 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2011-01-30 19:22:38 +0000 |
commit | 6f813f2cdeea3a46175d0a3728a9cf3590f22802 (patch) | |
tree | e44dd021985eb76a051fa3e5ca3109daf390f2c3 /ttystatus/messager.py | |
parent | ebf5abb9285ae04f18466f22f03d3e074cff309e (diff) | |
parent | 86c254a187349ce85fdefbad4a2aeb10c41acda3 (diff) | |
download | python-ttystatus-6f813f2cdeea3a46175d0a3728a9cf3590f22802.tar.gz |
Merge changes to write to /dev/tty by default.
Diffstat (limited to 'ttystatus/messager.py')
-rw-r--r-- | ttystatus/messager.py | 22 |
1 files changed, 18 insertions, 4 deletions
diff --git a/ttystatus/messager.py b/ttystatus/messager.py index c322590..ce30e73 100644 --- a/ttystatus/messager.py +++ b/ttystatus/messager.py @@ -27,13 +27,17 @@ class Messager(object): '''Write messages to the terminal.''' def __init__(self, output=None, period=None): - self.output = output or sys.stderr + self._enabled = True + self.output = output or self._open_tty() self._period = 1.0 if period is None else period 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.set_width(self._get_terminal_width()) # Width of terminal signal.signal(signal.SIGWINCH, self._sigwinch_handler) + + def _open_tty(self): # pragma: no cover + return open('/dev/tty', 'w') def set_width(self, actual_width): self.width = actual_width - 1 @@ -72,12 +76,13 @@ class Messager(object): def _raw_write(self, string): '''Write raw data if output is terminal.''' - if self.output.isatty(): + + if self._enabled and self.output and self.output.isatty(): try: self.output.write(string) - except IOError: # pragma: no cover - # Ignored on purpose. self.output.flush() + except IOError: # pragma: no cover + self._enabled = False def _overwrite(self, string): '''Overwrite current message on terminal.''' @@ -128,3 +133,12 @@ class Messager(object): '''Finalize output.''' self._overwrite(self._cached_msg) self._raw_write('\n') + + def disable(self): + '''Disable all output.''' + self._enabled = False + + def enable(self): + '''Enable output to happen.''' + self._enabled = True + |