From 046779615f0a492d567300f08de79eaf5f780d84 Mon Sep 17 00:00:00 2001 From: Henrique Date: Mon, 25 Nov 2019 15:41:58 -0500 Subject: [PATCH 1/3] Added some navigation keys to the command bar same way as in a Linux terminal --- .../tools/console/commander/commander.py | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index d751422b2..feaa4e368 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -67,6 +67,11 @@ class CommandBuffer: else: self._cursor = x + def set_text(self, text: str) -> None: + self.text = text + self._cursor = len(self.text) + self.render() + def render(self): parts, remaining = self.master.commands.parse_partial(self.text) ret = [] @@ -133,6 +138,12 @@ class CommandBuffer: self.cursor = self.cursor - 1 self.completion = None + def delete(self) -> None: + if self.cursor == len(self.text): + return + self.text = self.text[:self.cursor] + self.text[self.cursor + 1:] + self.completion = None + def insert(self, k: str) -> None: """ Inserts text at the cursor. @@ -197,16 +208,37 @@ class CommandEdit(urwid.WidgetWrap): self.update() def keypress(self, size, key) -> None: - if key == "backspace": + if key == "delete": + self.cbuf.delete() + elif key == "ctrl a" or key == 'home': + self.cbuf.cursor = 0 + elif key == "ctrl e" or key == 'end': + self.cbuf.cursor = len(self.cbuf.text) + elif key == "meta b": + self.cbuf.cursor = self.cbuf.text.rfind(' ', 0, self.cbuf.cursor) + elif key == "meta f": + pos = self.cbuf.text.find(' ', self.cbuf.cursor + 1) + if pos == -1: + pos = len(self.cbuf.text) + self.cbuf.cursor = pos + elif key == "ctrl w": + txt = self.cbuf.text.strip() + if(txt != ''): + chunks = txt.split(' ')[0:-1] + if len(chunks) == 0: + self.cbuf.set_text(' '.join(chunks)) + else: + self.cbuf.set_text(' '.join(chunks) + ' ') + elif key == "backspace": self.cbuf.backspace() - elif key == "left": + elif key == "left" or key == "ctrl b": self.cbuf.left() - elif key == "right": + elif key == "right" or key == "ctrl f": self.cbuf.right() - elif key == "up": + elif key == "up" or key == "ctrl p": self.history.add_command(self.cbuf) self.cbuf = self.history.get_prev() or self.cbuf - elif key == "down": + elif key == "down" or key == "ctrl n": self.cbuf = self.history.get_next() or self.cbuf elif key == "shift tab": self.cbuf.cycle_completion(False) From 8449db103ea8a8514d9c1b94ddb197efee689240 Mon Sep 17 00:00:00 2001 From: Henrique Date: Mon, 25 Nov 2019 18:55:12 -0500 Subject: [PATCH 2/3] Fixed `ctrl w` --- .../tools/console/commander/commander.py | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index feaa4e368..46c1cb96b 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -222,13 +222,19 @@ class CommandEdit(urwid.WidgetWrap): pos = len(self.cbuf.text) self.cbuf.cursor = pos elif key == "ctrl w": - txt = self.cbuf.text.strip() - if(txt != ''): - chunks = txt.split(' ')[0:-1] - if len(chunks) == 0: - self.cbuf.set_text(' '.join(chunks)) - else: - self.cbuf.set_text(' '.join(chunks) + ' ') + prev_cursor = self.cbuf.cursor + pos = self.cbuf.text.rfind(' ', 0, self.cbuf.cursor - 1) + if pos == -1: + new_text = self.cbuf.text[self.cbuf.cursor:] + cursor_pos = 0 + else: + txt_after = self.cbuf.text[self.cbuf.cursor:] + txt_before = self.cbuf.text[0:pos] + new_text = f"{txt_before} {txt_after}" + cursor_pos = prev_cursor - (prev_cursor - pos) + 1 + + self.cbuf.set_text(new_text) + self.cbuf.cursor = cursor_pos elif key == "backspace": self.cbuf.backspace() elif key == "left" or key == "ctrl b": From b3b069f4680cbb7675d0d3d7cd738f55b5f48275 Mon Sep 17 00:00:00 2001 From: Henrique Date: Mon, 25 Nov 2019 19:00:54 -0500 Subject: [PATCH 3/3] Remove blank line --- mitmproxy/tools/console/commander/commander.py | 1 - 1 file changed, 1 deletion(-) diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index 46c1cb96b..02eda3bf9 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -232,7 +232,6 @@ class CommandEdit(urwid.WidgetWrap): txt_before = self.cbuf.text[0:pos] new_text = f"{txt_before} {txt_after}" cursor_pos = prev_cursor - (prev_cursor - pos) + 1 - self.cbuf.set_text(new_text) self.cbuf.cursor = cursor_pos elif key == "backspace":