Add --toggle flag to bookmark-add (fixes #1667)

This commit is contained in:
Marshall Lochbaum 2016-07-22 22:46:30 -04:00
parent 614794a62a
commit 28842c90b6
3 changed files with 25 additions and 5 deletions

View File

@ -1120,7 +1120,8 @@ class CommandDispatcher:
raise cmdexc.CommandError("Quickmark '{}' not found!".format(name))
@cmdutils.register(instance='command-dispatcher', scope='window')
def bookmark_add(self, url=None, title=None):
@cmdutils.argument('toggle', flag='t')
def bookmark_add(self, url=None, title=None, toggle=False):
"""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
@ -1134,6 +1135,8 @@ class CommandDispatcher:
Args:
url: url to save as a bookmark. If None, use url of current page.
title: title of the new bookmark.
toggle: remove the bookmark instead of raising an error if it
already exists.
"""
if url and not title:
raise cmdexc.CommandError('Title must be provided if url has '
@ -1149,12 +1152,13 @@ class CommandDispatcher:
if not title:
title = self._current_title()
try:
bookmark_manager.add(url, title)
if_added = bookmark_manager.add(url, title, toggle)
except urlmarks.Error as e:
raise cmdexc.CommandError(str(e))
else:
mes = "Bookmarked {}!" if if_added else "Removed bookmark {}!"
message.info(self._win_id,
"Bookmarked {}!".format(url.toDisplayString()))
mes.format(url.toDisplayString()))
@cmdutils.register(instance='command-dispatcher', scope='window',
maxsplit=0)

View File

@ -272,12 +272,17 @@ class BookmarkManager(UrlMarkManager):
elif len(parts) == 1:
self.marks[parts[0]] = ''
def add(self, url, title):
def add(self, url, title, toggle=False):
"""Add a new bookmark.
Return True if the bookmark was added, and False if it was
removed (which only happens if toggle is True).
Args:
url: The url to add as bookmark.
title: The title for the new bookmark.
toggle: remove the bookmark instead of raising an error if it
already exists.
"""
if not url.isValid():
errstr = urlutils.get_errstring(url)
@ -286,8 +291,13 @@ class BookmarkManager(UrlMarkManager):
urlstr = url.toString(QUrl.RemovePassword | QUrl.FullyEncoded)
if urlstr in self.marks:
raise AlreadyExistsError("Bookmark already exists!")
if toggle:
del self.marks[urlstr]
return False
else:
raise AlreadyExistsError("Bookmark already exists!")
else:
self.marks[urlstr] = title
self.changed.emit()
self.added.emit(title, urlstr)
return True

View File

@ -97,6 +97,12 @@ Feature: quickmarks and bookmarks
And I run :bookmark-del
Then the bookmark file should not contain "http://localhost:*/data/numbers/6.txt "
Scenario: Toggling a bookmark
When I open data/numbers/7.txt
And I run :bookmark-add
And I run :bookmark-add --toggle
Then the bookmark file should not contain "http://localhost:*/data/numbers/7.txt "
## quickmarks
Scenario: Saving a quickmark (:quickmark-add)