Refine look and feel, make editor operate on copy of data.

This commit is contained in:
Aldo Cortesi 2012-02-08 16:43:11 +13:00
parent 64360f5996
commit 8b5e081233
3 changed files with 27 additions and 25 deletions

View File

@ -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()

View File

@ -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:

View File

@ -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,6 +81,7 @@ class KVWalker(urwid.ListWalker):
self._modified()
def stop_edit(self):
if self.editing:
self.lst[self.focus] = self.editing.get_kv()
self.editing = False
self._modified()
@ -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
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)