diff options
author | mathieui <mathieui@mathieui.net> | 2016-08-19 01:00:06 +0200 |
---|---|---|
committer | mathieui <mathieui@mathieui.net> | 2016-08-19 01:00:06 +0200 |
commit | 82d00d495b816c237cbefac3ff2387907a7f8843 (patch) | |
tree | a9eb2b831373b765165959b003be3fc43aa8a1db /poezio/tabs | |
parent | c94df86b2cb0230cda2281f1d955b8796b2b29bb (diff) | |
download | poezio-82d00d495b816c237cbefac3ff2387907a7f8843.tar.gz poezio-82d00d495b816c237cbefac3ff2387907a7f8843.tar.bz2 poezio-82d00d495b816c237cbefac3ff2387907a7f8843.tar.xz poezio-82d00d495b816c237cbefac3ff2387907a7f8843.zip |
Add a "confirm" tab type
This tab will be used when an external events prompts a boolean choice
to a user, like a new ssl cert, or a XEP (e.g. 0070) that needs an
answer, and for which a command-line interface with an info message
would be a pain.
Diffstat (limited to 'poezio/tabs')
-rw-r--r-- | poezio/tabs/__init__.py | 1 | ||||
-rw-r--r-- | poezio/tabs/confirmtab.py | 105 |
2 files changed, 106 insertions, 0 deletions
diff --git a/poezio/tabs/__init__.py b/poezio/tabs/__init__.py index 4347e505..55a74457 100644 --- a/poezio/tabs/__init__.py +++ b/poezio/tabs/__init__.py @@ -3,6 +3,7 @@ from poezio.tabs.basetabs import STATE_PRIORITY from poezio.tabs.rostertab import RosterInfoTab from poezio.tabs.muctab import MucTab, NS_MUC_USER from poezio.tabs.privatetab import PrivateTab +from poezio.tabs.confirmtab import ConfirmTab from poezio.tabs.conversationtab import ConversationTab, StaticConversationTab,\ DynamicConversationTab from poezio.tabs.xmltab import XMLTab diff --git a/poezio/tabs/confirmtab.py b/poezio/tabs/confirmtab.py new file mode 100644 index 00000000..3d4bd2dc --- /dev/null +++ b/poezio/tabs/confirmtab.py @@ -0,0 +1,105 @@ +""" +A generic tab that displays a text and a boolean choice +""" + +import logging +log = logging.getLogger(__name__) + +import curses +import collections + +from poezio import windows +from poezio.decorators import refresh_wrapper + +from poezio.tabs import Tab + + +class ConfirmTab(Tab): + plugin_commands = {} + plugin_keys = {} + + def __init__(self, core, name, text, short_message, callback, critical=False): + """Parameters: + name: The name of the tab + text: the text shown in the tab + short_message: what will be displayed at the top and bottom of + the tab + callback: the function(bool) that will be called when the user + makes a choice + critical: if the message needs to be displayed in a flashy color + """ + Tab.__init__(self, core) + self.state = 'normal' + self.name = name + self.default_help_message = windows.HelpText("Choose with arrow keys and press enter") + self.input = self.default_help_message + self.infowin_top = windows.ConfirmStatusWin(short_message, critical) + self.infowin_bottom = windows.ConfirmStatusWin(short_message, critical) + self.dialog = windows.Dialog(text, critical) + self.key_func['^I'] = self.toggle_choice + self.key_func[' '] = self.toggle_choice + self.key_func['KEY_LEFT'] = self.toggle_choice + self.key_func['KEY_RIGHT'] = self.toggle_choice + self.key_func['^M'] = self.validate + self.resize() + self.update_keys() + self.update_commands() + self.completion_callback = callback + + def toggle_choice(self): + self.dialog.toggle_choice() + self.refresh() + self.core.doupdate() + + def validate(self): + self.completion_callback(self.dialog.accept) + self.close() + + def refresh(self): + if self.need_resize: + self.resize() + log.debug(' TAB Refresh: %s', self.__class__.__name__) + if self.size.tab_degrade_y: + display_info_win = False + else: + display_info_win = True + if display_info_win: + self.info_win.refresh() + self.refresh_tab_win() + self.infowin_top.refresh() + self.infowin_bottom.refresh() + self.dialog.refresh() + self.input.refresh() + + def resize(self): + if self.size.tab_degrade_y: + info_win_height = 0 + tab_win_height = 0 + else: + info_win_height = self.core.information_win_size + tab_win_height = Tab.tab_win_height() + + self.infowin_top.resize(1, self.width, 0, 0) + self.infowin_bottom.resize(1, self.width, self.height - 2 - info_win_height - tab_win_height, 0) + self.dialog.resize(self.height - 3 - info_win_height - tab_win_height, + self.width, 1, 0) + self.input.resize(1, self.width, self.height-1, 0) + + def close(self, arg=None): + self.core.close_tab(self) + + def on_input(self, key, raw): + res = self.input.do_command(key, raw=raw) + if res and not isinstance(self.input, windows.Input): + return True + elif res: + return False + if not raw and key in self.key_func: + return self.key_func[key]() + + def on_info_win_size_changed(self): + if self.core.information_win_size >= self.height-3: + return + self.dialog.resize(self.height-3-self.core.information_win_size - Tab.tab_win_height(), self.width, 1, 0) + self.infowin_bottom.resize(1, self.width, self.height-2-self.core.information_win_size - Tab.tab_win_height(), 0) + |