From 8b5e0812333db79e10abef91a690932bce15511f Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Wed, 8 Feb 2012 16:43:11 +1300 Subject: [PATCH] Refine look and feel, make editor operate on copy of data. --- libmproxy/console/__init__.py | 5 +++-- libmproxy/console/connview.py | 12 +++++------- libmproxy/console/kveditor.py | 35 +++++++++++++++++++---------------- 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/libmproxy/console/__init__.py b/libmproxy/console/__init__.py index 6fc76160e..3e98ed2d3 100644 --- a/libmproxy/console/__init__.py +++ b/libmproxy/console/__init__.py @@ -486,6 +486,7 @@ class ConsoleMaster(flow.FlowMaster): ('offset', 'dark cyan', 'default'), # KV Editor + ('focusfield', 'dark cyan', 'default'), ('editfield', 'black', 'light cyan'), ] @@ -536,9 +537,9 @@ class ConsoleMaster(flow.FlowMaster): self.header = None self.make_view() - def view_kveditor(self, title, value, callback): + def view_kveditor(self, title, value, callback, *args, **kwargs): self.statusbar = StatusBar(self, "foo") - self.body = kveditor.KVEditor(self, title, value, callback) + self.body = kveditor.KVEditor(self, title, value, callback, *args, **kwargs) self.header = None self.make_view() diff --git a/libmproxy/console/connview.py b/libmproxy/console/connview.py index f4f6c6622..ac25ea9ad 100644 --- a/libmproxy/console/connview.py +++ b/libmproxy/console/connview.py @@ -1,6 +1,6 @@ import urwid import common -from .. import utils, encoding +from .. import utils, encoding, flow VIEW_CUTOFF = 1024*100 @@ -341,6 +341,9 @@ class ConnectionView(common.WWrap): response.msg = msg self.master.refresh_connection(self.flow) + def set_headers(self, lst, conn): + conn.headers = flow.Headers(lst) + def edit(self, part): if self.state.view_flow_mode == common.VIEW_FLOW_REQUEST: conn = self.flow.request @@ -354,12 +357,7 @@ class ConnectionView(common.WWrap): c = self._spawn_editor(conn.content or "") conn.content = c.rstrip("\n") elif part == "h": - self.master.view_kveditor("Editing headers", conn.headers.lst, None) - #headertext = self._spawn_editor(repr(conn.headers)) - #headers = flow.Headers() - #fp = cStringIO.StringIO(headertext) - #headers.read(fp) - #conn.headers = headers + self.master.view_kveditor("Editing headers", conn.headers.lst, self.set_headers, conn) elif part == "u" and self.state.view_flow_mode == common.VIEW_FLOW_REQUEST: self.master.prompt_edit("URL", conn.get_url(), self.set_url) elif part == "m" and self.state.view_flow_mode == common.VIEW_FLOW_REQUEST: diff --git a/libmproxy/console/kveditor.py b/libmproxy/console/kveditor.py index 6d708de0a..dae303e9a 100644 --- a/libmproxy/console/kveditor.py +++ b/libmproxy/console/kveditor.py @@ -1,4 +1,4 @@ -import time +import copy import urwid import common @@ -6,7 +6,7 @@ class SText(common.WWrap): def __init__(self, txt, focused): w = urwid.Text(txt, wrap="any") if focused: - w = urwid.AttrWrap(w, "editfield") + w = urwid.AttrWrap(w, "focusfield") common.WWrap.__init__(self, w) def get_text(self): @@ -21,7 +21,8 @@ class SText(common.WWrap): class SEdit(common.WWrap): def __init__(self, txt): - w = urwid.Edit(txt, wrap="any", multiline=True) + w = urwid.Edit(edit_text=txt, wrap="any", multiline=True) + w = urwid.AttrWrap(w, "editfield") common.WWrap.__init__(self, w) def get_text(self): @@ -33,7 +34,7 @@ class SEdit(common.WWrap): class KVItem(common.WWrap): def __init__(self, focused, editing, maxk, k, v): - self.focused, self.editing = focused, editing + self.focused, self.editing, self.maxk = focused, editing, maxk if focused == 0 and editing: self.editing = self.kf = SEdit(k) else: @@ -60,7 +61,7 @@ class KVItem(common.WWrap): def keypress(self, s, k): if self.editing: - k = self.editing.keypress(s, k) + k = self.editing.keypress((s[0]-self.maxk-4,), k) return k def selectable(self): @@ -80,9 +81,10 @@ class KVWalker(urwid.ListWalker): self._modified() def stop_edit(self): - self.lst[self.focus] = self.editing.get_kv() - self.editing = False - self._modified() + if self.editing: + self.lst[self.focus] = self.editing.get_kv() + self.editing = False + self._modified() def left(self): self.focus_col = 0 @@ -107,6 +109,7 @@ class KVWalker(urwid.ListWalker): return KVItem(self.focus_col, False, self.maxk, *self.lst[self.focus]), self.focus def set_focus(self, focus): + self.stop_edit() self.focus = focus def get_next(self, pos): @@ -126,8 +129,10 @@ class KVListBox(urwid.ListBox): class KVEditor(common.WWrap): - def __init__(self, master, title, value, callback): + def __init__(self, master, title, value, callback, *cb_args, **cb_kwargs): + value = copy.deepcopy(value) self.master, self.title, self.value, self.callback = master, title, value, callback + self.cb_args, self.cb_kwargs = cb_args, cb_kwargs p = urwid.Text(title) p = urwid.Padding(p, align="left", width=("relative", 100)) p = urwid.AttrWrap(p, "heading") @@ -138,17 +143,15 @@ class KVEditor(common.WWrap): def keypress(self, size, key): if self.walker.editing: - if key in ["esc"]: - self.walker.stop_edit() - return None - elif key in ["left", "right", "up", "down"]: + if key in ["esc", "enter"]: self.walker.stop_edit() else: self.w.keypress(size, key) - return None + return None key = common.shortcuts(key) - if key == ["q", "esc"]: + if key in ["q", "esc"]: + self.callback(self.walker.lst, *self.cb_args, **self.cb_kwargs) self.master.pop_view() elif key in ["h", "left"]: self.walker.left() @@ -156,7 +159,7 @@ class KVEditor(common.WWrap): self.walker.right() elif key == "tab": self.walker.tab_next() - elif key == "enter": + elif key in ["enter", "e"]: self.walker.start_edit() else: return self.w.keypress(size, key)