set_bindings can now update or replace existing bindings
This commit is contained in:
@@ -642,15 +642,17 @@ class KeyMap(object):
|
|||||||
self._keymap = None
|
self._keymap = None
|
||||||
self.set_bindings(bindings)
|
self.set_bindings(bindings)
|
||||||
|
|
||||||
def set_bindings(self, bindings):
|
def set_bindings(self, bindings, mode='update'):
|
||||||
# Clear the keymap before applying the bindings to avoid confusion.
|
new_keymap = {}
|
||||||
# If a user defines custom bindings in their config file, they must
|
|
||||||
# explicitly define ALL of the bindings.
|
|
||||||
self._keymap = {}
|
|
||||||
for command, keys in bindings.items():
|
for command, keys in bindings.items():
|
||||||
if not isinstance(command, Command):
|
if not isinstance(command, Command):
|
||||||
command = Command(command)
|
command = Command(command)
|
||||||
self._keymap[command] = keys
|
new_keymap[command] = keys
|
||||||
|
|
||||||
|
if not self._keymap or mode == 'replace':
|
||||||
|
self._keymap = new_keymap
|
||||||
|
elif mode == 'update':
|
||||||
|
self._keymap.update(new_keymap)
|
||||||
|
|
||||||
def get(self, command):
|
def get(self, command):
|
||||||
if not isinstance(command, Command):
|
if not isinstance(command, Command):
|
||||||
@@ -694,4 +696,4 @@ class KeyMap(object):
|
|||||||
|
|
||||||
except (AttributeError, ValueError, TypeError):
|
except (AttributeError, ValueError, TypeError):
|
||||||
raise exceptions.ConfigError('Invalid configuration! "%s" is not a '
|
raise exceptions.ConfigError('Invalid configuration! "%s" is not a '
|
||||||
'valid key' % key)
|
'valid key' % key)
|
||||||
|
|||||||
@@ -136,7 +136,7 @@ def test_config_from_file():
|
|||||||
|
|
||||||
fargs, fbindings = Config.get_file(filename=fp.name)
|
fargs, fbindings = Config.get_file(filename=fp.name)
|
||||||
config = Config(**fargs)
|
config = Config(**fargs)
|
||||||
config.keymap.set_bindings(fbindings)
|
config.keymap.set_bindings(fbindings, 'replace')
|
||||||
assert config.config == {}
|
assert config.config == {}
|
||||||
assert config.keymap._keymap == {}
|
assert config.keymap._keymap == {}
|
||||||
|
|
||||||
@@ -153,7 +153,7 @@ def test_config_from_file():
|
|||||||
fp.flush()
|
fp.flush()
|
||||||
fargs, fbindings = Config.get_file(filename=fp.name)
|
fargs, fbindings = Config.get_file(filename=fp.name)
|
||||||
config.update(**fargs)
|
config.update(**fargs)
|
||||||
config.keymap.set_bindings(fbindings)
|
config.keymap.set_bindings(fbindings, 'replace')
|
||||||
assert config.config == args
|
assert config.config == args
|
||||||
assert config.keymap.get('REFRESH') == ['r', '<KEY_F5>']
|
assert config.keymap.get('REFRESH') == ['r', '<KEY_F5>']
|
||||||
assert config.keymap.get('UPVOTE') == ['']
|
assert config.keymap.get('UPVOTE') == ['']
|
||||||
@@ -224,4 +224,4 @@ def test_config_history():
|
|||||||
|
|
||||||
config.delete_history()
|
config.delete_history()
|
||||||
assert len(config.history) == 0
|
assert len(config.history) == 0
|
||||||
assert not os.path.exists(fp.name)
|
assert not os.path.exists(fp.name)
|
||||||
|
|||||||
@@ -370,13 +370,16 @@ def test_objects_keymap():
|
|||||||
keymap.get('downvote')
|
keymap.get('downvote')
|
||||||
assert 'DOWNVOTE' in six.text_type(e)
|
assert 'DOWNVOTE' in six.text_type(e)
|
||||||
|
|
||||||
# Updating the bindings wipes out the old ones
|
|
||||||
bindings = {'refresh': ['a', 0x12, '<LF>', '<KEY_UP>']}
|
bindings = {'refresh': ['a', 0x12, '<LF>', '<KEY_UP>']}
|
||||||
keymap.set_bindings(bindings)
|
bindings2 = {'upvote': ['b', 0x13, '<KEY_DOWN>']}
|
||||||
|
keymap.set_bindings(bindings, 'replace')
|
||||||
assert keymap.get('refresh')
|
assert keymap.get('refresh')
|
||||||
with pytest.raises(exceptions.ConfigError) as e:
|
with pytest.raises(exceptions.ConfigError) as e:
|
||||||
keymap.get('upvote')
|
keymap.get('upvote')
|
||||||
assert 'UPVOTE' in six.text_type(e)
|
assert 'UPVOTE' in six.text_type(e)
|
||||||
|
keymap.set_bindings(bindings2, 'update')
|
||||||
|
assert keymap.get('refresh')
|
||||||
|
assert keymap.get('upvote')
|
||||||
|
|
||||||
# Strings should be parsed correctly into keys
|
# Strings should be parsed correctly into keys
|
||||||
assert KeyMap.parse('a') == 97
|
assert KeyMap.parse('a') == 97
|
||||||
@@ -589,4 +592,4 @@ def test_objects_navigator_flip():
|
|||||||
nav.flip(3)
|
nav.flip(3)
|
||||||
assert nav.page_index == 2
|
assert nav.page_index == 2
|
||||||
assert nav.cursor_index == 3
|
assert nav.cursor_index == 3
|
||||||
assert not nav.inverted
|
assert not nav.inverted
|
||||||
|
|||||||
Reference in New Issue
Block a user