Make mode optionally in ConfigAPI.bind and .unbind

This commit is contained in:
Florian Bruhin 2017-09-19 13:14:41 +02:00
parent a23492fe27
commit 7d1549aaeb
3 changed files with 18 additions and 8 deletions

View File

@ -162,6 +162,8 @@ To bind a key:
config.bind(',v', 'spawn mpv {url}', mode='normal') config.bind(',v', 'spawn mpv {url}', mode='normal')
---- ----
For bindings in normal mode, you can also leave out the `, mode='normal'` part.
If the key is already bound, `force=True` needs to be given to rebind it: If the key is already bound, `force=True` needs to be given to rebind it:
[source,python] [source,python]

View File

@ -180,11 +180,11 @@ class ConfigAPI:
with self._handle_error('setting', name): with self._handle_error('setting', name):
self._config.set_obj(name, value) self._config.set_obj(name, value)
def bind(self, key, command, *, mode, force=False): def bind(self, key, command, *, mode='normal', force=False):
with self._handle_error('binding', key): with self._handle_error('binding', key):
self._keyconfig.bind(key, command, mode=mode, force=force) self._keyconfig.bind(key, command, mode=mode, force=force)
def unbind(self, key, *, mode): def unbind(self, key, *, mode='normal'):
with self._handle_error('unbinding', key): with self._handle_error('unbinding', key):
self._keyconfig.unbind(key, mode=mode) self._keyconfig.unbind(key, mode=mode)

View File

@ -191,16 +191,24 @@ class TestConfigPy:
configfiles.read_config_py(confpy.filename) configfiles.read_config_py(confpy.filename)
assert config.instance._values['colors.hints.bg'] == expected assert config.instance._values['colors.hints.bg'] == expected
def test_bind(self, confpy): @pytest.mark.parametrize('line, mode', [
confpy.write('config.bind(",a", "message-info foo", mode="normal")') ('config.bind(",a", "message-info foo")', 'normal'),
('config.bind(",a", "message-info foo", "prompt")', 'prompt'),
])
def test_bind(self, confpy, line, mode):
confpy.write(line)
configfiles.read_config_py(confpy.filename) configfiles.read_config_py(confpy.filename)
expected = {'normal': {',a': 'message-info foo'}} expected = {mode: {',a': 'message-info foo'}}
assert config.instance._values['bindings.commands'] == expected assert config.instance._values['bindings.commands'] == expected
def test_unbind(self, confpy): @pytest.mark.parametrize('line, key, mode', [
confpy.write('config.unbind("o", mode="normal")') ('config.unbind("o")', 'o', 'normal'),
('config.unbind("y", mode="prompt")', 'y', 'prompt'),
])
def test_unbind(self, confpy, line, key, mode):
confpy.write(line)
configfiles.read_config_py(confpy.filename) configfiles.read_config_py(confpy.filename)
expected = {'normal': {'o': None}} expected = {mode: {key: None}}
assert config.instance._values['bindings.commands'] == expected assert config.instance._values['bindings.commands'] == expected
def test_mutating(self, confpy): def test_mutating(self, confpy):