make navigate take counts for inc, dec, and up

This commit is contained in:
Peter Rice 2016-08-15 21:09:15 -04:00
parent 5c766ff7da
commit d3e4d62d14
3 changed files with 14 additions and 12 deletions

View File

@ -482,7 +482,8 @@ class CommandDispatcher:
@cmdutils.register(instance='command-dispatcher', scope='window') @cmdutils.register(instance='command-dispatcher', scope='window')
@cmdutils.argument('where', choices=['prev', 'next', 'up', 'increment', @cmdutils.argument('where', choices=['prev', 'next', 'up', 'increment',
'decrement']) 'decrement'])
def navigate(self, where: str, tab=False, bg=False, window=False): @cmdutils.argument('count', count=True)
def navigate(self, where: str, tab=False, bg=False, window=False, count=1):
"""Open typical prev/next links or navigate using the URL path. """Open typical prev/next links or navigate using the URL path.
This tries to automatically click on typical _Previous Page_ or This tries to automatically click on typical _Previous Page_ or
@ -526,7 +527,7 @@ class CommandDispatcher:
handler(browsertab=widget, win_id=self._win_id, baseurl=url, handler(browsertab=widget, win_id=self._win_id, baseurl=url,
tab=tab, background=bg, window=window) tab=tab, background=bg, window=window)
elif where in ['up', 'increment', 'decrement']: elif where in ['up', 'increment', 'decrement']:
new_url = handlers[where](url) new_url = handlers[where](url, count)
self._open(new_url, tab, bg, window) self._open(new_url, tab, bg, window)
else: # pragma: no cover else: # pragma: no cover
raise ValueError("Got called with invalid value {} for " raise ValueError("Got called with invalid value {} for "

View File

@ -31,7 +31,7 @@ class Error(Exception):
"""Raised when the navigation can't be done.""" """Raised when the navigation can't be done."""
def incdec(url, inc_or_dec): def incdec(url, count, inc_or_dec):
"""Helper method for :navigate when `where' is increment/decrement. """Helper method for :navigate when `where' is increment/decrement.
Args: Args:
@ -43,13 +43,13 @@ def incdec(url, inc_or_dec):
""" """
segments = set(config.get('general', 'url-incdec-segments')) segments = set(config.get('general', 'url-incdec-segments'))
try: try:
new_url = urlutils.incdec_number(url, inc_or_dec, segments=segments) new_url = urlutils.incdec_number(url, inc_or_dec, count, segments=segments)
except urlutils.IncDecError as error: except urlutils.IncDecError as error:
raise Error(error.msg) raise Error(error.msg)
return new_url return new_url
def path_up(url): def path_up(url, count):
"""Helper method for :navigate when `where' is up. """Helper method for :navigate when `where' is up.
Args: Args:
@ -58,8 +58,9 @@ def path_up(url):
path = url.path() path = url.path()
if not path or path == '/': if not path or path == '/':
raise Error("Can't go up!") raise Error("Can't go up!")
new_path = posixpath.join(path, posixpath.pardir) for i in range(0, min(count, path.count('/'))):
url.setPath(new_path) path = posixpath.join(path, posixpath.pardir)
url.setPath(path)
return url return url

View File

@ -499,7 +499,7 @@ class IncDecError(Exception):
return '{}: {}'.format(self.msg, self.url.toString()) return '{}: {}'.format(self.msg, self.url.toString())
def _get_incdec_value(match, incdec, url): def _get_incdec_value(match, incdec, url, count):
"""Get an incremented/decremented URL based on a URL match.""" """Get an incremented/decremented URL based on a URL match."""
pre, zeroes, number, post = match.groups() pre, zeroes, number, post = match.groups()
# This should always succeed because we match \d+ # This should always succeed because we match \d+
@ -507,9 +507,9 @@ def _get_incdec_value(match, incdec, url):
if incdec == 'decrement': if incdec == 'decrement':
if val <= 0: if val <= 0:
raise IncDecError("Can't decrement {}!".format(val), url) raise IncDecError("Can't decrement {}!".format(val), url)
val -= 1 val -= count
elif incdec == 'increment': elif incdec == 'increment':
val += 1 val += count
else: else:
raise ValueError("Invalid value {} for indec!".format(incdec)) raise ValueError("Invalid value {} for indec!".format(incdec))
if zeroes: if zeroes:
@ -521,7 +521,7 @@ def _get_incdec_value(match, incdec, url):
return ''.join([pre, zeroes, str(val), post]) return ''.join([pre, zeroes, str(val), post])
def incdec_number(url, incdec, segments=None): def incdec_number(url, incdec, count=1, segments=None):
"""Find a number in the url and increment or decrement it. """Find a number in the url and increment or decrement it.
Args: Args:
@ -566,7 +566,7 @@ def incdec_number(url, incdec, segments=None):
if not match: if not match:
continue continue
setter(_get_incdec_value(match, incdec, url)) setter(_get_incdec_value(match, incdec, url, count))
return url return url
raise IncDecError("No number found in URL!", url) raise IncDecError("No number found in URL!", url)