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): def sig_modified(self, sender):
self.bindings = list(self.master.keymap.list("all")) self.bindings = list(self.master.keymap.list("all"))
self._modified()
self.set_focus(min(self.index, len(self.bindings) - 1)) self.set_focus(min(self.index, len(self.bindings) - 1))
self._modified()
def get_edit_text(self): def get_edit_text(self):
return self.focus_obj.get_edit_text() return self.focus_obj.get_edit_text()

View File

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