From c1f1e1a9396b40de837d5803ca372c02782640a0 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Fri, 9 Dec 2016 12:55:16 +0100 Subject: [PATCH] [core/themes] Add separator customization Add customized separators: * The default separators are automatically disabled if custom separators are used (to "just" disable the default, use empty custom separators) * Use previous background color as their background color and the current background color as foreground color * Allow the separator-block-width to be configured see #23 --- bumblebee/output.py | 13 ++++++++++++- bumblebee/theme.py | 21 +++++++++++++++++++-- tests/test_theme.py | 24 ++++++++++++++++++++++++ tests/util.py | 8 ++++++++ themes/solarized-powerline.json | 1 - themes/test.json | 4 +++- 6 files changed, 66 insertions(+), 5 deletions(-) diff --git a/bumblebee/output.py b/bumblebee/output.py index d3ea0f4..ace407f 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -48,10 +48,21 @@ class I3BarOutput(object): full_text = u"{}{}".format(prefix, full_text) if suffix: full_text = u"{}{}".format(full_text, suffix) + separator = self._theme.separator(widget) + if separator: + self._widgets.append({ + u"full_text": separator, + "separator": False, + "color": self._theme.separator_fg(widget), + "background": self._theme.separator_bg(widget), + "separator_block_width": self._theme.separator_block_width(widget), + }) self._widgets.append({ - u"full_text": u"{}".format(full_text), + u"full_text": full_text, "color": self._theme.fg(widget), "background": self._theme.bg(widget), + "separator_block_width": self._theme.separator_block_width(widget), + "separator": True if separator is None else False, }) def begin(self): diff --git a/bumblebee/theme.py b/bumblebee/theme.py index a4d40c6..6e328e0 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -36,6 +36,7 @@ class Theme(object): self._cycle = self._cycles[0] if len(self._cycles) > 0 else {} self._cycle_idx = 0 self._widget = None + self._prevbg = None def prefix(self, widget): """Return the theme prefix for a widget's full text""" @@ -57,6 +58,20 @@ class Theme(object): """Return the background color for this widget""" return self._get(widget, "bg", None) + def separator(self, widget): + """Return the separator between widgets""" + return self._get(widget, "separator", None) + + def separator_fg(self, widget): + return self.bg(widget) + + def separator_bg(self, widget): + return self._prevbg + + def separator_block_width(self, widget): + """Return the SBW""" + return self._get(widget, "separator-block-width", None) + def loads(self, data): """Initialize the theme from a JSON string""" theme = json.loads(data) @@ -87,9 +102,11 @@ class Theme(object): self._widget = widget if self._widget != widget: + self._prevbg = self.bg(self._widget) self._widget = widget - self._cycle_idx = (self._cycle_idx + 1) % len(self._cycles) - self._cycle = self._cycles[self._cycle_idx] + if len(self._cycles) > 0: + self._cycle_idx = (self._cycle_idx + 1) % len(self._cycles) + self._cycle = self._cycles[self._cycle_idx] module_theme = self._theme.get(widget.module, {}) diff --git a/tests/test_theme.py b/tests/test_theme.py index b039d17..d2220cd 100644 --- a/tests/test_theme.py +++ b/tests/test_theme.py @@ -61,6 +61,14 @@ class TestTheme(unittest.TestCase): self.themedWidget.module = self.widgetTheme self.assertEquals(self.theme.bg(self.themedWidget), self.widgetBgColor) + def test_absent_cycle(self): + theme = self.theme + try: + theme.fg(self.anyWidget) + theme.fg(self.anotherWidget) + except Exception as e: + self.fail(e) + def test_reset(self): theme = self.cycleTheme data = theme.data() @@ -70,4 +78,20 @@ class TestTheme(unittest.TestCase): theme.reset() self.assertEquals(theme.fg(self.anyWidget), data["cycle"][0]["fg"]) + def test_separator_block_width(self): + theme = self.theme + data = theme.data() + + self.assertEquals(theme.separator_block_width(self.anyWidget), data["defaults"]["separator-block-width"]) + + def test_separator(self): + for theme in [self.theme, self.cycleTheme]: + data = theme.data() + theme.reset() + prev_bg = theme.bg(self.anyWidget) + theme.bg(self.anotherWidget) + + self.assertEquals(theme.separator_fg(self.anotherWidget), theme.bg(self.anotherWidget)) + self.assertEquals(theme.separator_bg(self.anotherWidget), prev_bg) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py index 3a8095d..0d78aef 100644 --- a/tests/util.py +++ b/tests/util.py @@ -45,10 +45,18 @@ class MockTheme(object): self.attr_suffix = None self.attr_fg = None self.attr_bg = None + self.attr_separator = None + self.attr_separator_block_width = 0 def reset(self): pass + def separator_block_width(self, widget): + return self.attr_separator_block_width + + def separator(self, widget): + return self.attr_separator + def prefix(self, widget): return self.attr_prefix diff --git a/themes/solarized-powerline.json b/themes/solarized-powerline.json index 22b6faf..cb9049b 100644 --- a/themes/solarized-powerline.json +++ b/themes/solarized-powerline.json @@ -1,7 +1,6 @@ { "icons": [ "awesome-fonts" ], "defaults": { - "default-separators": false, "separator-block-width": 0, "warning": { "fg": "#002b36", diff --git a/themes/test.json b/themes/test.json index ea713bd..4640634 100644 --- a/themes/test.json +++ b/themes/test.json @@ -4,7 +4,9 @@ "prefix": "default-prefix", "suffix": "default-suffix", "fg": "#000000", - "bg": "#111111" + "bg": "#111111", + "separator": " * ", + "separator-block-width": 10 }, "test-widget": { "fg": "#ababab",