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'), ('offset', 'dark cyan', 'default'),
# KV Editor # KV Editor
('focusfield', 'dark cyan', 'default'),
('editfield', 'black', 'light cyan'), ('editfield', 'black', 'light cyan'),
] ]
@ -536,9 +537,9 @@ class ConsoleMaster(flow.FlowMaster):
self.header = None self.header = None
self.make_view() 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.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.header = None
self.make_view() self.make_view()

View File

@ -1,6 +1,6 @@
import urwid import urwid
import common import common
from .. import utils, encoding from .. import utils, encoding, flow
VIEW_CUTOFF = 1024*100 VIEW_CUTOFF = 1024*100
@ -341,6 +341,9 @@ class ConnectionView(common.WWrap):
response.msg = msg response.msg = msg
self.master.refresh_connection(self.flow) self.master.refresh_connection(self.flow)
def set_headers(self, lst, conn):
conn.headers = flow.Headers(lst)
def edit(self, part): def edit(self, part):
if self.state.view_flow_mode == common.VIEW_FLOW_REQUEST: if self.state.view_flow_mode == common.VIEW_FLOW_REQUEST:
conn = self.flow.request conn = self.flow.request
@ -354,12 +357,7 @@ class ConnectionView(common.WWrap):
c = self._spawn_editor(conn.content or "") c = self._spawn_editor(conn.content or "")
conn.content = c.rstrip("\n") conn.content = c.rstrip("\n")
elif part == "h": elif part == "h":
self.master.view_kveditor("Editing headers", conn.headers.lst, None) self.master.view_kveditor("Editing headers", conn.headers.lst, self.set_headers, conn)
#headertext = self._spawn_editor(repr(conn.headers))
#headers = flow.Headers()
#fp = cStringIO.StringIO(headertext)
#headers.read(fp)
#conn.headers = headers
elif part == "u" and self.state.view_flow_mode == common.VIEW_FLOW_REQUEST: elif part == "u" and self.state.view_flow_mode == common.VIEW_FLOW_REQUEST:
self.master.prompt_edit("URL", conn.get_url(), self.set_url) self.master.prompt_edit("URL", conn.get_url(), self.set_url)
elif part == "m" and self.state.view_flow_mode == common.VIEW_FLOW_REQUEST: 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 urwid
import common import common
@ -6,7 +6,7 @@ class SText(common.WWrap):
def __init__(self, txt, focused): def __init__(self, txt, focused):
w = urwid.Text(txt, wrap="any") w = urwid.Text(txt, wrap="any")
if focused: if focused:
w = urwid.AttrWrap(w, "editfield") w = urwid.AttrWrap(w, "focusfield")
common.WWrap.__init__(self, w) common.WWrap.__init__(self, w)
def get_text(self): def get_text(self):
@ -21,7 +21,8 @@ class SText(common.WWrap):
class SEdit(common.WWrap): class SEdit(common.WWrap):
def __init__(self, txt): 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) common.WWrap.__init__(self, w)
def get_text(self): def get_text(self):
@ -33,7 +34,7 @@ class SEdit(common.WWrap):
class KVItem(common.WWrap): class KVItem(common.WWrap):
def __init__(self, focused, editing, maxk, k, v): 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: if focused == 0 and editing:
self.editing = self.kf = SEdit(k) self.editing = self.kf = SEdit(k)
else: else:
@ -60,7 +61,7 @@ class KVItem(common.WWrap):
def keypress(self, s, k): def keypress(self, s, k):
if self.editing: if self.editing:
k = self.editing.keypress(s, k) k = self.editing.keypress((s[0]-self.maxk-4,), k)
return k return k
def selectable(self): def selectable(self):
@ -80,9 +81,10 @@ class KVWalker(urwid.ListWalker):
self._modified() self._modified()
def stop_edit(self): def stop_edit(self):
self.lst[self.focus] = self.editing.get_kv() if self.editing:
self.editing = False self.lst[self.focus] = self.editing.get_kv()
self._modified() self.editing = False
self._modified()
def left(self): def left(self):
self.focus_col = 0 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 return KVItem(self.focus_col, False, self.maxk, *self.lst[self.focus]), self.focus
def set_focus(self, focus): def set_focus(self, focus):
self.stop_edit()
self.focus = focus self.focus = focus
def get_next(self, pos): def get_next(self, pos):
@ -126,8 +129,10 @@ class KVListBox(urwid.ListBox):
class KVEditor(common.WWrap): 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.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.Text(title)
p = urwid.Padding(p, align="left", width=("relative", 100)) p = urwid.Padding(p, align="left", width=("relative", 100))
p = urwid.AttrWrap(p, "heading") p = urwid.AttrWrap(p, "heading")
@ -138,17 +143,15 @@ class KVEditor(common.WWrap):
def keypress(self, size, key): def keypress(self, size, key):
if self.walker.editing: if self.walker.editing:
if key in ["esc"]: if key in ["esc", "enter"]:
self.walker.stop_edit()
return None
elif key in ["left", "right", "up", "down"]:
self.walker.stop_edit() self.walker.stop_edit()
else: else:
self.w.keypress(size, key) self.w.keypress(size, key)
return None return None
key = common.shortcuts(key) 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() self.master.pop_view()
elif key in ["h", "left"]: elif key in ["h", "left"]:
self.walker.left() self.walker.left()
@ -156,7 +159,7 @@ class KVEditor(common.WWrap):
self.walker.right() self.walker.right()
elif key == "tab": elif key == "tab":
self.walker.tab_next() self.walker.tab_next()
elif key == "enter": elif key in ["enter", "e"]:
self.walker.start_edit() self.walker.start_edit()
else: else:
return self.w.keypress(size, key) return self.w.keypress(size, key)