Add support for /set <option> toggle, which toggles the current value

fix #2184
This commit is contained in:
Florent Le Coz 2012-12-15 03:28:06 +01:00
parent 3c83157821
commit c7f4abaef6
4 changed files with 31 additions and 14 deletions

View file

@ -24,6 +24,8 @@ http://dev.louiz.org/projects/poezio/roadmap
- Add a /self command. - Add a /self command.
- Add a /runkey command. - Add a /runkey command.
- Support underlined text in the input with C-c u. - Support underlined text in the input with C-c u.
- Add support for /set <option> toggle, which toggles the current value, if
its a bool.
* Poezio 0.7.5.1 & 2 - Summer 2012 * Poezio 0.7.5.1 & 2 - Summer 2012
- Fix tracebacks caused by a change in Sleekxmpp - Fix tracebacks caused by a change in Sleekxmpp

View file

@ -276,14 +276,17 @@ These commands work in *any* tab.
*/bookmarks*:: Show the current bookmarks. */bookmarks*:: Show the current bookmarks.
*/set [plugin|][section] <option> <value>*:: Set the value to the option in your */set [plugin|][section] <option> <value>*:: Set the value to the option in
configuration file. You can, for example, change your default nickname by your configuration file. You can, for example, change your default nickname
doing "/set default_nick toto" or your resource with "/set resource blabla". by doing "/set default_nick toto" or your resource with "/set resource
Doing so will write in the main config file, and in the main section blabla". Doing so will write in the main config file, and in the main
([Poezio]). But you can also write to another section, with "/set bindings section ([Poezio]). But you can also write to another section, with "/set
M-i ^i", to a plugin configuration with "/set mpd_client| host main" (notice bindings M-i ^i", to a plugin configuration with "/set mpd_client| host
the *|*, it is mandatory to write in a plugin), or even to another section main" (notice the *|*, it is mandatory to write in a plugin), or even to
in a plugin configuration "/set plugin|other_section option value". another section in a plugin configuration "/set plugin|other_section option
value". *toggle* can be used as a special value for a boolean option. It
just set the option to true if its currently false, and to false if its
currently true.
*/move_tab <source> <destination>*:: Move tab <source> to <destination>. If */move_tab <source> <destination>*:: Move tab <source> to <destination>. If
the create_gaps option is true, then it will leave a gap at the <source> the create_gaps option is true, then it will leave a gap at the <source>

View file

@ -158,12 +158,25 @@ class Config(RawConfigParser):
set the value in the configuration then save it set the value in the configuration then save it
to the file to the file
""" """
# Special case for a 'toggle' value. We take the current value
# and set the opposite. Warning if the no current value exists
# or it is not a bool.
if value == "toggle":
current = self.get(option, "", section)
if current.lower() == "false":
value = "true"
elif current.lower() == "true":
value = "false"
else:
return "Could not toggle option: %s. Current value is %s." % (option, current or "empty")
if self.has_section(section): if self.has_section(section):
RawConfigParser.set(self, section, option, value) RawConfigParser.set(self, section, option, value)
else: else:
self.add_section(section) self.add_section(section)
RawConfigParser.set(self, section, option, value) RawConfigParser.set(self, section, option, value)
self.write_in_file(section, option, value) self.write_in_file(section, option, value)
return "%s=%s" % (option, value)
def set(self, option, value, section=DEFSECTION): def set(self, option, value, section=DEFSECTION):
""" """

View file

