From 237320a539c90411bcc27a40a6ae30fb800cda16 Mon Sep 17 00:00:00 2001 From: Miroslav Date: Sat, 3 Mar 2018 22:12:56 +0200 Subject: Minor fixes. Renaming prep_message method to shorten_message. Docstring. \u2026 instead of .... max_width parameter. --- mitmproxy/tools/console/statusbar.py | 16 ++++++++++------ test/mitmproxy/tools/console/test_statusbar.py | 23 +++++++++++------------ 2 files changed, 21 insertions(+), 18 deletions(-) diff --git a/mitmproxy/tools/console/statusbar.py b/mitmproxy/tools/console/statusbar.py index f65031b3..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(self.prep_message(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,14 +61,17 @@ class ActionBar(urwid.WidgetWrap): def prep_prompt(self, p): return p.strip() + ": " - def prep_message(self, msg): + 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 - cols, _ = self.master.ui.get_cols_rows() + msg_end = "\u2026" # unicode ellipsis for the end of shortened message prompt = "(more in eventlog)" msg_lines = msg_text.split("\n") @@ -78,9 +82,9 @@ class ActionBar(urwid.WidgetWrap): else: line_length = len(first_line) - if line_length > cols: - shortening_index = max(0, cols - len(prompt) - 3) - first_line = first_line[:shortening_index] + "..." + 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 = "" diff --git a/test/mitmproxy/tools/console/test_statusbar.py b/test/mitmproxy/tools/console/test_statusbar.py index 7b991890..108f238e 100644 --- a/test/mitmproxy/tools/console/test_statusbar.py +++ b/test/mitmproxy/tools/console/test_statusbar.py @@ -2,7 +2,6 @@ import pytest from mitmproxy import options from mitmproxy.tools.console import statusbar, master -from unittest import mock def test_statusbar(monkeypatch): @@ -40,23 +39,23 @@ def test_statusbar(monkeypatch): ("", [(None, ""), ("warn", "")]), (("info", "Line fits into statusbar"), [("info", "Line fits into statusbar"), ("warn", "")]), - ("Line doesn't fit into statusbar", [(None, "Line does..."), + ("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 ..."), + ("Two long lines\nFirst doesn't fit", [(None, "Two long li\u2026"), ("warn", "(more in eventlog)")]) ]) -def test_prep_message(message, ready_message): - m = mock.Mock() - m.ui.get_cols_rows.return_value = (30, 30) +def test_shorten_message(message, ready_message): + o = options.Options() + m = master.ConsoleMaster(o) ab = statusbar.ActionBar(m) - assert ab.prep_message(message) == ready_message + assert ab.shorten_message(message, max_width=30) == ready_message -def test_prep_message_narrow(): - m = mock.Mock() - m.ui.get_cols_rows.return_value = (4, 4) +def test_shorten_message_narrow(): + o = options.Options() + m = master.ConsoleMaster(o) ab = statusbar.ActionBar(m) - prep_msg = ab.prep_message("error") - assert prep_msg == [(None, "..."), ("warn", "(more in eventlog)")] + shorten_msg = ab.shorten_message("error", max_width=4) + assert shorten_msg == [(None, "\u2026"), ("warn", "(more in eventlog)")] -- cgit v1.2.3