adding profile to the layout file

fix layout examples - adding profile

refactor - changing layoutfile to configjson
This commit is contained in:
David Levanon 2020-09-20 23:54:10 +03:00 committed by David Levanon
parent 69c8a3f1d9
commit 130b0a0af1
10 changed files with 271 additions and 161 deletions

View File

@ -1,4 +1,5 @@
{ {
"layout": {
"tab1": [ "tab1": [
{ {
"children": [ "children": [
@ -27,4 +28,9 @@
] ]
} }
] ]
},
"profile": {
"background_color": "#170717",
"foreground_color": "#f5c0b7"
}
} }

View File

@ -1,4 +1,5 @@
{ {
"layout": {
"vertical": false, "vertical": false,
"tab1": [ "tab1": [
{ {
@ -9,3 +10,4 @@
} }
] ]
} }
}

View File

@ -1,4 +1,5 @@
{ {
"layout": {
"tab1": [ "tab1": [
{ {
"command": "bash" "command": "bash"
@ -11,3 +12,4 @@
} }
] ]
} }
}

View File

@ -1,4 +1,5 @@
{ {
"layout": {
"tab1": [ "tab1": [
{ {
"command": "bash" "command": "bash"
@ -24,3 +25,4 @@
} }
] ]
} }
}

View File

@ -1,4 +1,5 @@
{ {
"layout": {
"vertical": false, "vertical": false,
"tab1": [ "tab1": [
{ {
@ -34,3 +35,4 @@
} }
] ]
} }
}

View File

@ -1,4 +1,5 @@
{ {
"layout": {
"tab1": [ "tab1": [
{ {
"children": [ "children": [
@ -8,6 +9,9 @@
{ {
"command": "bash" "command": "bash"
}, },
{
"command": "bash"
},
{ {
"command": "bash" "command": "bash"
} }
@ -15,6 +19,41 @@
}, },
{ {
"children": [ "children": [
{
"command": "bash"
},
{
"command": "bash"
},
{
"command": "bash"
},
{
"command": "bash"
}
]
},
{
"children": [
{
"command": "bash"
},
{
"command": "bash"
},
{
"command": "bash"
},
{
"command": "bash"
}
]
},
{
"children": [
{
"command": "bash"
},
{ {
"command": "bash" "command": "bash"
}, },
@ -27,4 +66,10 @@
] ]
} }
] ]
},
"profile": {
"background_color": "#070717",
"foreground_color": "#f5c0b7",
"font": "Monospace 16"
}
} }

View File

@ -51,6 +51,7 @@ from terminatorlib.factory import Factory
from terminatorlib.version import APP_NAME, APP_VERSION from terminatorlib.version import APP_NAME, APP_VERSION
from terminatorlib.util import dbg, err from terminatorlib.util import dbg, err
from terminatorlib.layoutlauncher import LayoutLauncher from terminatorlib.layoutlauncher import LayoutLauncher
from terminatorlib.configjson import ConfigJson
if __name__ == '__main__': if __name__ == '__main__':
# Workaround for IBus intefering with broadcast when using dead keys # Workaround for IBus intefering with broadcast when using dead keys
@ -68,6 +69,11 @@ if __name__ == '__main__':
OPTIONS,dbus_options = terminatorlib.optionparse.parse_options() OPTIONS,dbus_options = terminatorlib.optionparse.parse_options()
if OPTIONS.configjson:
layoutname = ConfigJson().extend_config(OPTIONS.configjson)
if layoutname and ((not OPTIONS.layout) or OPTIONS.layout == 'default'):
OPTIONS.layout = layoutname
TERMINATOR = Terminator() TERMINATOR = Terminator()
TERMINATOR.set_origcwd(ORIGCWD) TERMINATOR.set_origcwd(ORIGCWD)

View File

@ -2,9 +2,15 @@ from .util import dbg, err
from os import path from os import path
import sys import sys
import json import json
import traceback import copy
from .config import Config
class ConfigJson(object):
JSON_PROFILE_NAME = "__internal_json_profile__"
JSON_LAYOUT_NAME = "__internal_json_layout__"
profile_to_use = 'default'
class LayoutFile(object):
def build_single_tab_layout(self, layoutjson, vertical): def build_single_tab_layout(self, layoutjson, vertical):
dbg ('Budiling a single tab layout from json: %s ' % layoutjson) dbg ('Budiling a single tab layout from json: %s ' % layoutjson)
@ -50,6 +56,7 @@ class LayoutFile(object):
'type': 'Terminal', 'type': 'Terminal',
'order': order, 'order': order,
'parent': parent, 'parent': parent,
'profile': self.profile_to_use,
'command': layoutjson['command'] 'command': layoutjson['command']
} }
@ -87,21 +94,7 @@ class LayoutFile(object):
counter += 1 counter += 1
def get_layout_file(self, layoutname): def get_layout(self, layoutjson):
if (not path.exists(layoutname)):
return None
dbg('Loading layout from a file: %s' % layoutname)
layoutjson = None
try:
with open(layoutname) as json_file:
layoutjson = json.load(json_file)
except Exception as ex:
err('Error loading layout file %s (%s)' % (layoutname, ex))
return None
try: try:
vertical = True vertical = True
if "vertical" in layoutjson: if "vertical" in layoutjson:
@ -119,6 +112,57 @@ class LayoutFile(object):
dbg('Json layout is: %s' % result) dbg('Json layout is: %s' % result)
return result return result
except Exception as ex: except Exception as ex:
err('Error building a layout from file %s (%s)' % (layoutname, ex)) err('Error building a layout from file %s' % ex)
traceback.print_exc(ex) return None
def get_profile(self, profilejson, baseprofile):
try:
result = copy.deepcopy(baseprofile)
result.update(profilejson)
dbg('Json profile is: %s' % result)
return result
except Exception as ex:
err('Error building a profile from json file %s' % ex)
return None
def read_config(self, jsonfile):
if not path.exists(jsonfile):
dbg("Json config file is missing %s" % jsonfile)
return None
dbg('Loading config json from a file: %s' % jsonfile)
layoutjson = None
try:
with open(jsonfile) as json_file:
layoutjson = json.load(json_file)
except Exception as ex:
err('Error loading config json file %s (%s)' % (jsonfile, ex))
return None
return layoutjson
def extend_config(self, jsonfile):
configjson = self.read_config(jsonfile)
if not configjson:
return None
config = Config()
if 'profile' in configjson:
profile = self.get_profile(configjson['profile'], config.base.profiles['default'])
if profile:
config.base.profiles[self.JSON_PROFILE_NAME] = profile
self.profile_to_use = self.JSON_PROFILE_NAME
if 'layout' in configjson:
layout = self.get_layout(configjson['layout'])
if layout:
config.base.layouts[self.JSON_LAYOUT_NAME] = layout
return self.JSON_LAYOUT_NAME
return None return None

