diff options
author | Rob Lanphier <robla@robla.net> | 2010-03-19 16:40:49 -0700 |
---|---|---|
committer | Rob Lanphier <robla@robla.net> | 2010-03-19 16:40:49 -0700 |
commit | b00516272a14a42ed1486fbb98066995852c189c (patch) | |
tree | 2d2d225d3ae8317e657e3198cbd0611507f25590 /urwid/widget.py | |
parent | cd3d63c86098487773c8f2542f3877b6ebe59755 (diff) | |
download | urwid-b00516272a14a42ed1486fbb98066995852c189c.tar.gz |
New Edit.insert_test_result() method (nice for full-field validation)
Diffstat (limited to 'urwid/widget.py')
-rw-r--r-- | urwid/widget.py | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/urwid/widget.py b/urwid/widget.py index cbdeb8b..89b5b18 100644 --- a/urwid/widget.py +++ b/urwid/widget.py @@ -952,6 +952,28 @@ class Edit(Text): self.set_edit_text(self._edit_text[:p] + text + self._edit_text[p:]) self.set_edit_pos(self.edit_pos + len(text)) + + def insert_text_result(self, ch): + """ + Return result of insert_text(ch) without actually performing the + insertion. Handy for pre-validation. + """ + + # if there's highlighted text, it'll get replaced by this character + if self.highlight: + start, stop = self.highlight + btext, etext = self.edit_text[:start], self.edit_text[stop:] + result_text = btext + etext + result_pos = start + else: + result_text = self.edit_text + result_pos = self.edit_pos + + if type(ch) == type(u""): + ch = ch.encode("utf-8") + + result_text = result_text[:result_pos] + ch + result_text[result_pos:] + return (result_text, result_pos) def keypress(self, size, key): """ |