From 9fd6ba3d79e2e3f5a1a022cf334528deeb43f793 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Mon, 12 Oct 2015 19:14:41 +0300 Subject: Get rid of ASCII control chars in values --- NEWS | 7 +++++++ ttystatus/status.py | 11 +++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/NEWS b/NEWS index 03a5831..4f025c0 100644 --- a/NEWS +++ b/NEWS @@ -1,6 +1,13 @@ NEWS file for ttystatus ======================= +Version 0.30, released UNRELEASED +--------------------------------- + +* Strip ASCII control characters from displayed output (but expand + TABs) in widget values. This avoids TABs making values longer than + expected, and terminals getting confused by control sequences. + Version 0.29, released 2015-10-10 --------------------------------- diff --git a/ttystatus/status.py b/ttystatus/status.py index 46e291f..148b8ff 100644 --- a/ttystatus/status.py +++ b/ttystatus/status.py @@ -117,16 +117,23 @@ class TerminalStatus(object): for i, w in enumerate(widget_row): if w.static_width: - texts[i] = w.render(0) + texts[i] = self._make_safe(w.render(0)) remaining -= len(texts[i]) for i, w in enumerate(widget_row): if not w.static_width: - texts[i] = w.render(remaining) + texts[i] = self._make_safe(w.render(remaining)) remaining -= len(texts[i]) return (''.join(texts))[:max_chars] + def _make_safe(self, line): + '''Expand TABs, remove all other ASCII control characters.''' + ASCII_SPACE = 32 + return ''.join( + c if ord(c) >= ASCII_SPACE else '' + for c in line.expandtabs()) + def _write(self): '''Render and output current state of all widgets.''' self._m.write(self._render()) -- cgit v1.2.1