diff options
author | Henrique <typoon@gmail.com> | 2019-11-24 20:13:25 -0500 |
---|---|---|
committer | Henrique <typoon@gmail.com> | 2019-11-24 20:13:25 -0500 |
commit | 7b386d5393a68715e70a9ea6d2936c8b09104f86 (patch) | |
tree | 125e23d4136921ea7dc9f2508a482dd021e2ff1f /mitmproxy | |
parent | 1d43abcb289823107bd305ed2485af0c3986a270 (diff) | |
download | mitmproxy-7b386d5393a68715e70a9ea6d2936c8b09104f86.tar.gz mitmproxy-7b386d5393a68715e70a9ea6d2936c8b09104f86.tar.bz2 mitmproxy-7b386d5393a68715e70a9ea6d2936c8b09104f86.zip |
Fixed the logic according to some tests, added new tests
Diffstat (limited to 'mitmproxy')
-rw-r--r-- | mitmproxy/addons/command_history.py | 131 | ||||
-rw-r--r-- | mitmproxy/tools/console/commander/commander.py | 30 | ||||
-rw-r--r-- | mitmproxy/tools/console/statusbar.py | 2 |
3 files changed, 68 insertions, 95 deletions
diff --git a/mitmproxy/addons/command_history.py b/mitmproxy/addons/command_history.py index 9b1fd7f1..c97cab8f 100644 --- a/mitmproxy/addons/command_history.py +++ b/mitmproxy/addons/command_history.py @@ -8,15 +8,11 @@ from mitmproxy import ctx class CommandHistory: def __init__(self, size: int = 300) -> None: - self.saved_commands: typing.Deque[str] = collections.deque( - maxlen=size - ) - self.index: int = 0 + self.saved_commands: typing.Deque[str] = collections.deque(maxlen=size) - self.filter: str = '' - self.filtered_index: int = 0 self.filtered_commands: typing.Deque[str] = collections.deque() - self.filter_active: bool = True + self.current_index: int = -1 + self.filter_str: str = '' _command_history_path = os.path.join(os.path.expanduser(ctx.options.confdir), 'command_history') _history_lines = [] @@ -26,11 +22,7 @@ class CommandHistory: self.command_history_file = open(_command_history_path, 'w') for l in _history_lines: - self.add_command(l.strip(), True) - - @property - def last_index(self): - return len(self.saved_commands) - 1 + self.add_command(l.strip()) @property def last_filtered_index(self): @@ -39,111 +31,72 @@ class CommandHistory: @command.command("command_history.clear") def clear_history(self): self.saved_commands.clear() - self.index = 0 + self.filtered_commands.clear() self.command_history_file.truncate(0) self.command_history_file.seek(0) self.command_history_file.flush() - self.filter = '' - self.filtered_index = 0 - self.filtered_commands.clear() - self.filter_active = True + self.restart() + + @command.command("command_history.cancel") + def restart(self) -> None: + self.filtered_commands = self.saved_commands.copy() + self.current_index = -1 @command.command("command_history.next") def get_next(self) -> str: - if self.last_index == -1: + + if self.current_index == -1 or self.current_index == self.last_filtered_index: + self.current_index = -1 return '' + elif self.current_index < self.last_filtered_index: + self.current_index += 1 - if self.filter != '': - if self.filtered_index < self.last_filtered_index: - self.filtered_index = self.filtered_index + 1 - ret = self.filtered_commands[self.filtered_index] - else: - if self.index == -1: - ret = '' - elif self.index < self.last_index: - self.index = self.index + 1 - ret = self.saved_commands[self.index] - else: - self.index = -1 - ret = '' + ret = self.filtered_commands[self.current_index] return ret @command.command("command_history.prev") def get_prev(self) -> str: - if self.last_index == -1: - return '' - if self.filter != '': - if self.filtered_index > 0: - self.filtered_index = self.filtered_index - 1 - ret = self.filtered_commands[self.filtered_index] - else: - if self.index == -1: - self.index = self.last_index - elif self.index > 0: - self.index = self.index - 1 + if self.current_index == -1: + if self.last_filtered_index >= 0: + self.current_index = self.last_filtered_index + else: + return '' - ret = self.saved_commands[self.index] + elif self.current_index > 0: + self.current_index -= 1 + + ret = self.filtered_commands[self.current_index] return ret @command.command("command_history.filter") def set_filter(self, command: str) -> None: - """ - This is used when the user starts typing part of a command - and then press the "up" arrow. This way, the results returned are - only for the command that the user started typing - """ - if command.strip() == '': - return - - if self.filter != '': - last_filtered_command = self.filtered_commands[-1] - if command == last_filtered_command: - self.filter = '' - self.filtered_commands = [] - self.filtered_index = 0 - else: - self.filter = command - _filtered_commands = [c for c in self.saved_commands if c.startswith(command)] - self.filtered_commands = collections.deque(_filtered_commands) - - if command not in self.filtered_commands: - self.filtered_commands.append(command) + self.filter_str = command - self.filtered_index = self.last_filtered_index + _filtered_commands = [c for c in self.saved_commands if c.startswith(command)] + self.filtered_commands = collections.deque(_filtered_commands) - # No commands found, so act like no filter was added - if len(self.filtered_commands) == 1: - self.add_command(command) - self.filter = '' + if command and command not in self.filtered_commands: + self.filtered_commands.append(command) - @command.command("command_history.cancel") - def restart(self) -> None: - self.index = -1 - self.filter = '' - self.filtered_commands = [] - self.filtered_index = 0 + self.current_index = -1 @command.command("command_history.add") - def add_command(self, command: str, execution: bool = False) -> None: + def add_command(self, command: str) -> None: if command.strip() == '': return - if execution: - if command in self.saved_commands: - self.saved_commands.remove(command) + if command in self.saved_commands: + self.saved_commands.remove(command) - self.saved_commands.append(command) + self.saved_commands.append(command) - _history_str = "\n".join(self.saved_commands) - self.command_history_file.truncate(0) - self.command_history_file.seek(0) - self.command_history_file.write(_history_str) - self.command_history_file.flush() + _history_str = "\n".join(self.saved_commands) + self.command_history_file.truncate(0) + self.command_history_file.seek(0) + self.command_history_file.write(_history_str) + self.command_history_file.flush() - self.restart() - else: - if command not in self.saved_commands: - self.saved_commands.append(command) + self.restart() diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py index 99533cfa..ac313290 100644 --- a/mitmproxy/tools/console/commander/commander.py +++ b/mitmproxy/tools/console/commander/commander.py @@ -153,26 +153,46 @@ class CommandEdit(urwid.WidgetWrap): def __init__(self, master: mitmproxy.master.Master, text: str) -> None: super().__init__(urwid.Text(self.leader)) self.master = master + self.active_filter = False + self.filter_str = '' self.cbuf = CommandBuffer(master, text) self.update() def keypress(self, size, key) -> None: if key == "backspace": self.cbuf.backspace() + if self.cbuf.text == '': + self.active_filter = False + self.master.commands.execute("command_history.filter ''") + self.filter_str = '' elif key == "left": self.cbuf.left() elif key == "right": self.cbuf.right() elif key == "up": - _cmd = command_lexer.quote(self.cbuf.text) - self.master.commands.execute("command_history.filter %s" % _cmd) + if self.active_filter is False: + self.active_filter = True + self.filter_str = self.cbuf.text + _cmd = command_lexer.quote(self.cbuf.text) + self.master.commands.execute("command_history.filter %s" % _cmd) + cmd = self.master.commands.execute("command_history.prev") self.cbuf = CommandBuffer(self.master, cmd) elif key == "down": - _cmd = command_lexer.quote(self.cbuf.text) - self.master.commands.execute("command_history.filter %s" % _cmd) + prev_cmd = self.cbuf.text cmd = self.master.commands.execute("command_history.next") - self.cbuf = CommandBuffer(self.master, cmd) + + if cmd == '': + if prev_cmd == self.filter_str: + self.cbuf = CommandBuffer(self.master, prev_cmd) + else: + self.active_filter = False + self.master.commands.execute("command_history.filter ''") + self.filter_str = '' + self.cbuf = CommandBuffer(self.master, '') + else: + self.cbuf = CommandBuffer(self.master, cmd) + elif key == "shift tab": self.cbuf.cycle_completion(False) elif key == "tab": diff --git a/mitmproxy/tools/console/statusbar.py b/mitmproxy/tools/console/statusbar.py index 6d040d92..39141b97 100644 --- a/mitmproxy/tools/console/statusbar.py +++ b/mitmproxy/tools/console/statusbar.py @@ -141,7 +141,7 @@ class ActionBar(urwid.WidgetWrap): self.prompt_execute(k) elif k == "enter": cmd = command_lexer.quote(self._w.cbuf.text) - self.master.commands.execute(f"command_history.add {cmd} true") + self.master.commands.execute(f"command_history.add {cmd}") self.prompt_execute(self._w.get_edit_text()) else: if common.is_keypress(k): |