From 4666b31f8ab40558869527b2de43245ae9be6c68 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Fri, 14 Oct 2016 15:10:48 +0530 Subject: [PATCH] Load config from file --- kitty/config.py | 76 +++++++++++++++++++++++++++++++++++++++++++++++++ kitty/main.py | 16 ++++++----- kitty/term.py | 11 +++++++ 3 files changed, 96 insertions(+), 7 deletions(-) create mode 100644 kitty/config.py create mode 100644 kitty/term.py diff --git a/kitty/config.py b/kitty/config.py new file mode 100644 index 000000000..9a4e7ddb6 --- /dev/null +++ b/kitty/config.py @@ -0,0 +1,76 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2016, Kovid Goyal + +import re +from collections import namedtuple + +key_pat = re.compile(r'([a-zA-Z][a-zA-Z0-9_-]*)\s+(.+)$') + +defaults = {} + +for line in ''' +term xterm-termite +foreground #dddddd +foreground_bold #ffffff +cursor #dddddd +background #000000 + +# black +color0 #000000 +color8 #4d4d4d + +# red +color1 #cc0403 +color9 #f2201f + +# green +color2 #19cb00 +color10 #23fd00 + +# yellow +color3 #cecb00 +color11 #fffd00 + +# blue +color4 #001cd1 +color12 #1389f0 + +# magenta +color5 #cb1ed1 +color13 #fd28ff + +# cyan +color6 #0dcdcd +color14 #14ffff + +# white +color7 #dddddd +color15 #ffffff +'''.splitlines(): + line = line.strip() + if not line or line.startswith('#'): + continue + m = key_pat.match(line) + if m is not None: + key, val = m.groups() + defaults[key] = val +Options = namedtuple('Defaults', ','.join(defaults.keys())) +defaults = Options(**defaults) + + +def load_config(path): + if not path: + return defaults + ans = defaults._asdict() + with open(path) as f: + for line in f: + line = line.strip() + if not line or line.startswith('#'): + continue + m = key_pat.match(line) + if m is not None: + key, val = m.groups() + if key in ans: + ans[key] = val + return Options(**ans) diff --git a/kitty/main.py b/kitty/main.py index 41b83a75e..057f04ba6 100644 --- a/kitty/main.py +++ b/kitty/main.py @@ -9,20 +9,23 @@ import struct import signal from gettext import gettext as _ -from PyQt5.Qt import ( - QApplication, Qt, QMainWindow, QSocketNotifier, QMessageBox -) +from PyQt5.QtCore import Qt, QSocketNotifier +from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox +from .config import load_config from .constants import appname, str_version +from .term import TerminalWidget class MainWindow(QMainWindow): - def __init__(self): + def __init__(self, opts): QMainWindow.__init__(self) self.setWindowTitle(appname) sys.excepthook = self.on_unhandled_error self.handle_unix_signals() + self.terminal = TerminalWidget(opts, self) + self.setCentralWidget(self.terminal) def on_unhandled_error(self, etype, value, tb): if etype == KeyboardInterrupt: @@ -72,14 +75,13 @@ def option_parser(): def main(): args = option_parser().parse_args() - if args.config: - args.config = os.path.abspath(args.config) + opts = load_config(args.config) os.chdir(args.directory) QApplication.setAttribute(Qt.AA_DisableHighDpiScaling, True) app = QApplication([appname]) app.setOrganizationName(args.cls) app.setApplicationName(args.name) - w = MainWindow() + w = MainWindow(opts) w.show() try: app.exec_() diff --git a/kitty/term.py b/kitty/term.py new file mode 100644 index 000000000..2d28d6a05 --- /dev/null +++ b/kitty/term.py @@ -0,0 +1,11 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8 +# License: GPL v3 Copyright: 2016, Kovid Goyal + +from PyQt5.QtWidgets import QWidget + + +class TerminalWidget(QWidget): + + def __init__(self, opts, parent=None): + QWidget.__init__(self, parent)