From c7534bd4a3b2e65da9fa901794b01014e60c386a Mon Sep 17 00:00:00 2001 From: Panagiotis Ktistakis Date: Tue, 19 Apr 2016 12:52:47 +0300 Subject: [PATCH 1/4] Allow {url:pretty} variable in commands --- qutebrowser/commands/runners.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/qutebrowser/commands/runners.py b/qutebrowser/commands/runners.py index fbd7b1b22..bca05d80f 100644 --- a/qutebrowser/commands/runners.py +++ b/qutebrowser/commands/runners.py @@ -33,24 +33,33 @@ ParseResult = collections.namedtuple('ParseResult', ['cmd', 'args', 'cmdline', 'count']) +def _current_url(tabbed_browser): + """Convenience method to get the current url.""" + try: + return tabbed_browser.current_url() + except qtutils.QtValueError as e: + msg = "Current URL is invalid" + if e.reason: + msg += " ({})".format(e.reason) + msg += "!" + raise cmdexc.CommandError(msg) + + def replace_variables(win_id, arglist): """Utility function to replace variables like {url} in a list of args.""" args = [] tabbed_browser = objreg.get('tabbed-browser', scope='window', window=win_id) if '{url}' in arglist: - try: - url = tabbed_browser.current_url().toString(QUrl.FullyEncoded | - QUrl.RemovePassword) - except qtutils.QtValueError as e: - msg = "Current URL is invalid" - if e.reason: - msg += " ({})".format(e.reason) - msg += "!" - raise cmdexc.CommandError(msg) + url = _current_url(tabbed_browser).toString(QUrl.FullyEncoded | + QUrl.RemovePassword) + if '{url:pretty}' in arglist: + pretty_url = _current_url(tabbed_browser).toString(QUrl.RemovePassword) for arg in arglist: if arg == '{url}': args.append(url) + elif arg == '{url:pretty}': + args.append(pretty_url) else: args.append(arg) return args From 8550fb3401c8450ad353cef931d8a3723aeb422f Mon Sep 17 00:00:00 2001 From: Panagiotis Ktistakis Date: Wed, 20 Apr 2016 17:58:14 +0300 Subject: [PATCH 2/4] Add tests for {url:pretty} variable --- tests/integration/data/title with spaces.html | 10 ++++++++++ tests/integration/features/conftest.py | 4 +++- tests/integration/features/spawn.feature | 13 ++++++++++++- 3 files changed, 25 insertions(+), 2 deletions(-) create mode 100644 tests/integration/data/title with spaces.html diff --git a/tests/integration/data/title with spaces.html b/tests/integration/data/title with spaces.html new file mode 100644 index 000000000..d5e2ab9da --- /dev/null +++ b/tests/integration/data/title with spaces.html @@ -0,0 +1,10 @@ + + + + + Test title + + + foo + + diff --git a/tests/integration/features/conftest.py b/tests/integration/features/conftest.py index 2ff052937..414f882d7 100644 --- a/tests/integration/features/conftest.py +++ b/tests/integration/features/conftest.py @@ -275,10 +275,12 @@ def expect_message(quteproc, httpbin, category, message): @bdd.then(bdd.parsers.re(r'(?Pregex )?"(?P[^"]+)" should ' r'be logged')) -def should_be_logged(quteproc, is_regex, pattern): +def should_be_logged(quteproc, httpbin, is_regex, pattern): """Expect the given pattern on regex in the log.""" if is_regex: pattern = re.compile(pattern) + else: + pattern = pattern.replace('(port)', str(httpbin.port)) line = quteproc.wait_for(message=pattern) line.expected = True diff --git a/tests/integration/features/spawn.feature b/tests/integration/features/spawn.feature index 03506c18b..861091f91 100644 --- a/tests/integration/features/spawn.feature +++ b/tests/integration/features/spawn.feature @@ -16,9 +16,20 @@ Feature: :spawn When I run :spawn echo {url} Then "Executing echo with args ['about:blank'], userscript=False" should be logged + Scenario: Running :spawn with url variable in fully encoded format + When I open data/title with spaces.html + And I run :spawn echo {url} + Then "Executing echo with args ['http://localhost:(port)/data/title%20with%20spaces.html'], userscript=False" should be logged + + Scenario: Running :spawn with url variable in pretty decoded format + When I open data/title with spaces.html + And I run :spawn echo {url:pretty} + Then "Executing echo with args ['http://localhost:(port)/data/title with spaces.html'], userscript=False" should be logged + @posix Scenario: Running :spawn with userscript - When I execute the userscript open_current_url + When I open about:blank + And I execute the userscript open_current_url And I wait until about:blank is loaded Then the following tabs should be open: - about:blank From 1ef2f042cf0e78371fa7ae5d3034355b3bac8a21 Mon Sep 17 00:00:00 2001 From: Panagiotis Ktistakis Date: Wed, 20 Apr 2016 19:39:18 +0300 Subject: [PATCH 3/4] Mention {url:pretty} in the docs --- qutebrowser/browser/commands.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 278380578..e30d9884e 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -974,8 +974,10 @@ class CommandDispatcher: def spawn(self, cmdline, userscript=False, verbose=False, detach=False): """Spawn a command in a shell. - Note the {url} variable which gets replaced by the current URL might be - useful here. + Note the `{url}` and `{url:pretty}` variables might be useful here. + `{url}` gets replaced by the URL in fully encoded format and + `{url:pretty}` uses a "pretty form" with most percent-encoded + characters decoded. Args: userscript: Run the command as a userscript. You can use an From c27d4f440714976d34d750d6a36ab59fa7848ce7 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 20 Apr 2016 20:57:49 +0200 Subject: [PATCH 4/4] Update docs --- CHANGELOG.asciidoc | 1 + README.asciidoc | 2 +- doc/help/commands.asciidoc | 2 +- 3 files changed, 3 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 5ff06602c..0a725709b 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -22,6 +22,7 @@ Added - New `:edit-url` command to edit the URL in an external editor. - New `network -> custom-headers` setting to send custom headers with every request. +- New `{url:pretty}` commandline replacement which gets replaced by the decoded URL. Changed ~~~~~~~ diff --git a/README.asciidoc b/README.asciidoc index 8531a92f5..aca9172d3 100644 --- a/README.asciidoc +++ b/README.asciidoc @@ -173,10 +173,10 @@ Contributors, sorted by the number of commits in descending order: * John ShaggyTwoDope Jenkins * Jimmy * Peter Vilim +* Panagiotis Ktistakis * Clayton Craft * Oliver Caldwell * Jonas Schürmann -* Panagiotis Ktistakis * Jakub Klinkovský * skinnay * error800 diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 5d4786bbe..bf0891904 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -655,7 +655,7 @@ Syntax: +:spawn [*--userscript*] [*--verbose*] [*--detach*] 'cmdline'+ Spawn a command in a shell. -Note the {url} variable which gets replaced by the current URL might be useful here. +Note the `{url}` and `{url:pretty}` variables might be useful here. `{url}` gets replaced by the URL in fully encoded format and `{url:pretty}` uses a "pretty form" with most percent-encoded characters decoded. ==== positional arguments * +'cmdline'+: The commandline to execute.