Mercurial > urwid-satext
comparison urwid_satext/sat_widgets.py @ 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 | a99951c9ce2a |
comparison
equal
deleted
inserted
replaced
56:22ab98a06492 | 57:d493f95724a7 |
---|---|
111 except AttributeError: | 111 except AttributeError: |
112 #No completion method defined | 112 #No completion method defined |
113 pass | 113 pass |
114 return super(AdvancedEdit, self).keypress(size, key) | 114 return super(AdvancedEdit, self).keypress(size, key) |
115 | 115 |
116 class ModalEdit(AdvancedEdit): | |
117 """AdvancedEdit with vi-like mode management | |
118 - there is a new 'mode' property wich can be changed with properties | |
119 specified during init | |
120 - completion callback received a new 'mode' argument | |
121 """ | |
122 | |
123 def __init__(self, modes, *args, **kwargs): | |
124 """ first argument is "modes", others are the same paramaters as AdvancedEdit | |
125 @param modes: dictionnary in the form: | |
126 'key_to_change_mode': ('Mode', caption) | |
127 e.g.: 'i': ('INSERTION', '> ') | |
128 There *MUST* be a None key (for NORMAL mode)""" | |
129 assert(isinstance(modes, dict) and None in modes) | |
130 self._modes = modes | |
131 super(ModalEdit, self).__init__(*args, **kwargs) | |
132 self.mode = self._modes[None][0] | |
133 | |
134 @property | |
135 def mode(self): | |
136 return self._mode | |
137 | |
138 @mode.setter | |
139 def mode(self, value): | |
140 mode_key = None | |
141 for key in self._modes: | |
142 if self._modes[key][0] == value: | |
143 mode_key = key | |
144 break | |
145 | |
146 mode, caption = self._modes[mode_key] | |
147 self._mode = mode | |
148 self.set_caption(caption) | |
149 if not mode_key: #we are in NORMAL mode | |
150 self.set_edit_text('') | |
151 | |
152 def setCompletionMethod(self, callback): | |
153 """ Same as AdvancedEdit.setCompletionMethod, but with a third argument: current mode""" | |
154 super(ModalEdit, self).setCompletionMethod(lambda text,data: callback(text, data, self._mode)) | |
155 | |
156 def keypress(self, size, key): | |
157 if key == 'esc': | |
158 self.mode = "NORMAL" | |
159 return | |
160 if self._mode == 'NORMAL' and key in self._modes: | |
161 self.mode = self._modes[key][0] | |
162 return | |
163 return super(ModalEdit, self).keypress(size, key) | |
116 | 164 |
117 class SurroundedText(urwid.FlowWidget): | 165 class SurroundedText(urwid.FlowWidget): |
118 """Text centered on a repeated character (like a Divider, but with a text in the center)""" | 166 """Text centered on a repeated character (like a Divider, but with a text in the center)""" |
119 | 167 |
120 def __init__(self,text,car=utf8decode('─')): | 168 def __init__(self,text,car=utf8decode('─')): |