Load config from file
This commit is contained in:
76
kitty/config.py
Normal file
76
kitty/config.py
Normal file
@@ -0,0 +1,76 @@
|
||||
#!/usr/bin/env python
|
||||
# vim:fileencoding=utf-8
|
||||
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
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)
|
||||
@@ -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_()
|
||||
|
||||
11
kitty/term.py
Normal file
11
kitty/term.py
Normal file
@@ -0,0 +1,11 @@
|
||||
#!/usr/bin/env python
|
||||
# vim:fileencoding=utf-8
|
||||
# License: GPL v3 Copyright: 2016, Kovid Goyal <kovid at kovidgoyal.net>
|
||||
|
||||
from PyQt5.QtWidgets import QWidget
|
||||
|
||||
|
||||
class TerminalWidget(QWidget):
|
||||
|
||||
def __init__(self, opts, parent=None):
|
||||
QWidget.__init__(self, parent)
|
||||
Reference in New Issue
Block a user