console: keybindings tweaks

- consistent sort order
- preserve help on edit
This commit is contained in:
Aldo Cortesi 2017-06-14 10:01:07 +12:00
parent 2108bfb106
commit e8939b8b9f
2 changed files with 10 additions and 7 deletions

View File

@ -48,8 +48,8 @@ class KeyListWalker(urwid.ListWalker):
def sig_modified(self, sender):
self.bindings = list(self.master.keymap.list("all"))
self._modified()
self.set_focus(min(self.index, len(self.bindings) - 1))
self._modified()
def get_edit_text(self):
return self.focus_obj.get_edit_text()

View File

@ -19,7 +19,7 @@ Contexts = {
class Binding:
def __init__(self, key, command, contexts, help):
self.key, self.command, self.contexts = key, command, contexts
self.key, self.command, self.contexts = key, command, sorted(contexts)
self.help = help
def keyspec(self):
@ -29,6 +29,9 @@ class Binding:
"""
return self.key.replace("space", " ")
def sortkey(self):
return self.key + ",".join(self.contexts)
class Keymap:
def __init__(self, master):
@ -56,16 +59,16 @@ class Keymap:
Add a key to the key map.
"""
self._check_contexts(contexts)
self.remove(key, contexts)
for b in self.bindings:
if b.key == key and b.command == command:
b.contexts = list(set(b.contexts + contexts))
if b.key == key and b.command.strip() == command.strip():
b.contexts = sorted(list(set(b.contexts + contexts)))
if help:
b.help = help
self.bind(b)
break
else:
self.remove(key, contexts)
b = Binding(key=key, command=command, contexts=contexts, help=help)
self.bindings.append(b)
self.bind(b)
@ -107,8 +110,8 @@ class Keymap:
b = [x for x in self.bindings if context in x.contexts or context == "all"]
single = [x for x in b if len(x.key.split()) == 1]
multi = [x for x in b if len(x.key.split()) != 1]
single.sort(key=lambda x: x.key)
multi.sort(key=lambda x: x.key)
single.sort(key=lambda x: x.sortkey())
multi.sort(key=lambda x: x.sortkey())
return single + multi
def handle(self, context: str, key: str) -> typing.Optional[str]: