diff --git a/qutebrowser/misc/keyhintwidget.py b/qutebrowser/misc/keyhintwidget.py index 9a4d99d95..270983b74 100644 --- a/qutebrowser/misc/keyhintwidget.py +++ b/qutebrowser/misc/keyhintwidget.py @@ -94,28 +94,32 @@ class KeyHintView(QLabel): self.hide() return + keyconf = objreg.get('key-config') + is_special = lambda k: k.startswith('<') and k.endswith('>') + bindings = [(key, cmd) for (key, cmd) + in keyconf.get_bindings_for(modename).items() + if key.startswith(prefix) and not is_special(key)] + + if not bindings: + return + self.show() suffix_color = html.escape(config.get('colors', 'keyhint.fg.suffix')) text = '' - keyconf = objreg.get('key-config') - # this is only fired in normal mode - for key, cmd in keyconf.get_bindings_for(modename).items(): - # for now, special keys can't be part of keychains, so ignore them - is_special_binding = key.startswith('<') and key.endswith('>') - if key.startswith(prefix) and not is_special_binding: - text += ( - "" - "{}" - "{}" - "{}" - "" - ).format( - html.escape(prefix), - suffix_color, - html.escape(key[len(prefix):]), - html.escape(cmd) - ) + for key, cmd in bindings: + text += ( + "" + "{}" + "{}" + "{}" + "" + ).format( + html.escape(prefix), + suffix_color, + html.escape(key[len(prefix):]), + html.escape(cmd) + ) text = '{}
'.format(text) self.setText(text) diff --git a/tests/unit/misc/test_keyhints.py b/tests/unit/misc/test_keyhints.py index 85f6c55ce..339e09df5 100644 --- a/tests/unit/misc/test_keyhints.py +++ b/tests/unit/misc/test_keyhints.py @@ -110,3 +110,11 @@ def test_color_switch(keyhint, config_stub, key_config_stub): ('aa', 'cmd-aa')])) keyhint.update_keyhint('normal', 'a') assert keyhint.text() == expected_text(('a', '#ABCDEF', 'a', 'cmd-aa')) + +def test_no_matches(keyhint, key_config_stub): + """Ensure the widget isn't visible if there are no keystrings to show.""" + key_config_stub.set_bindings_for('normal', OrderedDict([ + ('aa', 'cmd-aa'), + ('ab', 'cmd-ab')])) + keyhint.update_keyhint('normal', 'z') + assert not keyhint.isVisible()