From 114c2c01d32476fe971cedf94745e1d7bff5e1ea Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sun, 19 Jan 2014 22:25:54 +0100 Subject: [PATCH] First broken attempt at implementing count --- qutebrowser/commands/commands.py | 16 ++++++------- qutebrowser/commands/keys.py | 41 +++++++++++++++++++++++--------- qutebrowser/commands/utils.py | 13 +++++++--- 3 files changed, 48 insertions(+), 22 deletions(-) diff --git a/qutebrowser/commands/commands.py b/qutebrowser/commands/commands.py index a85440fd3..b09645354 100644 --- a/qutebrowser/commands/commands.py +++ b/qutebrowser/commands/commands.py @@ -53,29 +53,29 @@ class Print(Command): nargs = 0 signal = pyqtSignal() -# FIXME implement count class ScrollLeft(Command): nargs = 0 key = 'h' - signal = pyqtSignal() + count = True + signal = pyqtSignal([], [int]) -# FIXME implement count class ScrollDown(Command): nargs = 0 key = 'j' - signal = pyqtSignal() + count = True + signal = pyqtSignal([], [int]) -# FIXME implement count class ScrollUp(Command): nargs = 0 key = 'k' - signal = pyqtSignal() + count = True + signal = pyqtSignal([], [int]) -# FIXME implement count class ScrollRight(Command): nargs = 0 key = 'l' - signal = pyqtSignal() + count = True + signal = pyqtSignal([], [int]) class Undo(Command): nargs = 0 diff --git a/qutebrowser/commands/keys.py b/qutebrowser/commands/keys.py index c65b2af95..8e7df935e 100644 --- a/qutebrowser/commands/keys.py +++ b/qutebrowser/commands/keys.py @@ -2,6 +2,7 @@ from PyQt5.QtCore import QObject, Qt, pyqtSignal from PyQt5.QtWidgets import QShortcut from PyQt5.QtGui import QKeySequence import logging +import re class KeyParser(QObject): keystring = '' @@ -16,28 +17,46 @@ class KeyParser(QObject): def handle(self, e): logging.debug('Got key: {} / text: "{}"'.format(e.key(), e.text())) - if not e.text().strip(): + txt = e.text().strip() + if not txt: logging.debug('Ignoring, no text') return - self.keystring += e.text() + + self.keystring += txt + if self.keystring == ':': self.set_cmd_text.emit(':') self.keystring = '' return + + (countstr, cmdstr) = re.match('^(\d*)(.*)', self.keystring).groups() + + if not cmdstr: + return + try: - cmd = self.key_to_cmd[self.keystring] + cmd = self.key_to_cmd[cmdstr] except KeyError: - pos = len(self.keystring) - if any([self.keystring[-1] == needle[pos-1] + pos = len(cmdstr) + if any([cmdstr[-1] == needle[pos-1] for needle in self.key_to_cmd]): - logging.debug('No match for "{}" (added {})'.format(self.keystring, e.text())) + logging.debug('No match for "{}" (added {})'.format( + self.keystring, txt)) else: - logging.debug('Giving up with "{}", no matches'.format(self.keystring)) + logging.debug('Giving up with "{}", no matches'.format( + self.keystring)) self.keystring = '' + return + + self.keystring = '' + count = int(countstr) if countstr else None + + if cmd.nargs and cmd.nargs != 0: + logging.debug('Filling statusbar with partial command {}'.format( + cmd.name)) + self.set_cmd_text.emit(':{} '.format(cmd.name)) else: - self.keystring = '' - if cmd.nargs and cmd.nargs != 0: - logging.debug('Filling statusbar with partial command {}'.format(cmd.name)) - self.set_cmd_text.emit(':{} '.format(cmd.name)) + if count is not None: + cmd.run(count=count) else: cmd.run() diff --git a/qutebrowser/commands/utils.py b/qutebrowser/commands/utils.py index e3fb4ea34..8f55809b7 100644 --- a/qutebrowser/commands/utils.py +++ b/qutebrowser/commands/utils.py @@ -41,6 +41,7 @@ class Command(QObject): name = None key = None signal = None + count = False bind = True def __init__(self): @@ -54,15 +55,21 @@ class Command(QObject): (self.nargs == '+' and len(argv) < 1)): raise TypeError("Invalid argument count!") - def run(self, argv=None): + 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: - self.signal.emit() + if count is not None: + self.signal.emit(count) + else: + self.signal.emit() elif self.nargs == 1: - self.signal.emit(argv[0]) + if count is not None: + self.signal.emit(count, argv[0]) + else: + self.signal.emit(argv[0]) else: raise NotImplementedError