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): def init_cmds(self):
cmdutils.register_all() cmdutils.register_all()
cmds = cmdutils.cmd_dict cmds = cmdutils.cmd_dict
cmds['open'].signal.connect(self.mainwindow.tabs.openurl) for cmd in cmds.values():
cmds['tabopen'].signal.connect(self.mainwindow.tabs.tabopen) cmd.signal.connect(self.cmd_handler)
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)
self.keyparser.from_cmd_dict(cmdutils.cmd_dict) 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): class Open(Command):
nargs = 1 nargs = 1
key = 'o' key = 'o'
signal = pyqtSignal(str)
class TabOpen(Command): class TabOpen(Command):
nargs = 1 nargs = 1
key = 'O' key = 'O'
signal = pyqtSignal(str)
class TabClose(Command): class TabClose(Command):
nargs = 0 nargs = 0
key = 'd' key = 'd'
signal = pyqtSignal()
class TabNext(Command): class TabNext(Command):
nargs = 0 nargs = 0
key = 'J' key = 'J'
signal = pyqtSignal()
class TabPrev(Command): class TabPrev(Command):
nargs = 0 nargs = 0
key = 'K' key = 'K'
signal = pyqtSignal()
class Quit(Command): class Quit(Command):
nargs = 0 nargs = 0
signal = pyqtSignal()
class Reload(Command): class Reload(Command):
nargs = 0 nargs = 0
key = 'r' key = 'r'
signal = pyqtSignal()
class Stop(Command): class Stop(Command):
nargs = 0 nargs = 0
signal = pyqtSignal()
class Back(Command): class Back(Command):
nargs = 0 nargs = 0
key = 'H' key = 'H'
signal = pyqtSignal()
class Forward(Command): class Forward(Command):
nargs = 0 nargs = 0
key = 'L' key = 'L'
signal = pyqtSignal()
class Print(Command): class Print(Command):
nargs = 0 nargs = 0
signal = pyqtSignal()
class ScrollLeft(Command): class ScrollLeft(Command):
nargs = 0 nargs = 0
key = 'h' key = 'h'
count = True count = True
signal = pyqtSignal([], [int])
class ScrollDown(Command): class ScrollDown(Command):
nargs = 0 nargs = 0
key = 'j' key = 'j'
count = True count = True
signal = pyqtSignal([], [int])
class ScrollUp(Command): class ScrollUp(Command):
nargs = 0 nargs = 0
key = 'k' key = 'k'
count = True count = True
signal = pyqtSignal([], [int])
class ScrollRight(Command): class ScrollRight(Command):
nargs = 0 nargs = 0
key = 'l' key = 'l'
count = True count = True
signal = pyqtSignal([], [int])
class Undo(Command): class Undo(Command):
nargs = 0 nargs = 0
key = 'u' key = 'u'
signal = pyqtSignal()
class ScrollStart(Command): class ScrollStart(Command):
nargs = 0 nargs = 0
key = 'gg' key = 'gg'
signal = pyqtSignal()
class ScrollEnd(Command): class ScrollEnd(Command):
nargs = 0 nargs = 0
key = 'G' key = 'G'
signal = pyqtSignal()

View File

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

View File

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

View File

@ -65,16 +65,24 @@ class TabbedBrowser(TabWidget):
# FIXME display warning if end of history # FIXME display warning if end of history
self.currentWidget().forward() 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) 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) 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) 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) self.currentWidget().page().mainFrame().scroll(count, 0)
def scroll_start_act(self): def scroll_start_act(self):