From cecb79cf050dc77c6663a570aca6c90eb74ac3a8 Mon Sep 17 00:00:00 2001 From: Philip Lewis Date: Sun, 25 Mar 2018 15:18:02 -0400 Subject: [PATCH] Fix keyhints for special characters `prefix` is a string and `seq` is a key sequence, so removing `len(prefix)` items from `seq` will remove too many if `prefix` contains a special character (ex ""). Remove the number of characters from `str(seq)` instead. --- qutebrowser/misc/keyhintwidget.py | 2 +- tests/unit/misc/test_keyhints.py | 24 ++++++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/qutebrowser/misc/keyhintwidget.py b/qutebrowser/misc/keyhintwidget.py index 11446aa40..e1de9a6cc 100644 --- a/qutebrowser/misc/keyhintwidget.py +++ b/qutebrowser/misc/keyhintwidget.py @@ -130,7 +130,7 @@ class KeyHintView(QLabel): ).format( html.escape(prefix), suffix_color, - html.escape(str(seq[len(prefix):])), + html.escape(str(seq)[len(prefix):]), html.escape(cmd) ) text = '{}
'.format(text) diff --git a/tests/unit/misc/test_keyhints.py b/tests/unit/misc/test_keyhints.py index c45913a2a..9ac6e6221 100644 --- a/tests/unit/misc/test_keyhints.py +++ b/tests/unit/misc/test_keyhints.py @@ -92,6 +92,30 @@ def test_suggestions(keyhint, config_stub): ('a', 'yellow', 'c', 'message-info cmd-ac')) +def test_suggestions_special(keyhint, config_stub): + """Test that special characters work properly as prefix.""" + bindings = {'normal': { + 'a': 'message-info cmd-Cca', + '': 'message-info cmd-CcCc', + '': 'message-info cmd-CcCx', + 'cbb': 'message-info cmd-cbb', + 'xd': 'message-info cmd-xd', + 'xe': 'message-info cmd-xe', + }} + default_bindings = {'normal': { + 'c': 'message-info cmd-Ccc', + }} + config_stub.val.bindings.default = default_bindings + config_stub.val.bindings.commands = bindings + + keyhint.update_keyhint('normal', '') + assert keyhint.text() == expected_text( + ('<Ctrl+c>', 'yellow', 'a', 'message-info cmd-Cca'), + ('<Ctrl+c>', 'yellow', 'c', 'message-info cmd-Ccc'), + ('<Ctrl+c>', 'yellow', '<Ctrl+c>', 'message-info cmd-CcCc'), + ('<Ctrl+c>', 'yellow', '<Ctrl+x>', 'message-info cmd-CcCx')) + + def test_suggestions_with_count(keyhint, config_stub, monkeypatch, stubs): """Test that a count prefix filters out commands that take no count.""" monkeypatch.setattr('qutebrowser.commands.cmdutils.cmd_dict', {