Move config toggling to :config-cycle

This removes :set option! and allows :config-cycle option instead.
This commit is contained in:
Florian Bruhin 2017-10-03 07:29:44 +02:00
parent 81993a70a2
commit f533e3b751
3 changed files with 61 additions and 57 deletions

View File

@ -792,7 +792,7 @@ Syntax: +:set [*--temp*] [*--print*] ['option'] ['value']+
Set an option. Set an option.
If the option name ends with '?', the value of the option is shown instead. If the option name ends with '!' and it is a boolean value, toggle it. If the option name ends with '?', the value of the option is shown instead.
==== positional arguments ==== positional arguments
* +'option'+: The name of the option. * +'option'+: The name of the option.

View File

@ -47,8 +47,6 @@ class ConfigCommands:
If the option name ends with '?', the value of the option is shown If the option name ends with '?', the value of the option is shown
instead. instead.
If the option name ends with '!' and it is a boolean value, toggle it.
Args: Args:
option: The name of the option. option: The name of the option.
value: The value to set. value: The value to set.
@ -66,15 +64,7 @@ class ConfigCommands:
return return
with self._handle_config_error(): with self._handle_config_error():
if option.endswith('!') and option != '!' and value is None: if value is None:
option = option[:-1]
opt = self._config.get_opt(option)
if not isinstance(opt.typ, configtypes.Bool):
raise cmdexc.CommandError(
"set: Can't toggle non-bool setting {}".format(option))
old_value = self._config.get_obj(option)
self._config.set_obj(option, not old_value, save_yaml=not temp)
elif value is None:
raise cmdexc.CommandError("set: The following arguments " raise cmdexc.CommandError("set: The following arguments "
"are required: value") "are required: value")
else: else:
@ -95,13 +85,20 @@ class ConfigCommands:
temp: Set value temporarily until qutebrowser is closed. temp: Set value temporarily until qutebrowser is closed.
print_: Print the value after setting. print_: Print the value after setting.
""" """
if len(values) < 2:
raise configexc.CommandError("Need at least two values")
with self._handle_config_error(): with self._handle_config_error():
opt = self._config.get_opt(option)
old_value = self._config.get_obj(option, mutable=False)
if not values and isinstance(opt.typ, configtypes.Bool):
values = ['true', 'false']
if len(values) < 2:
raise cmdexc.CommandError("Need at least two values for "
"non-boolean settings.")
# Use the next valid value from values, or the first if the current # Use the next valid value from values, or the first if the current
# value does not appear in the list # value does not appear in the list
old_value = self._config.get_obj(option, mutable=False) with self._handle_config_error():
opt = self._config.get_opt(option)
values = [opt.typ.from_str(val) for val in values] values = [opt.typ.from_str(val) for val in values]
try: try:
@ -110,6 +107,8 @@ class ConfigCommands:
value = values[idx] value = values[idx]
except ValueError: except ValueError:
value = values[0] value = values[0]
with self._handle_config_error():
self._config.set_obj(option, value, save_yaml=not temp) self._config.set_obj(option, value, save_yaml=not temp)
if print_: if print_:

View File

@ -113,36 +113,6 @@ class TestSet:
msg = message_mock.getmsg(usertypes.MessageLevel.info) msg = message_mock.getmsg(usertypes.MessageLevel.info)
assert msg.text == 'url.auto_search = dns' assert msg.text == 'url.auto_search = dns'
def test_set_toggle(self, commands, config_stub):
"""Run ':set auto_save.session!'.
Should toggle the value.
"""
assert not config_stub.val.auto_save.session
commands.set(0, 'auto_save.session!')
assert config_stub.val.auto_save.session
assert config_stub._yaml['auto_save.session']
def test_set_toggle_nonbool(self, commands, config_stub):
"""Run ':set url.auto_search!'.
Should show an error
"""
assert config_stub.val.url.auto_search == 'naive'
with pytest.raises(cmdexc.CommandError, match="set: Can't toggle "
"non-bool setting url.auto_search"):
commands.set(0, 'url.auto_search!')
assert config_stub.val.url.auto_search == 'naive'
def test_set_toggle_print(self, commands, config_stub, message_mock):
"""Run ':set -p auto_save.session!'.
Should toggle the value and show the new value.
"""
commands.set(0, 'auto_save.session!', print_=True)
msg = message_mock.getmsg(usertypes.MessageLevel.info)
assert msg.text == 'auto_save.session = true'
def test_set_invalid_option(self, commands): def test_set_invalid_option(self, commands):
"""Run ':set foo bar'. """Run ':set foo bar'.
@ -180,14 +150,13 @@ class TestSet:
"value"): "value"):
commands.set(win_id=0, option=option) commands.set(win_id=0, option=option)
@pytest.mark.parametrize('suffix', '?!') def test_invalid(self, commands):
def test_invalid(self, commands, suffix): """Run ':set foo?'.
"""Run ':set foo?' / ':set foo!'.
Should show an error. Should show an error.
""" """
with pytest.raises(cmdexc.CommandError, match="set: No option 'foo'"): with pytest.raises(cmdexc.CommandError, match="set: No option 'foo'"):
commands.set(win_id=0, option='foo' + suffix) commands.set(win_id=0, option='foo?')
class TestCycle: class TestCycle:
@ -222,6 +191,42 @@ class TestCycle:
commands.config_cycle(opt, '[foo]', '[bar]') commands.config_cycle(opt, '[foo]', '[bar]')
assert config_stub.get(opt) == ['foo'] assert config_stub.get(opt) == ['foo']
def test_toggle(self, commands, config_stub):
"""Run ':config-cycle auto_save.session'.
Should toggle the value.
"""
assert not config_stub.val.auto_save.session
commands.config_cycle('auto_save.session')
assert config_stub.val.auto_save.session
assert config_stub._yaml['auto_save.session']
@pytest.mark.parametrize('args', [
['url.auto_search'], ['url.auto_search', 'foo']
])
def test_toggle_nonbool(self, commands, config_stub, args):
"""Run :config-cycle without a bool and 0/1 value.
:config-cycle url.auto_search
:config-cycle url.auto_search foo
Should show an error.
"""
assert config_stub.val.url.auto_search == 'naive'
with pytest.raises(cmdexc.CommandError, match="Need at least "
"two values for non-boolean settings."):
commands.config_cycle(*args)
assert config_stub.val.url.auto_search == 'naive'
def test_set_toggle_print(self, commands, config_stub, message_mock):
"""Run ':config-cycle -p auto_save.session'.
Should toggle the value and show the new value.
"""
commands.config_cycle('auto_save.session', print_=True)
msg = message_mock.getmsg(usertypes.MessageLevel.info)
assert msg.text == 'auto_save.session = true'
class TestBind: class TestBind: