diff options
author | Aldo Cortesi <aldo@nullcube.com> | 2012-02-08 18:25:00 +1300 |
---|---|---|
committer | Aldo Cortesi <aldo@nullcube.com> | 2012-02-08 18:25:00 +1300 |
commit | 76f2595df76f81bdbca360fe032167b6b1269a4d (patch) | |
tree | 99fbef5d9b9281dabcc6b289d3aeab132f6b80d1 /libmproxy | |
parent | 4026aa2e5f6929633b4800d55cf26698f9dd3c40 (diff) | |
download | mitmproxy-76f2595df76f81bdbca360fe032167b6b1269a4d.tar.gz mitmproxy-76f2595df76f81bdbca360fe032167b6b1269a4d.tar.bz2 mitmproxy-76f2595df76f81bdbca360fe032167b6b1269a4d.zip |
KVEditor: "e" shortcut spawns an external editor on a field.
Diffstat (limited to 'libmproxy')
-rw-r--r-- | libmproxy/console/__init__.py | 22 | ||||
-rw-r--r-- | libmproxy/console/connview.py | 24 | ||||
-rw-r--r-- | libmproxy/console/kveditor.py | 19 | ||||
-rw-r--r-- | libmproxy/utils.py | 12 |
4 files changed, 53 insertions, 24 deletions
diff --git a/libmproxy/console/__init__.py b/libmproxy/console/__init__.py index 1b0ab35f..cbc789f6 100644 --- a/libmproxy/console/__init__.py +++ b/libmproxy/console/__init__.py @@ -426,6 +426,28 @@ class ConsoleMaster(flow.FlowMaster): False ) + def spawn_editor(self, data): + fd, name = tempfile.mkstemp('', "mproxy") + os.write(fd, data) + os.close(fd) + c = os.environ.get("EDITOR") + #If no EDITOR is set, assume 'vi' + if not c: + c = "vi" + cmd = [c, name] + self.ui.stop() + try: + subprocess.call(cmd) + except: + self.statusbar.message("Can't start editor: %s" % c) + self.ui.start() + os.unlink(name) + return data + self.ui.start() + data = open(name).read() + os.unlink(name) + return data + def spawn_external_viewer(self, data, contenttype): if contenttype: ext = mimetypes.guess_extension(contenttype) or "" diff --git a/libmproxy/console/connview.py b/libmproxy/console/connview.py index ac25ea9a..0b354479 100644 --- a/libmproxy/console/connview.py +++ b/libmproxy/console/connview.py @@ -275,28 +275,6 @@ class ConnectionView(common.WWrap): else: self.view_request() - def _spawn_editor(self, data): - fd, name = tempfile.mkstemp('', "mproxy") - os.write(fd, data) - os.close(fd) - c = os.environ.get("EDITOR") - #If no EDITOR is set, assume 'vi' - if not c: - c = "vi" - cmd = [c, name] - self.master.ui.stop() - try: - subprocess.call(cmd) - except: - self.master.statusbar.message("Can't start editor: %s" % c) - self.master.ui.start() - os.unlink(name) - return data - self.master.ui.start() - data = open(name).read() - os.unlink(name) - return data - def edit_method(self, m): for i in self.methods: if i[1] == m: @@ -354,7 +332,7 @@ class ConnectionView(common.WWrap): self.flow.backup() if part == "b": - c = self._spawn_editor(conn.content or "") + c = self.master.spawn_editor(conn.content or "") conn.content = c.rstrip("\n") elif part == "h": self.master.view_kveditor("Editing headers", conn.headers.lst, self.set_headers, conn) diff --git a/libmproxy/console/kveditor.py b/libmproxy/console/kveditor.py index 40387a56..ceed2e00 100644 --- a/libmproxy/console/kveditor.py +++ b/libmproxy/console/kveditor.py @@ -1,6 +1,7 @@ import copy import urwid import common +from .. import utils class SText(common.WWrap): def __init__(self, txt, focused): @@ -76,6 +77,15 @@ class KVWalker(urwid.ListWalker): self.focus_col = 0 self.editing = False + def get_current_value(self): + if self.lst: + return self.lst[self.focus][self.focus_col] + + def set_current_value(self, val): + row = list(self.lst[self.focus]) + row[self.focus_col] = val + self.lst[self.focus] = tuple(row) + def delete_focus(self): if self.lst: del self.lst[self.focus] @@ -174,7 +184,7 @@ class KVEditor(common.WWrap): else: self.w.keypress(size, key) return None - + key = common.shortcuts(key) if key in ["q", "esc"]: self.callback(self.walker.lst, *self.cb_args, **self.cb_kwargs) @@ -191,6 +201,13 @@ class KVEditor(common.WWrap): self.walker.insert() elif key == "d": self.walker.delete_focus() + elif key == "e": + o = self.walker.get_current_value() + if o is not None: + n = self.master.spawn_editor(o) + n = utils.clean_hanging_newline(n) + self.walker.set_current_value(n) + self.walker._modified() elif key in ["enter", "e"]: self.walker.start_edit() else: diff --git a/libmproxy/utils.py b/libmproxy/utils.py index 4e53e6ce..4339ec6d 100644 --- a/libmproxy/utils.py +++ b/libmproxy/utils.py @@ -411,6 +411,18 @@ def parse_url(url): return scheme, host, port, path +def clean_hanging_newline(t): + """ + Many editors will silently add a newline to the final line of a + document (I'm looking at you, Vim). This function fixes this common + problem at the risk of removing a hanging newline in the rare cases + where the user actually intends it. + """ + if t[-1] == "\n": + return t[:-1] + return t + + def parse_size(s): """ Parses a size specification. Valid specifications are: |