From c52cb995183daef87d3e4b45f54a648efd353d17 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Fri, 9 Dec 2016 08:58:45 +0100 Subject: [PATCH] [core/theme] Add support for foreground and background colors Themes can now define "fg" and "bg" attributes that are used for foreground (text) color and background color. see #23 --- bumblebee/output.py | 4 +++- bumblebee/theme.py | 8 ++++++++ tests/test_i3baroutput.py | 12 ++++++++++++ tests/test_theme.py | 14 ++++++++++++++ tests/util.py | 14 ++++++++++++++ themes/test.json | 8 +++++++- 6 files changed, 58 insertions(+), 2 deletions(-) diff --git a/bumblebee/output.py b/bumblebee/output.py index 92bad91..5eba197 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -53,7 +53,9 @@ class I3BarOutput(object): if suffix: full_text = u"{}{}".format(full_text, suffix) self._widgets.append({ - u"full_text": u"{}".format(full_text) + u"full_text": u"{}".format(full_text), + "color": self._theme.fg(widget), + "background": self._theme.bg(widget), }) def begin(self): diff --git a/bumblebee/theme.py b/bumblebee/theme.py index 2498655..28fe5b3 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -30,6 +30,14 @@ class Theme(object): """Return the theme suffix for a widget's full text""" return self._get(widget, "suffix", None) + def fg(self, widget): + """Return the foreground color for this widget""" + return self._get(widget, "fg", None) + + def bg(self, widget): + """Return the background color for this widget""" + return self._get(widget, "bg", None) + def loads(self, data): """Initialize the theme from a JSON string""" theme = json.loads(data) diff --git a/tests/test_i3baroutput.py b/tests/test_i3baroutput.py index dbf2feb..a836247 100644 --- a/tests/test_i3baroutput.py +++ b/tests/test_i3baroutput.py @@ -19,6 +19,8 @@ class TestI3BarOutput(unittest.TestCase): self.expectedStart = json.dumps({"version": 1, "click_events": True}) + "[\n" self.expectedStop = "]\n" self.someWidget = MockWidget("foo bar baz") + self.anyColor = "#ababab" + self.anotherColor = "#cccccc" @mock.patch("sys.stdout", new_callable=StringIO) def test_start(self, stdout): @@ -89,4 +91,14 @@ class TestI3BarOutput(unittest.TestCase): self.theme.suffix(self.someWidget) )) + @mock.patch("sys.stdout", new_callable=StringIO) + def test_colors(self, stdout): + self.theme.set_fg(self.anyColor) + self.theme.set_bg(self.anotherColor) + self.output.draw(self.someWidget) + self.output.flush() + result = json.loads(stdout.getvalue())[0] + self.assertEquals(result["color"], self.anyColor) + self.assertEquals(result["background"], self.anotherColor) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/test_theme.py b/tests/test_theme.py index a956d4f..51b10a9 100644 --- a/tests/test_theme.py +++ b/tests/test_theme.py @@ -14,10 +14,14 @@ class TestTheme(unittest.TestCase): self.theme = Theme(self.validThemeName) self.widgetTheme = "test-widget" + self.defaultColor = "#000000" + self.defaultBgColor = "#111111" + self.widgetBgColor = "#222222" self.defaultPrefix = "default-prefix" self.defaultSuffix = "default-suffix" self.widgetPrefix = "widget-prefix" self.widgetSuffix = "widget-suffix" + self.widgetColor = "#ababab" def test_load_valid_theme(self): try: @@ -43,4 +47,14 @@ class TestTheme(unittest.TestCase): self.someWidget.set_module(self.widgetTheme) self.assertEquals(self.theme.prefix(self.someWidget), self.widgetPrefix) + def test_widget_fg(self): + self.assertEquals(self.theme.fg(self.someWidget), self.defaultColor) + self.someWidget.set_module(self.widgetTheme) + self.assertEquals(self.theme.fg(self.someWidget), self.widgetColor) + + def test_widget_bg(self): + self.assertEquals(self.theme.bg(self.someWidget), self.defaultBgColor) + self.someWidget.set_module(self.widgetTheme) + self.assertEquals(self.theme.bg(self.someWidget), self.widgetBgColor) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py index 3779c1c..564fe54 100644 --- a/tests/util.py +++ b/tests/util.py @@ -49,6 +49,8 @@ class MockTheme(object): def __init__(self): self._prefix = None self._suffix = None + self._fg = None + self._bg = None def set_prefix(self, value): self._prefix = value @@ -56,10 +58,22 @@ class MockTheme(object): def set_suffix(self, value): self._suffix = value + def set_fg(self, value): + self._fg = value + + def set_bg(self, value): + self._bg = value + def prefix(self, widget): return self._prefix def suffix(self, widget): return self._suffix + def fg(self, widget): + return self._fg + + def bg(self, widget): + return self._bg + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/themes/test.json b/themes/test.json index 5f5cd00..ea713bd 100644 --- a/themes/test.json +++ b/themes/test.json @@ -2,6 +2,12 @@ "icons": [ "test" ], "defaults": { "prefix": "default-prefix", - "suffix": "default-suffix" + "suffix": "default-suffix", + "fg": "#000000", + "bg": "#111111" + }, + "test-widget": { + "fg": "#ababab", + "bg": "#222222" } }