aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAldo Cortesi <aldo@corte.si>2018-03-05 09:07:18 +1300
committerGitHub <noreply@github.com>2018-03-05 09:07:18 +1300
commitbc6550abc556cba473eb4ba14ffd3ec9c5208e7a (patch)
treef4cb20325f797011357522e39e1fe7e20150d733
parenta2740ee4aec5bd6258a0d9c17d58b7ef7d2ed3ee (diff)
parent237320a539c90411bcc27a40a6ae30fb800cda16 (diff)
downloadmitmproxy-bc6550abc556cba473eb4ba14ffd3ec9c5208e7a.tar.gz
mitmproxy-bc6550abc556cba473eb4ba14ffd3ec9c5208e7a.tar.bz2
mitmproxy-bc6550abc556cba473eb4ba14ffd3ec9c5208e7a.zip
Merge pull request #2945 from kajojify/shortening
Polite shortening of statusbar messages. Fix #1433
-rw-r--r--mitmproxy/tools/console/master.py4
-rw-r--r--mitmproxy/tools/console/statusbar.py33
-rw-r--r--test/mitmproxy/tools/console/test_statusbar.py28
3 files changed, 63 insertions, 2 deletions
diff --git a/mitmproxy/tools/console/master.py b/mitmproxy/tools/console/master.py
index 5da6ef0b..5cc1cf43 100644
--- a/mitmproxy/tools/console/master.py
+++ b/mitmproxy/tools/console/master.py
@@ -94,7 +94,9 @@ class ConsoleMaster(master.Master):
self.start_err = entry
else:
signals.status_message.send(
- message=(entry.level, "{}: {}".format(entry.level.title(), entry.msg)),
+ message=(entry.level,
+ "{}: {}".format(entry.level.title(),
+ str(entry.msg).lstrip())),
expire=5
)
diff --git a/mitmproxy/tools/console/statusbar.py b/mitmproxy/tools/console/statusbar.py
index 8553a66f..d601968e 100644
--- a/mitmproxy/tools/console/statusbar.py
+++ b/mitmproxy/tools/console/statusbar.py
@@ -49,7 +49,8 @@ class ActionBar(urwid.WidgetWrap):
def sig_message(self, sender, message, expire=1):
if self.prompting:
return
- w = urwid.Text(message)
+ cols, _ = self.master.ui.get_cols_rows()
+ w = urwid.Text(self.shorten_message(message, cols))
self._w = w
if expire:
def cb(*args):
@@ -60,6 +61,36 @@ class ActionBar(urwid.WidgetWrap):
def prep_prompt(self, p):
return p.strip() + ": "
+ def shorten_message(self, msg, max_width):
+ """
+ Shorten message so that it fits into a single line in the statusbar.
+ """
+ if isinstance(msg, tuple):
+ disp_attr, msg_text = msg
+ elif isinstance(msg, str):
+ disp_attr, msg_text = None, msg
+ else:
+ return msg
+ msg_end = "\u2026" # unicode ellipsis for the end of shortened message
+ prompt = "(more in eventlog)"
+
+ msg_lines = msg_text.split("\n")
+ first_line = msg_lines[0]
+ if len(msg_lines) > 1:
+ # First line of messages with a few lines must end with prompt.
+ line_length = len(first_line) + len(prompt)
+ else:
+ line_length = len(first_line)
+
+ if line_length > max_width:
+ shortening_index = max(0, max_width - len(prompt) - len(msg_end))
+ first_line = first_line[:shortening_index] + msg_end
+ else:
+ if len(msg_lines) == 1:
+ prompt = ""
+
+ return [(disp_attr, first_line), ("warn", prompt)]
+
def sig_prompt(self, sender, prompt, text, callback, args=()):
signals.focus.send(self, section="footer")
self._w = urwid.Edit(self.prep_prompt(prompt), text or "")
diff --git a/test/mitmproxy/tools/console/test_statusbar.py b/test/mitmproxy/tools/console/test_statusbar.py
index db8a63a7..108f238e 100644
--- a/test/mitmproxy/tools/console/test_statusbar.py
+++ b/test/mitmproxy/tools/console/test_statusbar.py
@@ -1,3 +1,5 @@
+import pytest
+
from mitmproxy import options
from mitmproxy.tools.console import statusbar, master
@@ -31,3 +33,29 @@ def test_statusbar(monkeypatch):
bar = statusbar.StatusBar(m) # this already causes a redraw
assert bar.ib._w
+
+
+@pytest.mark.parametrize("message,ready_message", [
+ ("", [(None, ""), ("warn", "")]),
+ (("info", "Line fits into statusbar"), [("info", "Line fits into statusbar"),
+ ("warn", "")]),
+ ("Line doesn't fit into statusbar", [(None, "Line doesn'\u2026"),
+ ("warn", "(more in eventlog)")]),
+ (("alert", "Two lines.\nFirst fits"), [("alert", "Two lines."),
+ ("warn", "(more in eventlog)")]),
+ ("Two long lines\nFirst doesn't fit", [(None, "Two long li\u2026"),
+ ("warn", "(more in eventlog)")])
+])
+def test_shorten_message(message, ready_message):
+ o = options.Options()
+ m = master.ConsoleMaster(o)
+ ab = statusbar.ActionBar(m)
+ assert ab.shorten_message(message, max_width=30) == ready_message
+
+
+def test_shorten_message_narrow():
+ o = options.Options()
+ m = master.ConsoleMaster(o)
+ ab = statusbar.ActionBar(m)
+ shorten_msg = ab.shorten_message("error", max_width=4)
+ assert shorten_msg == [(None, "\u2026"), ("warn", "(more in eventlog)")]