Rewrite whole command system.

Every command signal now returns a tuple (count, args) and gets handled
in app.py by cmd_handler. This makes things easier in a lot of places.
This commit is contained in:
Florian Bruhin 2014-01-19 22:55:00 +01:00
parent 114c2c01d3
commit 1e8cb42c00
5 changed files with 77 additions and 72 deletions

View File

@ -49,25 +49,48 @@ class QuteBrowser(QApplication):
def init_cmds(self):
cmdutils.register_all()
cmds = cmdutils.cmd_dict
cmds['open'].signal.connect(self.mainwindow.tabs.openurl)
cmds['tabopen'].signal.connect(self.mainwindow.tabs.tabopen)
cmds['quit'].signal.connect(QApplication.closeAllWindows) # FIXME
cmds['tabclose'].signal.connect(self.mainwindow.tabs.close_act)
cmds['tabprev'].signal.connect(self.mainwindow.tabs.switch_prev)
cmds['tabnext'].signal.connect(self.mainwindow.tabs.switch_next)
cmds['reload'].signal.connect(self.mainwindow.tabs.reload_act)
cmds['stop'].signal.connect(self.mainwindow.tabs.stop_act)
cmds['back'].signal.connect(self.mainwindow.tabs.back_act)
cmds['forward'].signal.connect(self.mainwindow.tabs.forward_act)
cmds['print'].signal.connect(self.mainwindow.tabs.print_act)
cmds['scrolldown'].signal.connect(self.mainwindow.tabs.scroll_down_act)
cmds['scrollup'].signal.connect(self.mainwindow.tabs.scroll_up_act)
cmds['scrollleft'].signal.connect(self.mainwindow.tabs.scroll_left_act)
cmds['scrollright'].signal.connect(
self.mainwindow.tabs.scroll_right_act)
cmds['scrollstart'].signal.connect(
self.mainwindow.tabs.scroll_start_act)
cmds['scrollend'].signal.connect(
self.mainwindow.tabs.scroll_end_act)
cmds['undo'].signal.connect(self.mainwindow.tabs.undo_close)
for cmd in cmds.values():
cmd.signal.connect(self.cmd_handler)
self.keyparser.from_cmd_dict(cmdutils.cmd_dict)
def cmd_handler(self, tpl):
(count, argv) = tpl
cmd = argv[0]
args = argv[1:]
if cmd == 'open':
self.mainwindow.tabs.openurl(*args)
elif cmd == 'tabopen':
self.mainwindow.tabs.tabopen(*args)
elif cmd == 'quit':
QApplication.closeAllWindows() # FIXME
elif cmd == 'tabclose':
self.mainwindow.tabs.close_act()
elif cmd == 'tabprev':
self.mainwindow.tabs.switch_prev()
elif cmd == 'tabnext':
self.mainwindow.tabs.switch_next()
elif cmd == 'reload':
self.mainwindow.tabs.reload_act()
elif cmd == 'stop':
self.mainwindow.tabs.stop_act()
elif cmd == 'back':
self.mainwindow.tabs.back_act()
elif cmd == 'forward':
self.mainwindow.tabs.forward_act()
elif cmd == 'print':
self.mainwindow.tabs.print_act()
elif cmd == 'scrolldown':
self.mainwindow.tabs.scroll_down_act(count=count)
elif cmd == 'scrollup':
self.mainwindow.tabs.scroll_up_act(count=count)
elif cmd == 'scrollleft':
self.mainwindow.tabs.scroll_left_act(count=count)
elif cmd == 'scrollright':
self.mainwindow.tabs.scroll_right_act(count=count)
elif cmd == 'scrollstart':
self.mainwindow.tabs.scroll_start_act()
elif cmd == 'scrollend':
self.mainwindow.tabs.scroll_end_act()
elif cmd == 'undo':
self.mainwindow.tabs.undo_close()

View File

