aboutsummaryrefslogtreecommitdiffstats
path: root/mitmproxy/tools
diff options
context:
space:
mode:
authorHenrique <typoon@gmail.com>2019-11-23 15:31:00 -0500
committerHenrique <typoon@gmail.com>2019-11-23 15:31:00 -0500
commita866b424fe60928fb5336f1fa146326424763ca5 (patch)
treec2cdbebf181c3f15601daf472929210a59449f52 /mitmproxy/tools
parent16b55f9476373347a3c2553e070497b383288360 (diff)
downloadmitmproxy-a866b424fe60928fb5336f1fa146326424763ca5.tar.gz
mitmproxy-a866b424fe60928fb5336f1fa146326424763ca5.tar.bz2
mitmproxy-a866b424fe60928fb5336f1fa146326424763ca5.zip
Moved command history to an addon and added a new feature:
* If you start typing a command and press "up" only commands starting with that string will be returned
Diffstat (limited to 'mitmproxy/tools')
-rw-r--r--mitmproxy/tools/console/commander/commander.py89
-rw-r--r--mitmproxy/tools/console/statusbar.py11
2 files changed, 15 insertions, 85 deletions
diff --git a/mitmproxy/tools/console/commander/commander.py b/mitmproxy/tools/console/commander/commander.py
index d661d530..c59c7f83 100644
--- a/mitmproxy/tools/console/commander/commander.py
+++ b/mitmproxy/tools/console/commander/commander.py
@@ -12,6 +12,7 @@ import mitmproxy.flow
import mitmproxy.master
import mitmproxy.types
+from mitmproxy import command_lexer
class Completer:
@abc.abstractmethod
@@ -148,88 +149,13 @@ class CommandBuffer:
self.completion = None
-# TODO: This class should be a Singleton
-class CommandHistory:
- def __init__(self, master: mitmproxy.master.Master, size: int = 300) -> None:
- self.saved_commands: collections.deque = collections.deque(
- [CommandBuffer(master, "")],
- maxlen=size
- )
- self.index: int = 0
- self.size: int = size
- self.master: mitmproxy.master.Master = master
-
- _command_history_path = os.path.join(os.path.expanduser(mitmproxy.options.CONF_DIR), 'command_history')
- if os.path.exists(_command_history_path):
- with open(_command_history_path, 'r') as f:
- for l in f.readlines():
- cbuf = CommandBuffer(master, l.strip())
- self.add_command(cbuf)
- f.close()
-
- self.command_history_file = open(_command_history_path, 'w')
-
- @property
- def last_index(self):
- return len(self.saved_commands) - 1
-
- def clear_history(self):
- """
- Needed for test suite.
- TODO: Maybe create a command to clear the history?
- """
- self.saved_commands: collections.deque = collections.deque(
- [CommandBuffer(self.master, "")],
- maxlen=self.size
- )
-
- self.index = 0
- self.command_history_file.truncate(0)
- self.command_history_file.seek(0)
- self.command_history_file.flush()
-
- def get_next(self) -> typing.Optional[CommandBuffer]:
- if self.index < self.last_index:
- self.index = self.index + 1
- return self.saved_commands[self.index]
-
- def get_prev(self) -> typing.Optional[CommandBuffer]:
- if self.index > 0:
- self.index = self.index - 1
- return self.saved_commands[self.index]
-
- def add_command(self, command: CommandBuffer, execution: bool = False) -> None:
- if self.index == self.last_index or execution:
- 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.saved_commands.append(command)
- if not execution and self.index < self.last_index:
- self.index += 1
- if execution:
- self.index = self.last_index
-
- # This prevents the constructor from trying to overwrite the file
- # that it is currently reading
- if hasattr(self, 'command_history_file'):
- _history_str = "\n".join([c.text for c in 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()
-
-
class CommandEdit(urwid.WidgetWrap):
leader = ": "
- def __init__(self, master: mitmproxy.master.Master,
- text: str, history: CommandHistory) -> None:
+ def __init__(self, master: mitmproxy.master.Master, text: str) -> None:
super().__init__(urwid.Text(self.leader))
self.master = master
self.cbuf = CommandBuffer(master, text)
- self.history = history
self.update()
def keypress(self, size, key) -> None:
@@ -240,10 +166,15 @@ class CommandEdit(urwid.WidgetWrap):
elif key == "right":
self.cbuf.right()
elif key == "up":
- self.history.add_command(self.cbuf)
- self.cbuf = self.history.get_prev() or self.cbuf
+ _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":
- self.cbuf = self.history.get_next() or self.cbuf
+ _cmd = command_lexer.quote(self.cbuf.text)
+ self.master.commands.execute("command_history.filter %s" % _cmd)
+ cmd = self.master.commands.execute("command_history.next")
+ 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 43f5170d..6d040d92 100644
--- a/mitmproxy/tools/console/statusbar.py
+++ b/mitmproxy/tools/console/statusbar.py
@@ -3,10 +3,11 @@ from typing import Optional
import urwid
+import mitmproxy.tools.console.master # noqa
+from mitmproxy import command_lexer
from mitmproxy.tools.console import common
from mitmproxy.tools.console import signals
from mitmproxy.tools.console import commandexecutor
-import mitmproxy.tools.console.master # noqa
from mitmproxy.tools.console.commander import commander
@@ -43,8 +44,6 @@ class ActionBar(urwid.WidgetWrap):
signals.status_prompt_onekey.connect(self.sig_prompt_onekey)
signals.status_prompt_command.connect(self.sig_prompt_command)
- self.command_history = commander.CommandHistory(master)
-
self.prompting = None
self.onekey = False
@@ -104,7 +103,6 @@ class ActionBar(urwid.WidgetWrap):
self._w = commander.CommandEdit(
self.master,
partial,
- self.command_history,
)
if cursor is not None:
self._w.cbuf.cursor = cursor
@@ -134,7 +132,7 @@ class ActionBar(urwid.WidgetWrap):
def keypress(self, size, k):
if self.prompting:
if k == "esc":
- self.command_history.index = self.command_history.last_index
+ self.master.commands.execute('command_history.cancel')
self.prompt_done()
elif self.onekey:
if k == "enter":
@@ -142,7 +140,8 @@ class ActionBar(urwid.WidgetWrap):
elif k in self.onekey:
self.prompt_execute(k)
elif k == "enter":
- self.command_history.add_command(self._w.cbuf, True)
+ cmd = command_lexer.quote(self._w.cbuf.text)
+ self.master.commands.execute(f"command_history.add {cmd} true")
self.prompt_execute(self._w.get_edit_text())
else:
if common.is_keypress(k):