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)
|
- 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.)
|
- move curcommand stuff to other places (e.g. current widget, etc.)
|
||||||
maybe rename curcommand to commands or so?
|
maybe rename curcommand to commands or so?
|
||||||
also some curcommand stuff is in tabbedbrowser, etc.
|
also some curcommand stuff is in tabbedbrowser, etc.
|
||||||
|
@ -406,19 +406,10 @@ class CurCommandDispatcher(QObject):
|
|||||||
Args:
|
Args:
|
||||||
count: How many steps to take.
|
count: How many steps to take.
|
||||||
"""
|
"""
|
||||||
if zoom is not None and count is not None:
|
try:
|
||||||
message.error("Either argument or count must be given!")
|
level = cmdutils.arg_or_count(zoom, count, default=100)
|
||||||
return
|
except ValueError as e:
|
||||||
if zoom is not None:
|
message.error(e)
|
||||||
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
|
|
||||||
tab = self._tabs.currentWidget()
|
tab = self._tabs.currentWidget()
|
||||||
tab.zoom_perc(level)
|
tab.zoom_perc(level)
|
||||||
|
|
||||||
|
@ -29,6 +29,44 @@ from qutebrowser.commands._command import Command
|
|||||||
cmd_dict = {}
|
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
|
class register: # pylint: disable=invalid-name
|
||||||
|
|
||||||
"""Decorator to register a new command handler.
|
"""Decorator to register a new command handler.
|
||||||
|
@ -400,23 +400,12 @@ class TabbedBrowser(TabWidget):
|
|||||||
Args:
|
Args:
|
||||||
index: The tab index to focus, starting with 1.
|
index: The tab index to focus, starting with 1.
|
||||||
"""
|
"""
|
||||||
if index is not None and count is not None:
|
try:
|
||||||
message.error("Either argument or count must be given!")
|
idx = cmdutils.arg_or_count(index, count, default=1,
|
||||||
|
countzero=self.count())
|
||||||
|
except ValueError as e:
|
||||||
|
message.error(e)
|
||||||
return
|
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():
|
if 1 <= idx <= self.count():
|
||||||
self.setCurrentIndex(idx - 1)
|
self.setCurrentIndex(idx - 1)
|
||||||
else:
|
else:
|
||||||
|
Loading…
Reference in New Issue
Block a user