Mercurial > urwid-satext
changeset 57:d493f95724a7
new ModalEdit for vi-like bahaviour
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 21 Oct 2012 16:36:59 +0200 |
parents | 22ab98a06492 |
children | 7155b81ffdd5 |
files | urwid_satext/sat_widgets.py |
diffstat | 1 files changed, 48 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/urwid_satext/sat_widgets.py Tue Oct 16 01:09:51 2012 +0200 +++ b/urwid_satext/sat_widgets.py Sun Oct 21 16:36:59 2012 +0200 @@ -113,6 +113,54 @@ pass return super(AdvancedEdit, self).keypress(size, key) +class ModalEdit(AdvancedEdit): + """AdvancedEdit with vi-like mode management + - there is a new 'mode' property wich can be changed with properties + specified during init + - completion callback received a new 'mode' argument + """ + + def __init__(self, modes, *args, **kwargs): + """ first argument is "modes", others are the same paramaters as AdvancedEdit + @param modes: dictionnary in the form: + 'key_to_change_mode': ('Mode', caption) + e.g.: 'i': ('INSERTION', '> ') + There *MUST* be a None key (for NORMAL mode)""" + assert(isinstance(modes, dict) and None in modes) + self._modes = modes + super(ModalEdit, self).__init__(*args, **kwargs) + self.mode = self._modes[None][0] + + @property + def mode(self): + return self._mode + + @mode.setter + def mode(self, value): + mode_key = None + for key in self._modes: + if self._modes[key][0] == value: + mode_key = key + break + + mode, caption = self._modes[mode_key] + self._mode = mode + self.set_caption(caption) + if not mode_key: #we are in NORMAL mode + self.set_edit_text('') + + def setCompletionMethod(self, callback): + """ Same as AdvancedEdit.setCompletionMethod, but with a third argument: current mode""" + super(ModalEdit, self).setCompletionMethod(lambda text,data: callback(text, data, self._mode)) + + def keypress(self, size, key): + if key == 'esc': + self.mode = "NORMAL" + return + if self._mode == 'NORMAL' and key in self._modes: + self.mode = self._modes[key][0] + return + return super(ModalEdit, self).keypress(size, key) class SurroundedText(urwid.FlowWidget): """Text centered on a repeated character (like a Divider, but with a text in the center)"""