Add an "r" shortcut in grid editors to read value from file.

This commit is contained in:
Aldo Cortesi 2012-08-25 12:21:45 +12:00
parent 3787f8befb
commit 82893ffae2
2 changed files with 24 additions and 5 deletions

View File

@ -13,7 +13,7 @@
# You should have received a copy of the GNU General Public License # You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
import copy, re import copy, re, os
import urwid import urwid
import common import common
from .. import utils, filt from .. import utils, filt
@ -27,6 +27,7 @@ def _mkhelp():
("d", "delete row"), ("d", "delete row"),
("e", "spawn external editor on current field"), ("e", "spawn external editor on current field"),
("q", "return to flow view"), ("q", "return to flow view"),
("r", "read value from file"),
("esc", "return to flow view/exit field edit mode"), ("esc", "return to flow view/exit field edit mode"),
("tab", "next field"), ("tab", "next field"),
("enter", "edit field"), ("enter", "edit field"),
@ -113,6 +114,8 @@ class GridRow(common.WWrap):
vals.append(v) vals.append(v)
if self.editor.is_error(i, v): if self.editor.is_error(i, v):
errors.add(i) errors.add(i)
elif self.editor.encode(v) is None:
errors.add(i)
return [vals, errors] return [vals, errors]
def keypress(self, s, k): def keypress(self, s, k):
@ -286,6 +289,20 @@ class GridEditor(common.WWrap):
) )
) )
def encode(self, s):
if not self.encoding:
return s
try:
return s.encode(self.encoding)
except ValueError:
return None
def read_file(self, p):
p = os.path.expanduser(p)
d = file(p, "r").read()
self.walker.set_current_value(d)
self.walker._modified()
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"]:
@ -303,11 +320,11 @@ class GridEditor(common.WWrap):
if key in ["q", "esc"]: if key in ["q", "esc"]:
res = [] res = []
for i in self.walker.lst: for i in self.walker.lst:
if any([x.strip() for x in i[0]]): if not i[1] and any([x.strip() for x in i[0]]):
v = i[0] v = i[0]
if self.encoding: if self.encoding:
v = [x.encode(self.encoding) for x in v] v = [self.encode(x) for x in v]
res.append(v) res.append(v)
self.callback(res, *self.cb_args, **self.cb_kwargs) self.callback(res, *self.cb_args, **self.cb_kwargs)
self.master.pop_view() self.master.pop_view()
elif key in ["h", "left"]: elif key in ["h", "left"]:
@ -322,6 +339,8 @@ class GridEditor(common.WWrap):
self.walker.insert() self.walker.insert()
elif key == "d": elif key == "d":
self.walker.delete_focus() self.walker.delete_focus()
elif key == "r":
self.master.path_prompt("Read file: ", "", self.read_file)
elif key == "e": elif key == "e":
o = self.walker.get_current_value() o = self.walker.get_current_value()
if o is not None: if o is not None:

View File

@ -253,7 +253,7 @@ def clean_hanging_newline(t):
problem at the risk of removing a hanging newline in the rare cases problem at the risk of removing a hanging newline in the rare cases
where the user actually intends it. where the user actually intends it.
""" """
if t[-1] == "\n": if t and t[-1] == "\n":
return t[:-1] return t[:-1]
return t return t