From affeba5b8f2c2ce9e2b0f9c0bb4ccbb52b73c8a2 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 15 Feb 2020 14:05:27 +0100 Subject: [PATCH] [core/theme] Add simple theme stub Add support for default fg/bg colors --- core/output.py | 3 +++ core/theme.py | 39 +++++++++++++++++++++++++++++++++++++-- tests/core/test_theme.py | 28 ++++++++++++++++++++++++++++ themes/default.json | 7 +++++++ 4 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 tests/core/test_theme.py create mode 100644 themes/default.json diff --git a/core/output.py b/core/output.py index ac2e418..ca21119 100644 --- a/core/output.py +++ b/core/output.py @@ -8,6 +8,7 @@ class i3(object): def __init__(self, theme=core.theme.Theme()): self._modules = [] self._status = {} + self._theme = theme def modules(self, modules=None): if not modules: @@ -40,6 +41,8 @@ class i3(object): 'full_text': widget.full_text(), 'instance': widget.id(), 'name': module.id(), + 'color': self._theme.fg(widget), + 'background': self._theme.bg(widget), }) return widgets diff --git a/core/theme.py b/core/theme.py index 73aa92a..4721c67 100644 --- a/core/theme.py +++ b/core/theme.py @@ -1,6 +1,41 @@ +import os +import io +import json + +THEME_BASE_DIR=os.path.dirname(os.path.realpath(__file__)) +PATHS=[ + '.', + os.path.join(THEME_BASE_DIR, '../themes'), + os.path.expanduser('~/.config/bumblebee-status/themes'), +] class Theme(object): - def __init__(self, name='default', iconset=None): - pass + def __init__(self, name='default', iconset=None, raw_data=None): + self.name = name + if raw_data: + self._data = raw_data + else: + self._data = self.load(name) + + def load(self, name): + for path in PATHS: + theme_file = os.path.join(path, '{}.json'.format(name)) + if os.path.isfile(theme_file): + with io.open(theme_file, encoding='utf-8') as data: + return json.load(data) + raise RuntimeError('unable to find theme {}'.format(name)) + + def fg(self, widget=None): + return self.__get(widget, 'fg') + + def bg(self, widget=None): + return self.__get(widget, 'bg') + + def __get(self, widget, key, default=None): + value = default + if 'defaults' in self._data: + value = self._data['defaults'].get(key, value) + + return value # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/core/test_theme.py b/tests/core/test_theme.py new file mode 100644 index 0000000..c9dc2a2 --- /dev/null +++ b/tests/core/test_theme.py @@ -0,0 +1,28 @@ +import unittest + +import core.theme + +class theme(unittest.TestCase): + def setUp(self): + self.invalidThemeName = 'this-theme-does-not-exist' + self.validThemeName = 'default' + self.defaults = { + 'defaults': { + 'fg': 'red', 'bg': 'black' + } + } + + def test_invalid_theme(self): + with self.assertRaises(RuntimeError): + core.theme.Theme(self.invalidThemeName) + + def test_valid_theme(self): + theme = core.theme.Theme(self.validThemeName) + self.assertEqual(self.validThemeName, theme.name) + + def test_defaults(self): + theme = core.theme.Theme(raw_data=self.defaults) + self.assertEqual(self.defaults['defaults']['fg'], theme.fg()) + self.assertEqual(self.defaults['defaults']['bg'], theme.bg()) + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/themes/default.json b/themes/default.json new file mode 100644 index 0000000..ddda5fe --- /dev/null +++ b/themes/default.json @@ -0,0 +1,7 @@ +{ + "icons": [ "ascii" ], + "defaults": { + "urgent": true, + "fg": "#aabbcc" + } +}