Rewrite command parsing
This commit is contained in:
parent
64568de851
commit
bc6d7fee25
@ -1,16 +1,16 @@
|
||||
import sys
|
||||
from PyQt5.QtWidgets import QWidget, QApplication
|
||||
from qutebrowser.widgets.mainwindow import MainWindow
|
||||
from qutebrowser.commands import CommandParser
|
||||
import qutebrowser.commands as cmds
|
||||
|
||||
def main():
|
||||
app = QApplication(sys.argv)
|
||||
|
||||
mw = MainWindow()
|
||||
cp = CommandParser()
|
||||
cp = cmds.CommandParser()
|
||||
mw.status.cmd.returnPressed.connect(lambda: cp.parse(mw.status.cmd.text()))
|
||||
cp.openurl.connect(mw.tabs.openurl)
|
||||
cp.tabopen.connect(mw.tabs.tabopen)
|
||||
cmds.cmd_dict['openurl'].connect(mw.tabs.openurl)
|
||||
cmds.cmd_dict['tabopen'].connect(mw.tabs.tabopen)
|
||||
mw.show()
|
||||
|
||||
sys.exit(app.exec_())
|
||||
|
@ -1,12 +1,44 @@
|
||||
from PyQt5.QtCore import QObject, pyqtSignal
|
||||
import inspect, sys
|
||||
|
||||
cmd_dict = {}
|
||||
|
||||
def register_all():
|
||||
def is_cmd(obj):
|
||||
return (inspect.isclass(obj) and obj.__module__ == __name__ and
|
||||
obj.__name__.endswith('Cmd'))
|
||||
|
||||
for (name, cls) in inspect.getmembers(sys.modules[__name__], is_cmd):
|
||||
cls.bind()
|
||||
|
||||
class CommandParser(QObject):
|
||||
openurl = pyqtSignal(str)
|
||||
tabopen = pyqtSignal(str)
|
||||
def parse(self, test):
|
||||
parts = text.lstrip(':').strip().split()
|
||||
cmd = parts[0]
|
||||
args = parts[1:]
|
||||
cmd_dict[cmd]().run(args)
|
||||
|
||||
def parse(self, cmd):
|
||||
c = cmd.split()
|
||||
if c[0] == ':open':
|
||||
self.openurl.emit(c[1])
|
||||
elif c[0] == ':tabopen':
|
||||
self.tabopen.emit(c[1])
|
||||
class Command(QObject):
|
||||
nargs = 0
|
||||
name = ''
|
||||
signal = None
|
||||
|
||||
@classmethod
|
||||
def bind(cls):
|
||||
if cls.name:
|
||||
cmd_dict[cls.name] = cls
|
||||
|
||||
def run(self, *args):
|
||||
self.signal.emit(*args)
|
||||
|
||||
class OpenCmd(Command):
|
||||
nargs = 1
|
||||
name = 'open'
|
||||
signal = pyqtSignal(str)
|
||||
|
||||
class TabOpenCmd(Command):
|
||||
nargs = 1
|
||||
name = 'tabopen'
|
||||
signal = pyqtSignal(str)
|
||||
|
||||
register_all()
|
||||
|
Loading…
Reference in New Issue
Block a user