From 41dc387d0cda5ab7a39ad8ec8a7d14af882d7b0b Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 8 Mar 2020 14:18:10 +0100 Subject: [PATCH] [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) --- core/theme.py | 26 ++++++++++++++++++++++++++ doc/NOTES.md | 3 +++ 2 files changed, 29 insertions(+) diff --git a/core/theme.py b/core/theme.py index 9f7dc8f..7f541ed 100644 --- a/core/theme.py +++ b/core/theme.py @@ -1,10 +1,13 @@ import os import io import json +import logging import core.event import util.algorithm +log = logging.getLogger(__name__) + THEME_BASE_DIR=os.path.dirname(os.path.realpath(__file__)) PATHS=[ '.', @@ -18,6 +21,7 @@ class Theme(object): self.__widget_count = 0 self.__previous = {} self.__current = {} + self.__keywords = {} if raw_data: self.__data = raw_data else: @@ -26,6 +30,8 @@ class Theme(object): util.algorithm.merge(self.__data, self.load(icons, 'icons')) if iconset != 'auto': 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('next-widget', self.__next_widget) @@ -52,6 +58,24 @@ class Theme(object): return json.load(data) 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): self.__widget_count = 0 self.__current.clear() @@ -89,6 +113,8 @@ class Theme(object): theme = self.__get(widget, state, {}) value = theme.get(key, value) + if not type(value) in (list, dict): + value = self.__keywords.get(value, value) self.__current[key] = value return value diff --git a/doc/NOTES.md b/doc/NOTES.md index bdeb4db..02e59b5 100644 --- a/doc/NOTES.md +++ b/doc/NOTES.md @@ -28,3 +28,6 @@ - generalize the battery/hbar/vbar concept - pango output (improve - maybe autodetect? see #531) - allow handlers to specify whether to update or not (e.g. scroll) + +## TODO +- theme: load vs. __load vs. load_keywords