From eeab4d41ba221e485bd06af70d4577b289447783 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 15 Nov 2015 19:16:56 +0100 Subject: [PATCH] Make it possible to pass a count via command name. This is only used for tests so far and not intended for general usage (and thus undocumented). --- qutebrowser/commands/runners.py | 22 +++++++++++++++++++--- tests/unit/commands/test_runners.py | 9 +++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/qutebrowser/commands/runners.py b/qutebrowser/commands/runners.py index f0f23fe5a..84f358682 100644 --- a/qutebrowser/commands/runners.py +++ b/qutebrowser/commands/runners.py @@ -29,7 +29,8 @@ from qutebrowser.utils import message, log, objreg, qtutils from qutebrowser.misc import split -ParseResult = collections.namedtuple('ParseResult', 'cmd, args, cmdline') +ParseResult = collections.namedtuple('ParseResult', ['cmd', 'args', 'cmdline', + 'count']) def replace_variables(win_id, arglist): @@ -128,9 +129,19 @@ class CommandRunner(QObject): keep: Whether to keep special chars and whitespace Return: - A (cmd, args, cmdline) ParseResult tuple. + A ParseResult tuple. """ cmdstr, sep, argstr = text.partition(' ') + if ':' in cmdstr: + count, cmdstr = cmdstr.split(':', maxsplit=1) + try: + count = int(count) + except ValueError as e: + # We just ignore invalid prefixes + count = None + else: + count = None + if not cmdstr and not fallback: raise cmdexc.NoSuchCommandError("No command given") if aliases: @@ -161,7 +172,7 @@ class CommandRunner(QObject): cmdline = [cmdstr, sep] else: cmdline = [cmdstr] + args[:] - return ParseResult(cmd=cmd, args=args, cmdline=cmdline) + return ParseResult(cmd=cmd, args=args, cmdline=cmdline, count=count) def _split_args(self, cmd, argstr, keep): """Split the arguments from an arg string. @@ -216,7 +227,12 @@ class CommandRunner(QObject): for result in self.parse_all(text): args = replace_variables(self._win_id, result.args) if count is not None: + if result.count is not None: + raise cmdexc.CommandMetaError("Got count via command and " + "prefix!") result.cmd.run(self._win_id, args, count=count) + elif result.count is not None: + result.cmd.run(self._win_id, args, count=result.count) else: result.cmd.run(self._win_id, args) diff --git a/tests/unit/commands/test_runners.py b/tests/unit/commands/test_runners.py index a03eab9d8..7202e3724 100644 --- a/tests/unit/commands/test_runners.py +++ b/tests/unit/commands/test_runners.py @@ -42,3 +42,12 @@ class TestCommandRunner: else: with pytest.raises(cmdexc.NoSuchCommandError): list(cr.parse_all(cmdline_test.cmd, aliases=False)) + + def test_parse_with_count(self): + """Test parsing of commands with a count.""" + cr = runners.CommandRunner(0) + result = cr.parse('20:scroll down', aliases=False) + assert result.cmd.name == 'scroll' + assert result.count == 20 + assert result.args == ['down'] + assert result.cmdline == ['scroll', 'down']