@ -4,90 +4,72 @@ from qutebrowser.commands.utils import Command
class Open(Command):
nargs = 1
key = 'o'
signal = pyqtSignal(str)
class TabOpen(Command):
nargs = 1
key = 'O'
signal = pyqtSignal(str)
class TabClose(Command):
nargs = 0
key = 'd'
signal = pyqtSignal()
class TabNext(Command):
nargs = 0
key = 'J'
signal = pyqtSignal()
class TabPrev(Command):
nargs = 0
key = 'K'
signal = pyqtSignal()
class Quit(Command):
nargs = 0
signal = pyqtSignal()
class Reload(Command):
nargs = 0
key = 'r'
signal = pyqtSignal()
class Stop(Command):
nargs = 0
signal = pyqtSignal()
class Back(Command):
nargs = 0
key = 'H'
signal = pyqtSignal()
class Forward(Command):
nargs = 0
key = 'L'
signal = pyqtSignal()
class Print(Command):
nargs = 0
signal = pyqtSignal()
class ScrollLeft(Command):
nargs = 0
key = 'h'
count = True
signal = pyqtSignal([], [int])
class ScrollDown(Command):
nargs = 0
key = 'j'
count = True
signal = pyqtSignal([], [int])
class ScrollUp(Command):
nargs = 0
key = 'k'
count = True
signal = pyqtSignal([], [int])
class ScrollRight(Command):
nargs = 0
key = 'l'
count = True
signal = pyqtSignal([], [int])
class Undo(Command):
nargs = 0
key = 'u'
signal = pyqtSignal()
class ScrollStart(Command):
nargs = 0
key = 'gg'
signal = pyqtSignal()
class ScrollEnd(Command):
nargs = 0
key = 'G'
signal = pyqtSignal()

View File

@ -42,6 +42,7 @@ class KeyParser(QObject):
for needle in self.key_to_cmd]):
logging.debug('No match for "{}" (added {})'.format(
self.keystring, txt))
return
else:
logging.debug('Giving up with "{}", no matches'.format(
self.keystring))
@ -55,8 +56,7 @@ class KeyParser(QObject):
logging.debug('Filling statusbar with partial command {}'.format(
cmd.name))
self.set_cmd_text.emit(':{} '.format(cmd.name))
elif count is not None:
cmd.run(count=count)
else:
if count is not None:
cmd.run(count=count)
else:
cmd.run()
cmd.run()

View File

@ -23,18 +23,18 @@ class CommandParser(QObject):
def parse(self, text):
parts = text.strip().split()
cmd = parts[0]
argv = parts[1:]
args = parts[1:]
try:
obj = cmd_dict[cmd]
except KeyError:
self.error.emit("{}: no such command".format(cmd))
return
try:
obj.check(argv)
obj.check(args)
except TypeError:
self.error.emit("{}: invalid argument count".format(cmd))
return
obj.run(argv)
obj.run(args)
class Command(QObject):
nargs = 0
@ -43,33 +43,25 @@ class Command(QObject):
signal = None
count = False
bind = True
signal = pyqtSignal(tuple)
def __init__(self):
super().__init__()
if self.name is None:
self.name = self.__class__.__name__.lower()
def check(self, argv):
if ((isinstance(self.nargs, int) and len(argv) != self.nargs) or
(self.nargs == '?' and len(argv) > 1) or
(self.nargs == '+' and len(argv) < 1)):
def check(self, args):
if ((isinstance(self.nargs, int) and len(args) != self.nargs) or
(self.nargs == '?' and len(args) > 1) or
(self.nargs == '+' and len(args) < 1)):
raise TypeError("Invalid argument count!")
def run(self, argv=None, count=None):
logging.debug("Cmd called: {}({})".format(self.__class__.__name__,
", ".join(argv) if argv else ''))
if not self.signal:
raise NotImplementedError
# some sane defaults
if self.nargs == 0:
if count is not None:
self.signal.emit(count)
else:
self.signal.emit()
elif self.nargs == 1:
if count is not None:
self.signal.emit(count, argv[0])
else:
self.signal.emit(argv[0])
else:
raise NotImplementedError
def run(self, args=None, count=None):
countstr = ' * {}'.format(count) if count is not None else ''
argstr = ", ".join(args) if args else ''
logging.debug("Cmd called: {}({}){}".format(self.name, argstr,
countstr))
argv = [self.name]
if args is not None:
argv += args
self.signal.emit((count, argv))

View File

@ -65,16 +65,24 @@ class TabbedBrowser(TabWidget):
# FIXME display warning if end of history
self.currentWidget().forward()
def scroll_down_act(self, count=50):
def scroll_down_act(self, count=None):
if count is None:
count = 50
self.currentWidget().page().mainFrame().scroll(0, count)
def scroll_up_act(self, count=50):
def scroll_up_act(self, count=None):
if count is None:
count = 50
self.currentWidget().page().mainFrame().scroll(0, -count)
def scroll_left_act(self, count=50):
def scroll_left_act(self, count=None):
if count is None:
count = 50
self.currentWidget().page().mainFrame().scroll(-count, 0)
def scroll_right_act(self, count=50):
def scroll_right_act(self, count=None):
if count is None:
count = 50
self.currentWidget().page().mainFrame().scroll(count, 0)
def scroll_start_act(self):