From 359eefe1ab5f3d10a12fb19980459cc4aa42ef08 Mon Sep 17 00:00:00 2001 From: wishfort36 <42300264+wishfort36@users.noreply.github.com> Date: Sun, 12 Aug 2018 21:21:01 +0200 Subject: [PATCH 1/5] Expose $QUTE_COUNT to userscripts --- doc/help/commands.asciidoc | 3 +++ qutebrowser/browser/commands.py | 10 +++++++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 6daff3bcc..04cf5f318 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -1200,6 +1200,9 @@ Spawn a command in a shell. * +*-o*+, +*--output*+: Whether the output should be shown in a new tab. * +*-d*+, +*--detach*+: Whether the command should be detached from qutebrowser. +==== count +Given to userscripts as $QUTE_COUNT. + ==== note * This command does not split arguments after the last argument and handles quotes literally. diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 6578e4bb7..5c0e41e77 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1173,8 +1173,9 @@ class CommandDispatcher: @cmdutils.register(instance='command-dispatcher', scope='window', maxsplit=0, no_replace_variables=True) + @cmdutils.argument('count', count=True) def spawn(self, cmdline, userscript=False, verbose=False, - output=False, detach=False): + output=False, detach=False, count=None): """Spawn a command in a shell. Args: @@ -1188,6 +1189,7 @@ class CommandDispatcher: output: Whether the output should be shown in a new tab. detach: Whether the command should be detached from qutebrowser. cmdline: The commandline to execute. + count: Given to userscripts as $QUTE_COUNT. """ cmdutils.check_exclusive((userscript, detach), 'ud') try: @@ -1211,7 +1213,7 @@ class CommandDispatcher: if userscript: def _selection_callback(s): try: - runner = self._run_userscript(s, cmd, args, verbose) + runner = self._run_userscript(s, cmd, args, verbose, count) runner.finished.connect(_on_proc_finished) except cmdexc.CommandError as e: message.error(str(e)) @@ -1238,17 +1240,19 @@ class CommandDispatcher: """Open main startpage in current tab.""" self.openurl(config.val.url.start_pages[0]) - def _run_userscript(self, selection, cmd, args, verbose): + def _run_userscript(self, selection, cmd, args, verbose, count): """Run a userscript given as argument. Args: cmd: The userscript to run. args: Arguments to pass to the userscript. verbose: Show notifications when the command started/exited. + count: Exposed to the userscript. """ env = { 'QUTE_MODE': 'command', 'QUTE_SELECTED_TEXT': selection, + 'QUTE_COUNT': str(count), # must be a string } idx = self._current_index() From a9725ddb4a351df205177a76dbc9de501ce4ad19 Mon Sep 17 00:00:00 2001 From: wishfort36 <42300264+wishfort36@users.noreply.github.com> Date: Sun, 12 Aug 2018 22:31:12 +0200 Subject: [PATCH 2/5] Add tests for $QUTE_COUNT --- tests/end2end/data/userscripts/hello_if_count | 11 +++++++++++ tests/end2end/features/spawn.feature | 11 +++++++++++ 2 files changed, 22 insertions(+) create mode 100755 tests/end2end/data/userscripts/hello_if_count diff --git a/tests/end2end/data/userscripts/hello_if_count b/tests/end2end/data/userscripts/hello_if_count new file mode 100755 index 000000000..7ba64a76e --- /dev/null +++ b/tests/end2end/data/userscripts/hello_if_count @@ -0,0 +1,11 @@ +#!/bin/bash + +if [ "$QUTE_COUNT" = "5" ]; then + + echo "message-info 'Count is five!'" >> "$QUTE_FIFO" + +elif [ "$QUTE_COUNT" = "None" ]; then + + echo "message-info 'No count!'" >> "$QUTE_FIFO" + +fi diff --git a/tests/end2end/features/spawn.feature b/tests/end2end/features/spawn.feature index 2a1ea0039..87ffb53e0 100644 --- a/tests/end2end/features/spawn.feature +++ b/tests/end2end/features/spawn.feature @@ -47,6 +47,17 @@ Feature: :spawn - data/hello.txt - data/hello.txt (active) + @posix + Scenario: Running :spawn with userscript and count + When I run :spawn -u (testdata)/userscripts/hello_if_count with count 5 + Then the message "Count is five!" should be shown + + @posix + Scenario: Running :spawn with userscript and no count + When I run :spawn -u (testdata)/userscripts/hello_if_count + Then the message "No count!" should be shown + + @windows Scenario: Running :spawn with userscript on Windows When I open data/hello.txt From b05738dd6ce7aee36df057f90ba9d792037eb45e Mon Sep 17 00:00:00 2001 From: wishfort36 <42300264+wishfort36@users.noreply.github.com> Date: Sun, 12 Aug 2018 22:39:32 +0200 Subject: [PATCH 3/5] Keep $QUTE_COUNT unset if a count is not given --- qutebrowser/browser/commands.py | 4 +++- tests/end2end/data/userscripts/hello_if_count | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 5c0e41e77..448741f9f 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1252,9 +1252,11 @@ class CommandDispatcher: env = { 'QUTE_MODE': 'command', 'QUTE_SELECTED_TEXT': selection, - 'QUTE_COUNT': str(count), # must be a string } + if count is not None: + env['QUTE_COUNT'] = str(count) # must be str + idx = self._current_index() if idx != -1: env['QUTE_TITLE'] = self._tabbed_browser.widget.page_title(idx) diff --git a/tests/end2end/data/userscripts/hello_if_count b/tests/end2end/data/userscripts/hello_if_count index 7ba64a76e..efc8bdd6e 100755 --- a/tests/end2end/data/userscripts/hello_if_count +++ b/tests/end2end/data/userscripts/hello_if_count @@ -4,7 +4,7 @@ if [ "$QUTE_COUNT" = "5" ]; then echo "message-info 'Count is five!'" >> "$QUTE_FIFO" -elif [ "$QUTE_COUNT" = "None" ]; then +elif [ -z "$QUTE_COUNT" ]; then echo "message-info 'No count!'" >> "$QUTE_FIFO" From 6d59c1b7a5a9098797df88745eaf01df73b9e206 Mon Sep 17 00:00:00 2001 From: wishfort36 <42300264+wishfort36@users.noreply.github.com> Date: Sun, 12 Aug 2018 22:44:51 +0200 Subject: [PATCH 4/5] Update doc/userscripts.asciidoc --- doc/userscripts.asciidoc | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/userscripts.asciidoc b/doc/userscripts.asciidoc index c2f35b026..ac0fe9bb3 100644 --- a/doc/userscripts.asciidoc +++ b/doc/userscripts.asciidoc @@ -45,6 +45,7 @@ In `command` mode: - `QUTE_URL`: The current URL. - `QUTE_TITLE`: The title of the current page. - `QUTE_SELECTED_TEXT`: The text currently selected on the page. +- `QUTE_COUNT`: The `count` from the spawn command running the userscript. In `hints` mode: From 0e3e41a5e84b28764db393c3a202fd2109242e3b Mon Sep 17 00:00:00 2001 From: wishfort36 <42300264+wishfort36@users.noreply.github.com> Date: Mon, 13 Aug 2018 07:50:54 +0200 Subject: [PATCH 5/5] Handle nitpicks --- qutebrowser/browser/commands.py | 2 +- tests/end2end/data/userscripts/hello_if_count | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 448741f9f..d5e1797ac 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1255,7 +1255,7 @@ class CommandDispatcher: } if count is not None: - env['QUTE_COUNT'] = str(count) # must be str + env['QUTE_COUNT'] = str(count) idx = self._current_index() if idx != -1: diff --git a/tests/end2end/data/userscripts/hello_if_count b/tests/end2end/data/userscripts/hello_if_count index efc8bdd6e..b9f07b86f 100755 --- a/tests/end2end/data/userscripts/hello_if_count +++ b/tests/end2end/data/userscripts/hello_if_count @@ -1,6 +1,6 @@ #!/bin/bash -if [ "$QUTE_COUNT" = "5" ]; then +if [ "$QUTE_COUNT" -eq 5 ]; then echo "message-info 'Count is five!'" >> "$QUTE_FIFO"