aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/addons
diff options
context:
space:
mode:
authorHenrique <typoon@gmail.com>2019-11-24 20:13:25 -0500
committerHenrique <typoon@gmail.com>2019-11-24 20:13:25 -0500
commit7b386d5393a68715e70a9ea6d2936c8b09104f86 (patch)
tree125e23d4136921ea7dc9f2508a482dd021e2ff1f /mitmproxy/addons
parent1d43abcb289823107bd305ed2485af0c3986a270 (diff)
downloadmitmproxy-7b386d5393a68715e70a9ea6d2936c8b09104f86.tar.gz
mitmproxy-7b386d5393a68715e70a9ea6d2936c8b09104f86.tar.bz2
mitmproxy-7b386d5393a68715e70a9ea6d2936c8b09104f86.zip
Fixed the logic according to some tests, added new tests
Diffstat (limited to 'mitmproxy/addons')
-rw-r--r--mitmproxy/addons/command_history.py131
1 files changed, 42 insertions, 89 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()