Add cmdutils.arg_or_count
This commit is contained in:
parent
63ce8e0966
commit
5a6208862e
1
TODO
1
TODO
@ -94,7 +94,6 @@ Style
|
||||
=====
|
||||
|
||||
- initialize completion models at some nicer place (not in widget)
|
||||
- add util function to check arg/count
|
||||
- move curcommand stuff to other places (e.g. current widget, etc.)
|
||||
maybe rename curcommand to commands or so?
|
||||
also some curcommand stuff is in tabbedbrowser, etc.
|
||||
|
@ -406,19 +406,10 @@ class CurCommandDispatcher(QObject):
|
||||
Args:
|
||||
count: How many steps to take.
|
||||
"""
|
||||
if zoom is not None and count is not None:
|
||||
message.error("Either argument or count must be given!")
|
||||
return
|
||||
if zoom is not None:
|
||||
try:
|
||||
level = int(zoom)
|
||||
except ValueError:
|
||||
message.error("Argument {} must be an integer!".format(zoom))
|
||||
return
|
||||
elif count is not None:
|
||||
level = count
|
||||
else:
|
||||
level = 100
|
||||
try:
|
||||
level = cmdutils.arg_or_count(zoom, count, default=100)
|
||||
except ValueError as e:
|
||||
message.error(e)
|
||||
tab = self._tabs.currentWidget()
|
||||
tab.zoom_perc(level)
|
||||
|
||||
|
@ -29,6 +29,44 @@ from qutebrowser.commands._command import Command
|
||||
cmd_dict = {}
|
||||
|
||||
|
||||
def arg_or_count(arg, count, default=None, countzero=None):
|
||||
|
||||
"""Get a value based on an argument and count given to a command.
|
||||
|
||||
If both arg and count are set, ValueError is raised.
|
||||
If only arg/count is set, it is used.
|
||||
If none is set, a default is returned or ValueError is raised.
|
||||
|
||||
Args:
|
||||
arg: The argument given to a command.
|
||||
count: The count given to a command.
|
||||
countzero: Special value if count is 0.
|
||||
|
||||
Return:
|
||||
The value to use.
|
||||
|
||||
Raise:
|
||||
ValueError: If nothing was set or the value couldn't be converted to
|
||||
an integer.
|
||||
"""
|
||||
if count is not None and arg is not None:
|
||||
raise ValueError("Both count and argument given!")
|
||||
elif arg is not None:
|
||||
try:
|
||||
return int(arg)
|
||||
except ValueError:
|
||||
raise ValueError("Invalid number: {}".format(arg))
|
||||
elif count is not None:
|
||||
if countzero is not None and count == 0:
|
||||
return countzero
|
||||
else:
|
||||
return int(count)
|
||||
elif default is not None:
|
||||
return int(default)
|
||||
else:
|
||||
raise ValueError("Either count or argument have to be set!")
|
||||
|
||||
|
||||
class register: # pylint: disable=invalid-name
|
||||
|
||||
"""Decorator to register a new command handler.
|
||||
|
@ -400,23 +400,12 @@ class TabbedBrowser(TabWidget):
|
||||
Args:
|
||||
index: The tab index to focus, starting with 1.
|
||||
"""
|
||||
if index is not None and count is not None:
|
||||
message.error("Either argument or count must be given!")
|
||||
try:
|
||||
idx = cmdutils.arg_or_count(index, count, default=1,
|
||||
countzero=self.count())
|
||||
except ValueError as e:
|
||||
message.error(e)
|
||||
return
|
||||
elif index is not None:
|
||||
try:
|
||||
idx = int(index)
|
||||
except ValueError:
|
||||
message.error("Argument ({}) needs to be a number!".format(
|
||||
index))
|
||||
return
|
||||
elif count is not None:
|
||||
if count == 0:
|
||||
idx = self.count()
|
||||
else:
|
||||
idx = count
|
||||
else:
|
||||
idx = 1
|
||||
if 1 <= idx <= self.count():
|
||||
self.setCurrentIndex(idx - 1)
|
||||
else:
|
||||
|
Loading…
Reference in New Issue
Block a user