View File

@ -74,6 +74,8 @@ def parse_options():
'execute inside the terminal, and its arguments')) 'execute inside the terminal, and its arguments'))
parser.add_option('-g', '--config', dest='config', parser.add_option('-g', '--config', dest='config',
help=_('Specify a config file')) help=_('Specify a config file'))
parser.add_option('-j', '--config-json', dest='configjson',
help=_('Specify a partial config json file'))
parser.add_option('-x', '--execute', dest='execute', action='callback', parser.add_option('-x', '--execute', dest='execute', action='callback',
callback=execute_cb, callback=execute_cb,
help=_('Use the rest of the command line as a command to execute ' help=_('Use the rest of the command line as a command to execute '

View File

@ -16,7 +16,6 @@ from .keybindings import Keybindings
from .util import dbg, err, enumerate_descendants from .util import dbg, err, enumerate_descendants
from .factory import Factory from .factory import Factory
from .version import APP_NAME, APP_VERSION from .version import APP_NAME, APP_VERSION
from .layoutfile import LayoutFile
try: try:
from gi.repository import GdkX11 from gi.repository import GdkX11
@ -50,6 +49,7 @@ class Terminator(Borg):
keybindings = None keybindings = None
style_providers = None style_providers = None
last_focused_term = None last_focused_term = None
layout_file = None
origcwd = None origcwd = None
dbus_path = None dbus_path = None
@ -227,9 +227,7 @@ class Terminator(Borg):
self.prelayout_windows = self.windows[:] self.prelayout_windows = self.windows[:]
layout = copy.deepcopy(self.config.layout_get_config(layoutname)) layout = copy.deepcopy(self.config.layout_get_config(layoutname))
if not layout:
layoutfile = LayoutFile()
layout = layoutfile.get_layout_file(layoutname)
if not layout: if not layout:
# User specified a non-existent layout. default to one Terminal # User specified a non-existent layout. default to one Terminal
err('layout %s not defined' % layout) err('layout %s not defined' % layout)
@ -425,6 +423,7 @@ class Terminator(Borg):
background-color: alpha(%s, %s); } background-color: alpha(%s, %s); }
""" """
profiles = self.config.base.profiles profiles = self.config.base.profiles
for profile in list(profiles.keys()): for profile in list(profiles.keys()):
if profiles[profile]['use_theme_colors']: if profiles[profile]['use_theme_colors']:
# Create a dummy window/vte and realise it so it has correct # Create a dummy window/vte and realise it so it has correct