mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-11-23 00:01:36 +00:00
Some refactoring. New test case.
This commit is contained in:
parent
ffbd7c20e5
commit
dcb3de40b1
@ -160,7 +160,7 @@ class CommandBuffer:
|
||||
|
||||
class CommandHistory:
|
||||
def __init__(self, master: mitmproxy.master.Master, size: int=30) -> None:
|
||||
self.history: collections.deque = collections.deque(
|
||||
self.saved_commands: collections.deque = collections.deque(
|
||||
[CommandBuffer(master, "")],
|
||||
maxlen=size
|
||||
)
|
||||
@ -168,27 +168,28 @@ class CommandHistory:
|
||||
|
||||
@property
|
||||
def last_index(self):
|
||||
return len(self.history) - 1
|
||||
return len(self.saved_commands) - 1
|
||||
|
||||
def get_next(self) -> typing.Optional[CommandBuffer]:
|
||||
if self.index < self.last_index:
|
||||
self.index = self.index + 1
|
||||
return self.history[self.index]
|
||||
return self.saved_commands[self.index]
|
||||
return None
|
||||
|
||||
def get_prev(self) -> typing.Optional[CommandBuffer]:
|
||||
if self.index > 0:
|
||||
self.index = self.index - 1
|
||||
return self.history[self.index]
|
||||
return self.saved_commands[self.index]
|
||||
return None
|
||||
|
||||
def add_command(self, command: CommandBuffer, execution: bool=False) -> None:
|
||||
if self.index == self.last_index or execution:
|
||||
last_item_empty = not self.history[-1].text
|
||||
if self.history[-1].text == command.text or (last_item_empty and execution):
|
||||
self.history[-1] = copy.copy(command)
|
||||
last_item = self.saved_commands[-1]
|
||||
last_item_empty = not last_item.text
|
||||
if last_item.text == command.text or (last_item_empty and execution):
|
||||
self.saved_commands[-1] = copy.copy(command)
|
||||
else:
|
||||
self.history.append(command)
|
||||
self.saved_commands.append(command)
|
||||
if not execution and self.index < self.last_index:
|
||||
self.index += 1
|
||||
if execution:
|
||||
|
@ -100,7 +100,8 @@ class ActionBar(urwid.WidgetWrap):
|
||||
|
||||
def sig_prompt_command(self, sender, partial=""):
|
||||
signals.focus.send(self, section="footer")
|
||||
self._w = commander.CommandEdit(self.master, partial, self.command_history)
|
||||
self._w = commander.CommandEdit(self.master, partial,
|
||||
self.command_history)
|
||||
self.prompting = commandexecutor.CommandExecutor(self.master)
|
||||
|
||||
def sig_prompt_onekey(self, sender, prompt, keys, callback, args=()):
|
||||
|
@ -41,20 +41,31 @@ class TestCommandHistory:
|
||||
commands = ["command1", "command2"]
|
||||
history, tctx_master = self.fill_history(commands)
|
||||
|
||||
history_commands = [buf.text for buf in history.history]
|
||||
assert history_commands == [""] + commands
|
||||
saved_commands = [buf.text for buf in history.saved_commands]
|
||||
assert saved_commands == [""] + commands
|
||||
|
||||
# The history size is only 3. So, we forget the first one command,
|
||||
# when adding fourth command
|
||||
# The history size is only 3. So, we forget the first
|
||||
# one command, when adding fourth command
|
||||
cbuf = commander.CommandBuffer(tctx_master, "command3")
|
||||
history.add_command(cbuf)
|
||||
history_commands = [buf.text for buf in history.history]
|
||||
assert history_commands == commands + ["command3"]
|
||||
saved_commands = [buf.text for buf in history.saved_commands]
|
||||
assert saved_commands == commands + ["command3"]
|
||||
|
||||
# Commands with the same text are not repeated in the history one by one
|
||||
history.add_command(cbuf)
|
||||
history_commands = [buf.text for buf in history.history]
|
||||
assert history_commands == commands + ["command3"]
|
||||
saved_commands = [buf.text for buf in history.saved_commands]
|
||||
assert saved_commands == commands + ["command3"]
|
||||
|
||||
# adding command in execution mode sets index at the beginning of the history
|
||||
# and replace the last command buffer if it is empty or has the same text
|
||||
cbuf = commander.CommandBuffer(tctx_master, "")
|
||||
history.add_command(cbuf)
|
||||
history.index = 0
|
||||
cbuf = commander.CommandBuffer(tctx_master, "command4")
|
||||
history.add_command(cbuf, True)
|
||||
assert history.index == history.last_index
|
||||
saved_commands = [buf.text for buf in history.saved_commands]
|
||||
assert saved_commands == ["command2", "command3", "command4"]
|
||||
|
||||
def test_get_next(self):
|
||||
commands = ["command1", "command2"]
|
||||
|
Loading…
Reference in New Issue
Block a user