diff --git a/bumblebee/output.py b/bumblebee/output.py index f614447..4b9603b 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -33,10 +33,12 @@ class I3BarOutput(object): def draw_widget(self, result, widget): """Draw a single widget""" full_text = widget.full_text() - if self._theme.prefix(): - full_text = "{}{}".format(self._theme.prefix(), full_text) - if self._theme.suffix(): - full_text = "{}{}".format(full_text, self._theme.suffix()) + prefix = self._theme.prefix(widget) + suffix = self._theme.suffix(widget) + if prefix: + full_text = "{}{}".format(prefix, full_text) + if suffix: + full_text = "{}{}".format(full_text, suffix) result.append({ u"full_text": "{}".format(full_text) }) diff --git a/bumblebee/theme.py b/bumblebee/theme.py index f056c85..b9462f2 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -12,15 +12,24 @@ def theme_path(): class Theme(object): """Represents a collection of icons and colors""" def __init__(self, name): - self._theme = self.load(name) + theme = self.load(name) + self._init(self.load(name)) - def prefix(self): + def _init(self, data): + """Initialize theme from data structure""" + self._defaults = data.get("defaults", {}) + + def prefix(self, widget): """Return the theme prefix for a widget's full text""" - return None + return self._get(widget, "prefix", None) - def suffix(self): + def suffix(self, widget): """Return the theme suffix for a widget's full text""" - return None + return self._get(widget, "suffix", None) + + def loads(self, data): + theme = json.loads(data) + self._init(theme) def load(self, name): """Load and parse a theme file""" @@ -35,4 +44,10 @@ class Theme(object): else: raise bumblebee.error.ThemeLoadError("no such theme: {}".format(name)) + def _get(self, widget, name,default=None): + value = default + value = self._defaults.get(name, value) + + return value + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/test_i3baroutput.py b/tests/test_i3baroutput.py index 067cf42..e621611 100644 --- a/tests/test_i3baroutput.py +++ b/tests/test_i3baroutput.py @@ -54,7 +54,7 @@ class TestI3BarOutput(unittest.TestCase): self.output.draw(self.someWidget) result = json.loads(stdout.getvalue())[0] self.assertEquals(result["full_text"], "{}{}".format( - self.theme.prefix(), self.someWidget.full_text()) + self.theme.prefix(self.someWidget), self.someWidget.full_text()) ) @mock.patch("sys.stdout", new_callable=StringIO) @@ -63,7 +63,7 @@ class TestI3BarOutput(unittest.TestCase): self.output.draw(self.someWidget) result = json.loads(stdout.getvalue())[0] self.assertEquals(result["full_text"], "{}{}".format( - self.someWidget.full_text(), self.theme.suffix()) + self.someWidget.full_text(), self.theme.suffix(self.someWidget)) ) @mock.patch("sys.stdout", new_callable=StringIO) @@ -73,7 +73,7 @@ class TestI3BarOutput(unittest.TestCase): self.output.draw(self.someWidget) result = json.loads(stdout.getvalue())[0] self.assertEquals(result["full_text"], "{}{}{}".format( - self.theme.prefix(), self.someWidget.full_text(), self.theme.suffix()) + self.theme.prefix(self.someWidget), self.someWidget.full_text(), self.theme.suffix(self.someWidget)) ) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/test_theme.py b/tests/test_theme.py index 23ad13c..6d57a71 100644 --- a/tests/test_theme.py +++ b/tests/test_theme.py @@ -3,15 +3,18 @@ import unittest from bumblebee.theme import Theme from bumblebee.error import ThemeLoadError +from tests.util import MockWidget class TestTheme(unittest.TestCase): def setUp(self): self.nonexistentThemeName = "no-such-theme" self.invalidThemeName = "invalid" + self.validThemeName = "solarized-powerline" + self.someWidget = MockWidget("foo") def test_load_valid_theme(self): try: - Theme("solarized-powerline") + Theme(self.validThemeName) except Exception as e: self.fail(e) @@ -23,4 +26,14 @@ class TestTheme(unittest.TestCase): with self.assertRaises(ThemeLoadError): Theme(self.invalidThemeName) + def test_prefix(self): + theme = Theme(self.validThemeName) + theme.loads('{"defaults": { "prefix": "test" }}') + self.assertEquals(theme.prefix(self.someWidget), "test") + + def test_suffix(self): + theme = Theme(self.validThemeName) + theme.loads('{"defaults": { "suffix": "test" }}') + self.assertEquals(theme.suffix(self.someWidget), "test") + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py index aa67cdc..462c6c8 100644 --- a/tests/util.py +++ b/tests/util.py @@ -40,10 +40,10 @@ class MockTheme(object): def set_suffix(self, value): self._suffix = value - def prefix(self): + def prefix(self, widget): return self._prefix - def suffix(self): + def suffix(self, widget): return self._suffix # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4