From 4e1905c23883e123a405338811d5340c608cb87c Mon Sep 17 00:00:00 2001 From: Goffi Date: Sun, 21 Oct 2012 16:40:19 +0200 Subject: [PATCH] primitivus: Primitivus is now modal (vi-like behaviour): - there are currently 3 modes: NORMAL, INSERTION and COMMAND - when a chat window is selected, INSERTION mode is set if possible - completion is managed according to mode. Currently only INSERTION mode do something - first command implementation: :quit --- frontends/src/primitivus/contact_list.py | 1 + frontends/src/primitivus/primitivus | 60 ++++++++++++++++++----- frontends/src/quick_frontend/quick_app.py | 2 +- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/frontends/src/primitivus/contact_list.py b/frontends/src/primitivus/contact_list.py index c2010774..d33a1e29 100644 --- a/frontends/src/primitivus/contact_list.py +++ b/frontends/src/primitivus/contact_list.py @@ -107,6 +107,7 @@ class ContactList(urwid.WidgetWrap, QuickContactList): widget.setState(widget.data == self.selected, invisible=True) if self.selected in self.alert_jid: self.alert_jid.remove(self.selected) + self.host.modeHint('INSERTION') self.update() self._emit('click') diff --git a/frontends/src/primitivus/primitivus b/frontends/src/primitivus/primitivus index da884a0b..f4f2878a 100755 --- a/frontends/src/primitivus/primitivus +++ b/frontends/src/primitivus/primitivus @@ -66,6 +66,19 @@ class PrimitivusApp(QuickApp): self.__saved_overlay = None self.x_notify = Notify() + + @property + def mode(self): + return self.editBar.mode + + @mode.setter + def mode(self, value): + self.editBar.mode = value + + def modeHint(self, value): + """Change mode if make sens (i.e.: if there is nothing in the editBar""" + if not self.editBar.get_edit_text(): + self.mode = value def debug(self): """convenient method to reset screen and launch p(u)db""" @@ -160,6 +173,14 @@ class PrimitivusApp(QuickApp): except AttributeError: return input + def commandHandler(self, editBar): + #TODO: separate class with auto documentation (with introspection) + # and completion method + command = editBar.get_edit_text() + if command == 'quit': + self.onExit() + raise urwid.ExitMainLoop() + def __buildMenuRoller(self): menu = sat_widgets.Menu(self.loop) general = _("General") @@ -193,13 +214,23 @@ class PrimitivusApp(QuickApp): self.contact_list = ContactList(self, on_click = self.contactSelected, on_change=lambda w: self.redraw()) #self.center_part = urwid.Columns([('weight',2,self.contact_list),('weight',8,Chat('',self))]) self.center_part = urwid.Columns([('weight',2,self.contact_list), ('weight',8,urwid.Filler(urwid.Text('')))]) - self.editBar = sat_widgets.AdvancedEdit(u'> ') - self.editBar.setCompletionMethod(self._nick_completion) + + modes = {None: ('NORMAL', ''), + 'i': ('INSERTION','> '), + ':': ('COMMAND',':')} + self.editBar = sat_widgets.ModalEdit(modes) + self.editBar.setCompletionMethod(self._text_completion) urwid.connect_signal(self.editBar,'click',self.onTextEntered) self.menu_roller = self.__buildMenuRoller() self.main_widget = sat_widgets.FocusFrame(self.center_part, header=self.menu_roller, footer=self.editBar, focus_part='footer') return self.main_widget + def _text_completion(self, text, completion_data, mode): + if mode == 'INSERTION': + return self._nick_completion(text, completion_data) + else: + return text + def _nick_completion(self, text, completion_data): """Completion method which complete pseudo in group chat for params, see AdvancedEdit""" @@ -291,17 +322,20 @@ class PrimitivusApp(QuickApp): def onTextEntered(self, editBar): """Called when text is entered in the main edit bar""" - contact = self.contact_list.getContact() ###Based on the fact that there is currently only one contact selectableat once - if contact: - chat = self.chat_wins[contact] - try: - self.sendMessage(contact, - editBar.get_edit_text(), - mess_type = "groupchat" if chat.type == 'group' else "chat", - profile_key=self.profile) - except: - self.notify(_("Error while sending message")) - editBar.set_edit_text('') + if self.mode == 'INSERTION': + contact = self.contact_list.getContact() ###Based on the fact that there is currently only one contact selectableat once + if contact: + chat = self.chat_wins[contact] + try: + self.sendMessage(contact, + editBar.get_edit_text(), + mess_type = "groupchat" if chat.type == 'group' else "chat", + profile_key=self.profile) + except: + self.notify(_("Error while sending message")) + editBar.set_edit_text('') + elif self.mode == 'COMMAND': + self.commandHandler(editBar) def newMessage(self, from_jid, to_jid, msg, _type, extra, profile): QuickApp.newMessage(self, from_jid, to_jid, msg, _type, extra, profile) diff --git a/frontends/src/quick_frontend/quick_app.py b/frontends/src/quick_frontend/quick_app.py index 7c55860d..0f669b45 100644 --- a/frontends/src/quick_frontend/quick_app.py +++ b/frontends/src/quick_frontend/quick_app.py @@ -30,7 +30,7 @@ import sat_frontends.quick_frontend.constants import gettext gettext.install('sat_frontend', "../i18n", unicode=True) -class QuickApp(): +class QuickApp(object): """This class contain the main methods needed for the frontend""" def __init__(self, single_profile=True):