2012-02-23 02:52:01 +00:00
|
|
|
# Copyright (C) 2012 Aldo Cortesi
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-02-08 03:43:11 +00:00
|
|
|
import copy
|
2012-02-07 03:39:37 +00:00
|
|
|
import urwid
|
|
|
|
import common
|
2012-02-08 05:25:00 +00:00
|
|
|
from .. import utils
|
2012-02-07 03:39:37 +00:00
|
|
|
|
2012-02-08 08:47:39 +00:00
|
|
|
|
|
|
|
def _mkhelp():
|
|
|
|
text = []
|
|
|
|
keys = [
|
2012-02-08 09:55:48 +00:00
|
|
|
("A", "insert row before cursor"),
|
2012-02-08 08:47:39 +00:00
|
|
|
("a", "add row after cursor"),
|
|
|
|
("d", "delete row"),
|
|
|
|
("e", "spawn external editor on current field"),
|
|
|
|
("q", "return to flow view"),
|
|
|
|
("esc", "return to flow view/exit field edit mode"),
|
|
|
|
("tab", "next field"),
|
|
|
|
("enter", "edit field"),
|
|
|
|
]
|
|
|
|
text.extend(common.format_keyvals(keys, key="key", val="text", indent=4))
|
|
|
|
return text
|
|
|
|
help_context = _mkhelp()
|
|
|
|
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
class SText(common.WWrap):
|
2012-02-08 01:07:17 +00:00
|
|
|
def __init__(self, txt, focused):
|
2012-02-07 03:39:37 +00:00
|
|
|
w = urwid.Text(txt, wrap="any")
|
2012-02-08 01:07:17 +00:00
|
|
|
if focused:
|
2012-02-08 03:43:11 +00:00
|
|
|
w = urwid.AttrWrap(w, "focusfield")
|
2012-02-07 03:39:37 +00:00
|
|
|
common.WWrap.__init__(self, w)
|
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
def get_text(self):
|
|
|
|
return self.w.get_text()[0]
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
def keypress(self, size, key):
|
|
|
|
return key
|
|
|
|
|
|
|
|
def selectable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
class SEdit(common.WWrap):
|
|
|
|
def __init__(self, txt):
|
2012-02-08 03:43:11 +00:00
|
|
|
w = urwid.Edit(edit_text=txt, wrap="any", multiline=True)
|
|
|
|
w = urwid.AttrWrap(w, "editfield")
|
2012-02-08 01:07:17 +00:00
|
|
|
common.WWrap.__init__(self, w)
|
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
def get_text(self):
|
|
|
|
return self.w.get_text()[0]
|
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
def selectable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
class KVItem(common.WWrap):
|
|
|
|
def __init__(self, focused, editing, maxk, k, v):
|
2012-02-08 03:43:11 +00:00
|
|
|
self.focused, self.editing, self.maxk = focused, editing, maxk
|
2012-02-08 01:07:17 +00:00
|
|
|
if focused == 0 and editing:
|
2012-02-08 01:58:48 +00:00
|
|
|
self.editing = self.kf = SEdit(k)
|
2012-02-08 01:07:17 +00:00
|
|
|
else:
|
|
|
|
self.kf = SText(k, True if focused == 0 else False)
|
|
|
|
|
|
|
|
if focused == 1 and editing:
|
2012-02-08 01:58:48 +00:00
|
|
|
self.editing = self.vf = SEdit(v)
|
2012-02-08 01:07:17 +00:00
|
|
|
else:
|
|
|
|
self.vf = SText(v, True if focused == 1 else False)
|
|
|
|
|
|
|
|
w = urwid.Columns(
|
|
|
|
[
|
|
|
|
("fixed", maxk + 2, self.kf),
|
|
|
|
self.vf
|
|
|
|
],
|
|
|
|
dividechars = 2
|
|
|
|
)
|
|
|
|
if focused is not None:
|
|
|
|
w.set_focus_column(focused)
|
|
|
|
common.WWrap.__init__(self, w)
|
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
def get_kv(self):
|
|
|
|
return (self.kf.get_text(), self.vf.get_text())
|
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
def keypress(self, s, k):
|
|
|
|
if self.editing:
|
2012-02-08 03:43:11 +00:00
|
|
|
k = self.editing.keypress((s[0]-self.maxk-4,), k)
|
2012-02-08 01:07:17 +00:00
|
|
|
return k
|
|
|
|
|
|
|
|
def selectable(self):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2012-02-11 05:23:07 +00:00
|
|
|
KEY_MAX = 30
|
2012-02-08 01:07:17 +00:00
|
|
|
class KVWalker(urwid.ListWalker):
|
2012-02-08 22:30:35 +00:00
|
|
|
def __init__(self, lst, editor):
|
|
|
|
self.lst, self.editor = lst, editor
|
2012-02-11 05:23:07 +00:00
|
|
|
self.maxk = min(max(len(v[0]) for v in lst), KEY_MAX) if lst else 20
|
2012-02-08 10:42:56 +00:00
|
|
|
if self.maxk < 20:
|
|
|
|
self.maxk = 20
|
2012-02-08 01:07:17 +00:00
|
|
|
self.focus = 0
|
|
|
|
self.focus_col = 0
|
|
|
|
self.editing = False
|
|
|
|
|
2012-02-08 22:30:35 +00:00
|
|
|
def _modified(self):
|
|
|
|
self.editor.show_empty_msg()
|
|
|
|
return urwid.ListWalker._modified(self)
|
|
|
|
|
2012-02-08 05:25:00 +00:00
|
|
|
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)
|
|
|
|
|
2012-02-08 03:55:11 +00:00
|
|
|
def delete_focus(self):
|
|
|
|
if self.lst:
|
|
|
|
del self.lst[self.focus]
|
|
|
|
self.focus = min(len(self.lst)-1, self.focus)
|
|
|
|
self._modified()
|
|
|
|
|
2012-02-08 04:52:43 +00:00
|
|
|
def _insert(self, pos):
|
|
|
|
self.focus = pos
|
2012-02-08 03:55:11 +00:00
|
|
|
self.lst.insert(self.focus, ("", ""))
|
|
|
|
self.focus_col = 0
|
|
|
|
self.start_edit()
|
|
|
|
|
2012-02-08 04:52:43 +00:00
|
|
|
def insert(self):
|
|
|
|
return self._insert(self.focus)
|
|
|
|
|
|
|
|
def add(self):
|
|
|
|
return self._insert(min(self.focus + 1, len(self.lst)))
|
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
def start_edit(self):
|
2012-02-08 22:32:29 +00:00
|
|
|
if self.lst:
|
|
|
|
self.editing = KVItem(self.focus_col, True, self.maxk, *self.lst[self.focus])
|
|
|
|
self._modified()
|
2012-02-08 01:07:17 +00:00
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
def stop_edit(self):
|
2012-02-08 03:43:11 +00:00
|
|
|
if self.editing:
|
|
|
|
self.lst[self.focus] = self.editing.get_kv()
|
|
|
|
self.editing = False
|
|
|
|
self._modified()
|
2012-02-08 01:58:48 +00:00
|
|
|
|
2012-02-08 01:07:17 +00:00
|
|
|
def left(self):
|
|
|
|
self.focus_col = 0
|
|
|
|
self._modified()
|
|
|
|
|
|
|
|
def right(self):
|
|
|
|
self.focus_col = 1
|
|
|
|
self._modified()
|
|
|
|
|
|
|
|
def tab_next(self):
|
2012-02-08 03:55:11 +00:00
|
|
|
self.stop_edit()
|
2012-02-08 01:07:17 +00:00
|
|
|
if self.focus_col == 0:
|
|
|
|
self.focus_col = 1
|
|
|
|
elif self.focus != len(self.lst)-1:
|
|
|
|
self.focus_col = 0
|
|
|
|
self.focus += 1
|
|
|
|
self._modified()
|
|
|
|
|
|
|
|
def get_focus(self):
|
|
|
|
if self.editing:
|
|
|
|
return self.editing, self.focus
|
2012-02-08 03:55:11 +00:00
|
|
|
elif self.lst:
|
2012-02-08 01:07:17 +00:00
|
|
|
return KVItem(self.focus_col, False, self.maxk, *self.lst[self.focus]), self.focus
|
2012-02-08 03:55:11 +00:00
|
|
|
else:
|
|
|
|
return None, None
|
2012-02-08 01:07:17 +00:00
|
|
|
|
|
|
|
def set_focus(self, focus):
|
2012-02-08 03:43:11 +00:00
|
|
|
self.stop_edit()
|
2012-02-08 01:07:17 +00:00
|
|
|
self.focus = focus
|
|
|
|
|
|
|
|
def get_next(self, pos):
|
|
|
|
if pos+1 >= len(self.lst):
|
|
|
|
return None, None
|
|
|
|
return KVItem(None, False, self.maxk, *self.lst[pos+1]), pos+1
|
|
|
|
|
|
|
|
def get_prev(self, pos):
|
|
|
|
if pos-1 < 0:
|
|
|
|
return None, None
|
|
|
|
return KVItem(None, False, self.maxk, *self.lst[pos-1]), pos-1
|
|
|
|
|
|
|
|
|
|
|
|
class KVListBox(urwid.ListBox):
|
|
|
|
def __init__(self, lw):
|
|
|
|
urwid.ListBox.__init__(self, lw)
|
|
|
|
|
|
|
|
|
2012-02-07 03:39:37 +00:00
|
|
|
class KVEditor(common.WWrap):
|
2012-02-08 03:43:11 +00:00
|
|
|
def __init__(self, master, title, value, callback, *cb_args, **cb_kwargs):
|
|
|
|
value = copy.deepcopy(value)
|
2012-02-07 03:39:37 +00:00
|
|
|
self.master, self.title, self.value, self.callback = master, title, value, callback
|
2012-02-08 03:43:11 +00:00
|
|
|
self.cb_args, self.cb_kwargs = cb_args, cb_kwargs
|
2012-02-07 03:39:37 +00:00
|
|
|
p = urwid.Text(title)
|
|
|
|
p = urwid.Padding(p, align="left", width=("relative", 100))
|
|
|
|
p = urwid.AttrWrap(p, "heading")
|
2012-02-08 22:30:35 +00:00
|
|
|
self.walker = KVWalker(self.value, self)
|
2012-02-08 01:07:17 +00:00
|
|
|
self.lb = KVListBox(self.walker)
|
2012-02-07 03:39:37 +00:00
|
|
|
self.w = urwid.Frame(self.lb, header = p)
|
|
|
|
self.master.statusbar.update("")
|
2012-02-08 22:30:35 +00:00
|
|
|
self.show_empty_msg()
|
|
|
|
|
|
|
|
def show_empty_msg(self):
|
|
|
|
if self.walker.lst:
|
|
|
|
self.w.set_footer(None)
|
|
|
|
else:
|
|
|
|
self.w.set_footer(
|
|
|
|
urwid.Text(
|
|
|
|
[
|
|
|
|
("highlight", "No values. Press "),
|
|
|
|
("key", "a"),
|
|
|
|
("highlight", " to add some."),
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
2012-02-07 03:39:37 +00:00
|
|
|
|
|
|
|
def keypress(self, size, key):
|
2012-02-08 01:07:17 +00:00
|
|
|
if self.walker.editing:
|
2012-02-08 03:43:11 +00:00
|
|
|
if key in ["esc", "enter"]:
|
2012-02-08 01:58:48 +00:00
|
|
|
self.walker.stop_edit()
|
2012-02-08 03:55:11 +00:00
|
|
|
elif key == "tab":
|
2012-02-08 22:36:10 +00:00
|
|
|
pf, pfc = self.walker.focus, self.walker.focus_col
|
2012-02-08 03:55:11 +00:00
|
|
|
self.walker.tab_next()
|
2012-02-08 22:36:10 +00:00
|
|
|
if self.walker.focus == pf and self.walker.focus_col != pfc:
|
2012-02-08 04:55:17 +00:00
|
|
|
self.walker.start_edit()
|
2012-02-07 03:39:37 +00:00
|
|
|
else:
|
2012-02-08 01:58:48 +00:00
|
|
|
self.w.keypress(size, key)
|
2012-02-08 03:43:11 +00:00
|
|
|
return None
|
2012-02-08 05:25:00 +00:00
|
|
|
|
2012-02-08 01:58:48 +00:00
|
|
|
key = common.shortcuts(key)
|
2012-02-08 03:43:11 +00:00
|
|
|
if key in ["q", "esc"]:
|
|
|
|
self.callback(self.walker.lst, *self.cb_args, **self.cb_kwargs)
|
2012-02-08 01:58:48 +00:00
|
|
|
self.master.pop_view()
|
|
|
|
elif key in ["h", "left"]:
|
|
|
|
self.walker.left()
|
|
|
|
elif key in ["l", "right"]:
|
|
|
|
self.walker.right()
|
|
|
|
elif key == "tab":
|
|
|
|
self.walker.tab_next()
|
2012-02-08 03:55:11 +00:00
|
|
|
elif key == "a":
|
|
|
|
self.walker.add()
|
2012-02-08 09:55:48 +00:00
|
|
|
elif key == "A":
|
2012-02-08 04:52:43 +00:00
|
|
|
self.walker.insert()
|
2012-02-08 03:55:11 +00:00
|
|
|
elif key == "d":
|
|
|
|
self.walker.delete_focus()
|
2012-02-08 05:25:00 +00:00
|
|
|
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()
|
2012-02-08 08:47:39 +00:00
|
|
|
elif key in ["enter"]:
|
2012-02-08 01:58:48 +00:00
|
|
|
self.walker.start_edit()
|
|
|
|
else:
|
|
|
|
return self.w.keypress(size, key)
|