@ -165,7 +165,7 @@ class Core(object):
'status': (self.command_status, _('Usage: /status <availability> [status message]\nStatus: Sets your availability and (optionally) your status message. The <availability> argument is one of \"available, chat, away, afk, dnd, busy, xa\" and the optional [status message] argument will be your status message.'), self.completion_status), 'status': (self.command_status, _('Usage: /status <availability> [status message]\nStatus: Sets your availability and (optionally) your status message. The <availability> argument is one of \"available, chat, away, afk, dnd, busy, xa\" and the optional [status message] argument will be your status message.'), self.completion_status),
'bookmark_local': (self.command_bookmark_local, _("Usage: /bookmark_local [roomname][/nick]\nBookmark Local: Bookmark locally the specified room (you will then auto-join it on each poezio start). This commands uses almost the same syntaxe as /join. Type /help join for syntaxe examples. Note that when typing \"/bookmark\" on its own, the room will be bookmarked with the nickname you\'re currently using in this room (instead of default_nick)"), self.completion_bookmark_local), 'bookmark_local': (self.command_bookmark_local, _("Usage: /bookmark_local [roomname][/nick]\nBookmark Local: Bookmark locally the specified room (you will then auto-join it on each poezio start). This commands uses almost the same syntaxe as /join. Type /help join for syntaxe examples. Note that when typing \"/bookmark\" on its own, the room will be bookmarked with the nickname you\'re currently using in this room (instead of default_nick)"), self.completion_bookmark_local),
'bookmark': (self.command_bookmark, _("Usage: /bookmark [roomname][/nick] [autojoin] [password]\nBookmark: Bookmark online the specified room (you will then auto-join it on each poezio start if autojoin is specified and is 'true'). This commands uses almost the same syntaxe as /join. Type /help join for syntaxe examples. Note that when typing \"/bookmark\" on its own, the room will be bookmarked with the nickname you\'re currently using in this room (instead of default_nick)"), self.completion_bookmark), 'bookmark': (self.command_bookmark, _("Usage: /bookmark [roomname][/nick] [autojoin] [password]\nBookmark: Bookmark online the specified room (you will then auto-join it on each poezio start if autojoin is specified and is 'true'). This commands uses almost the same syntaxe as /join. Type /help join for syntaxe examples. Note that when typing \"/bookmark\" on its own, the room will be bookmarked with the nickname you\'re currently using in this room (instead of default_nick)"), self.completion_bookmark),
'set': (self.command_set, _("Usage: /set [plugin|][section] <option> [value]\nSet: Set the value of an option in your configuration file. You can, for example, change your default nickname by doing `/set default_nick toto` or your resource with `/set resource blabla`. You can also set options in specific sections with `/set bindings M-i ^i` or in specific plugin with `/set mpd_client| host 127.0.0.1`"), self.completion_set), 'set': (self.command_set, _("Usage: /set [plugin|][section] <option> [value]\nSet: Set the value of an option in your configuration file. You can, for example, change your default nickname by doing `/set default_nick toto` or your resource with `/set resource blabla`. You can also set options in specific sections with `/set bindings M-i ^i` or in specific plugin with `/set mpd_client| host 127.0.0.1`. `toggle` can be used as a special value to toggle a boolean option."), self.completion_set),
'theme': (self.command_theme, _('Usage: /theme [theme_name]\nTheme: Reload the theme defined in the config file. If theme_name is provided, set that theme before reloading it.'), self.completion_theme), 'theme': (self.command_theme, _('Usage: /theme [theme_name]\nTheme: Reload the theme defined in the config file. If theme_name is provided, set that theme before reloading it.'), self.completion_theme),
'list': (self.command_list, _('Usage: /list\nList: Get the list of public chatrooms on the specified server.'), self.completion_list), 'list': (self.command_list, _('Usage: /list\nList: Get the list of public chatrooms on the specified server.'), self.completion_list),
'message': (self.command_message, _('Usage: /message <jid> [optional message]\nMessage: Open a conversation with the specified JID (even if it is not in our roster), and send a message to it, if the message is specified.'), self.completion_version), 'message': (self.command_message, _('Usage: /message <jid> [optional message]\nMessage: Open a conversation with the specified JID (even if it is not in our roster), and send a message to it, if the message is specified.'), self.completion_version),
@ -1977,7 +1977,7 @@ class Core(object):
if len(args) == 2: if len(args) == 2:
option = args[0] option = args[0]
value = args[1] value = args[1]
config.set_and_save(option, value) info = config.set_and_save(option, value)
elif len(args) == 3: elif len(args) == 3:
if '|' in args[0]: if '|' in args[0]:
plugin_name, section = args[0].split('|')[:2] plugin_name, section = args[0].split('|')[:2]
@ -1988,13 +1988,12 @@ class Core(object):
if not plugin_name in self.plugin_manager.plugins: if not plugin_name in self.plugin_manager.plugins:
return return
plugin = self.plugin_manager.plugins[plugin_name] plugin = self.plugin_manager.plugins[plugin_name]
plugin.config.set_and_save(option, value, section) info = plugin.config.set_and_save(option, value, section)
else: else:
section = args[0] section = args[0]
option = args[1] option = args[1]
value = args[2] value = args[2]
config.set_and_save(option, value, section) info = config.set_and_save(option, value, section)
msg = "%s=%s" % (option, value)
# Remove all gaptabs if switching from gaps to nogaps # Remove all gaptabs if switching from gaps to nogaps
if option == 'create_gaps' and value.lower() == 'false': if option == 'create_gaps' and value.lower() == 'false':
self.tabs = list(filter(lambda x: bool(x), self.tabs)) self.tabs = list(filter(lambda x: bool(x), self.tabs))
@ -2002,7 +2001,7 @@ class Core(object):
path = os.path.expanduser(value) path = os.path.expanduser(value)
self.plugin_manager.on_plugins_dir_change(path) self.plugin_manager.on_plugins_dir_change(path)
self.call_for_resize() self.call_for_resize()
self.information(msg, 'Info') self.information(info, "Info")
def completion_set(self, the_input): def completion_set(self, the_input):
"""Completion for /set""" """Completion for /set"""