From 3b0fb84c477baf7fea66294c19f101c03f85b220 Mon Sep 17 00:00:00 2001 From: Ismail S Date: Thu, 7 Jul 2016 20:16:19 +0100 Subject: [PATCH 1/5] Allow adding bookmarks by url --- qutebrowser/browser/commands.py | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 460b6ffe8..8ac78da26 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1116,12 +1116,23 @@ class CommandDispatcher: self._open(url, tab, bg, window) @cmdutils.register(instance='command-dispatcher', scope='window') - def bookmark_add(self): - """Save the current page as a bookmark.""" + def bookmark_add(self, url=None, title=None): + """Save the current page as a bookmark, or a specific url.""" + if url and not title: + raise cmdexc.CommandError('Title must be provided if url has ' + 'been provided') bookmark_manager = objreg.get('bookmark-manager') - url = self._current_url() + if isinstance(url, str): + try: + url = urlutils.fuzzy_url(url) + except urlutils.InvalidUrlError as e: + raise cmdexc.CommandError(e) + elif not url: + url = self._current_url() + if not title: + title = self._current_title() try: - bookmark_manager.add(url, self._current_title()) + bookmark_manager.add(url, title) except urlmarks.Error as e: raise cmdexc.CommandError(str(e)) else: From babbd0771cb49260c7874eb12d3bcfddfd1129e2 Mon Sep 17 00:00:00 2001 From: Ismail S Date: Fri, 8 Jul 2016 15:33:13 +0100 Subject: [PATCH 2/5] Refactor code --- qutebrowser/browser/commands.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 8ac78da26..326a06e55 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1120,15 +1120,15 @@ class CommandDispatcher: """Save the current page as a bookmark, or a specific url.""" if url and not title: raise cmdexc.CommandError('Title must be provided if url has ' - 'been provided') + 'been provided') bookmark_manager = objreg.get('bookmark-manager') - if isinstance(url, str): + if url is None: + url = self._current_url() + else: try: url = urlutils.fuzzy_url(url) except urlutils.InvalidUrlError as e: raise cmdexc.CommandError(e) - elif not url: - url = self._current_url() if not title: title = self._current_title() try: From 5ad66c29e19e5d63df65bb22e02c182945d9790e Mon Sep 17 00:00:00 2001 From: Ismail S Date: Fri, 8 Jul 2016 15:38:58 +0100 Subject: [PATCH 3/5] Update docstring --- qutebrowser/browser/commands.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 326a06e55..9f5ee479d 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1117,7 +1117,16 @@ class CommandDispatcher: @cmdutils.register(instance='command-dispatcher', scope='window') def bookmark_add(self, url=None, title=None): - """Save the current page as a bookmark, or a specific url.""" + """Save the current page as a bookmark, or a specific url. + + If no url and title are provided, then save the current page as a + bookmark. + If a url and title have been provided, then save the given url as + a bookmark with the provided title. + + Args: + url: url to save as a bookmark. If None, use url of current page. + title: title of the new bookmark.""" if url and not title: raise cmdexc.CommandError('Title must be provided if url has ' 'been provided') From 394d9d1404e94ea91225d8155c4d292d5ffc2bfa Mon Sep 17 00:00:00 2001 From: Ismail S Date: Fri, 8 Jul 2016 15:54:39 +0100 Subject: [PATCH 4/5] Add more tests for :bookmark-add --- tests/end2end/features/urlmarks.feature | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/end2end/features/urlmarks.feature b/tests/end2end/features/urlmarks.feature index 217f4012c..8f0136e59 100644 --- a/tests/end2end/features/urlmarks.feature +++ b/tests/end2end/features/urlmarks.feature @@ -8,6 +8,20 @@ Feature: quickmarks and bookmarks Then the message "Bookmarked http://localhost:*/data/title.html!" should be shown And the bookmark file should contain "http://localhost:*/data/title.html Test title" + Scenario: Saving a bookmark with a provided url and title + When I run :bookmark-add http://example.com "some example title" + Then the message "Bookmarked http://example.com!" should be shown + And the bookmark file should contain "http://example.com some example title" + + Scenario: Saving a bookmark with a url but no title + When I run :bookmark-add http://example.com + Then the error "Title must be provided if url has been provided" should be shown + + Scenario: Saving a bookmark with an invalid url + When I set general -> auto-search to false + And I run :bookmark-add foo! "some example title" + Then the error "Invalid URL" should be shown + Scenario: Saving a duplicate bookmark Given I have a fresh instance When I open data/title.html From fd7342b05558e251e6e0c1dd2c4230ad5330cfe4 Mon Sep 17 00:00:00 2001 From: Ismail S Date: Fri, 8 Jul 2016 15:58:47 +0100 Subject: [PATCH 5/5] Move """ to separate line --- qutebrowser/browser/commands.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 9f5ee479d..dc2386753 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1126,7 +1126,8 @@ class CommandDispatcher: Args: url: url to save as a bookmark. If None, use url of current page. - title: title of the new bookmark.""" + title: title of the new bookmark. + """ if url and not title: raise cmdexc.CommandError('Title must be provided if url has ' 'been provided')