Primitivus: AdvancedEdit and Notification bar improved

- AdvancedEdit now support a completion callback
- Notification bar now show progressions
This commit is contained in:
Goffi 2010-08-16 21:06:03 +08:00
commit 7f17176e2b

View file

@ -58,6 +58,19 @@ class AdvancedEdit(urwid.Edit):
new behaviour: emit a 'click' signal when enter is pressed"""
signals = urwid.Edit.signals + ['click']
def setCompletionMethod(self, callback):
"""Define method called when completion is asked
@callback: method with 2 arguments:
- the text to complete
- if there was already a completion, a dict with
- 'completed':last completion
- 'completion_pos': cursor position where the completion starts
- 'position': last completion cursor position
this dict must be used (and can be filled) to find next completion)
and which return the full text completed"""
self.completion_cb = callback
self.completion_data = {}
def keypress(self, size, key):
#TODO: insert mode is not managed yet
if key == 'ctrl a':
@ -74,6 +87,25 @@ class AdvancedEdit(urwid.Edit):
self.set_edit_pos(pos)
elif key == 'enter':
self._emit('click')
elif key == 'shift tab':
try:
before = self.edit_text[:self.edit_pos]
if self.completion_data:
if (not self.completion_data['completed']
or self.completion_data['position'] != self.edit_pos
or not before.endswith(self.completion_data['completed'])):
self.completion_data.clear()
else:
before = before[:-len(self.completion_data['completed'])]
complet = self.completion_cb(before, self.completion_data)
self.completion_data['completed'] = complet[len(before):]
self.set_edit_text(complet+self.edit_text[self.edit_pos:])
self.set_edit_pos(len(complet))
self.completion_data['position'] = self.edit_pos
return
except AttributeError:
#No completion method defined
pass
return super(AdvancedEdit, self).keypress(size, key)
@ -372,9 +404,10 @@ class NotificationBar(urwid.WidgetWrap):
def __init__(self):
self.waitNotifs = urwid.Text('')
self.message = ClickableText('', default_attr='notifs', select_attr='notifs')
self.message = ClickableText('', default_attr='notifs')
urwid.connect_signal(self.message, 'click', lambda wid: self.showNext())
self.columns = urwid.Columns([('fixed',6,self.waitNotifs),self.message])
self.progress = ClickableText('', default_attr='notifs')
self.columns = urwid.Columns([('fixed',6,self.waitNotifs),self.message,('fixed',4,self.progress)])
urwid.WidgetWrap.__init__(self, urwid.AttrMap(self.columns,'notifs'))
self.notifs = []
@ -383,6 +416,14 @@ class NotificationBar(urwid.WidgetWrap):
self.waitNotifs.set_text("(%i)" % len(self.notifs) if self.notifs else '')
self._emit('change')
def setProgress(self,percentage):
"""Define the progression to show on the right side of the bar"""
if percentage == None:
self.progress.set_text('')
else:
self.progress.set_text('%02i%%' % percentage)
self._emit('change')
def addPopUp(self, pop_up_widget):
"""Add a popup to the waiting queue"""
self.notifs.append(('popup',pop_up_widget))
@ -431,7 +472,7 @@ class NotificationBar(urwid.WidgetWrap):
def canHide(self):
"""Return True if there is now important information to show"""
return self.isQueueEmpty() and not self.message.get_text()
return self.isQueueEmpty() and not self.message.get_text() and not self.progress.get_text()
class MenuBox(urwid.WidgetWrap):