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.argument('where', choices=['prev', 'next', 'up', 'increment',
'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.
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,
tab=tab, background=bg, window=window)
elif where in ['up', 'increment', 'decrement']:
new_url = handlers[where](url)
new_url = handlers[where](url, count)
self._open(new_url, tab, bg, window)
else: # pragma: no cover
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."""
def incdec(url, inc_or_dec):
def incdec(url, count, inc_or_dec):
"""Helper method for :navigate when `where' is increment/decrement.
Args:
@ -43,13 +43,13 @@ def incdec(url, inc_or_dec):
"""
segments = set(config.get('general', 'url-incdec-segments'))
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:
raise Error(error.msg)
return new_url
def path_up(url):
def path_up(url, count):
"""Helper method for :navigate when `where' is up.
Args:
@ -58,8 +58,9 @@ def path_up(url):
path = url.path()
if not path or path == '/':
raise Error("Can't go up!")
new_path = posixpath.join(path, posixpath.pardir)
url.setPath(new_path)
for i in range(0, min(count, path.count('/'))):
path = posixpath.join(path, posixpath.pardir)
url.setPath(path)
return url

View File

@ -499,7 +499,7 @@ class IncDecError(Exception):
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."""
pre, zeroes, number, post = match.groups()
# This should always succeed because we match \d+
@ -507,9 +507,9 @@ def _get_incdec_value(match, incdec, url):
if incdec == 'decrement':
if val <= 0:
raise IncDecError("Can't decrement {}!".format(val), url)
val -= 1
val -= count
elif incdec == 'increment':
val += 1
val += count
else:
raise ValueError("Invalid value {} for indec!".format(incdec))
if zeroes:
@ -521,7 +521,7 @@ def _get_incdec_value(match, incdec, url):
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.
Args:
@ -566,7 +566,7 @@ def incdec_number(url, incdec, segments=None):
if not match:
continue
setter(_get_incdec_value(match, incdec, url))
setter(_get_incdec_value(match, incdec, url, count))
return url
raise IncDecError("No number found in URL!", url)