[core] Re-enable WAL support

Implement a generic "load keywords and replace during runtime"
mechanism, with the first concrete use-case of WAL colors (load them
during startup, and during runtime, whenever a matching name is found in
the keywords, replace with the actual color)
This commit is contained in:
Tobias Witek 2020-03-08 14:18:10 +01:00
parent be2864b063
commit 41dc387d0c
2 changed files with 29 additions and 0 deletions

View file

@ -1,10 +1,13 @@
import os import os
import io import io
import json import json
import logging
import core.event import core.event
import util.algorithm import util.algorithm
log = logging.getLogger(__name__)
THEME_BASE_DIR=os.path.dirname(os.path.realpath(__file__)) THEME_BASE_DIR=os.path.dirname(os.path.realpath(__file__))
PATHS=[ PATHS=[
'.', '.',
@ -18,6 +21,7 @@ class Theme(object):
self.__widget_count = 0 self.__widget_count = 0
self.__previous = {} self.__previous = {}
self.__current = {} self.__current = {}
self.__keywords = {}
if raw_data: if raw_data:
self.__data = raw_data self.__data = raw_data
else: else:
@ -26,6 +30,8 @@ class Theme(object):
util.algorithm.merge(self.__data, self.load(icons, 'icons')) util.algorithm.merge(self.__data, self.load(icons, 'icons'))
if iconset != 'auto': if iconset != 'auto':
util.algorithm.merge(self.__data, self.load(iconset, 'icons')) util.algorithm.merge(self.__data, self.load(iconset, 'icons'))
for colors in self.__data.get('colors', []):
util.algorithm.merge(self.__keywords, self.load_keywords(colors))
core.event.register('update', self.__start) core.event.register('update', self.__start)
core.event.register('next-widget', self.__next_widget) core.event.register('next-widget', self.__next_widget)
@ -52,6 +58,24 @@ class Theme(object):
return json.load(data) return json.load(data)
raise RuntimeError('unable to find theme {}'.format(name)) raise RuntimeError('unable to find theme {}'.format(name))
def __load(self, filename, sections):
result = {}
with io.open(os.path.expanduser(filename)) as data:
colors = json.load(data)
for field in sections:
for key in colors[field]:
result[key] = colors[field][key]
return result
def load_keywords(self, name):
try:
if isinstance(name, dict):
return name
if name.lower() == 'wal':
return self.__load('~/.cache/wal/colors.json', ['special', 'colors'])
except Exception as e:
log.error('failed to load colors: {}', e)
def __start(self): def __start(self):
self.__widget_count = 0 self.__widget_count = 0
self.__current.clear() self.__current.clear()
@ -89,6 +113,8 @@ class Theme(object):
theme = self.__get(widget, state, {}) theme = self.__get(widget, state, {})
value = theme.get(key, value) value = theme.get(key, value)
if not type(value) in (list, dict):
value = self.__keywords.get(value, value)
self.__current[key] = value self.__current[key] = value
return value return value

View file

@ -28,3 +28,6 @@
- generalize the battery/hbar/vbar concept - generalize the battery/hbar/vbar concept
- pango output (improve - maybe autodetect? see #531) - pango output (improve - maybe autodetect? see #531)
- allow handlers to specify whether to update or not (e.g. scroll) - allow handlers to specify whether to update or not (e.g. scroll)
## TODO
- theme: load vs. __load vs. load_keywords