From 64f5fc100e6fa3383f70bc2516938b4c95b9ceea Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Thu, 8 Dec 2016 11:31:20 +0100 Subject: [PATCH] [core/theme] Add prefix/postfix methods Add a way to specify prefix and postfix strings to the full text of a widget's text. Currently, the theme does not fill those yet. see #23 --- bumblebee-status | 4 +++- bumblebee/config.py | 8 +++++++- bumblebee/output.py | 11 ++++++++--- bumblebee/theme.py | 8 ++++++++ tests/test_i3baroutput.py | 31 ++++++++++++++++++++++++++++++- tests/util.py | 17 +++++++++++++++++ 6 files changed, 73 insertions(+), 6 deletions(-) diff --git a/bumblebee-status b/bumblebee-status index fcb5679..28e8e88 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -1,13 +1,15 @@ #!/usr/bin/env python import sys +import bumblebee.theme import bumblebee.engine import bumblebee.config import bumblebee.output def main(): config = bumblebee.config.Config(sys.argv[1:]) - output = bumblebee.output.I3BarOutput() + theme = bumblebee.theme.Theme(config.theme()) + output = bumblebee.output.I3BarOutput(theme=theme) engine = bumblebee.engine.Engine( config=config, output=output, diff --git a/bumblebee/config.py b/bumblebee/config.py index 58e3ca2..386f12f 100644 --- a/bumblebee/config.py +++ b/bumblebee/config.py @@ -7,12 +7,14 @@ module parameters, etc.) to all other components import argparse MODULE_HELP = "" +THEME_HELP = "" def create_parser(): """Create the argument parser""" parser = argparse.ArgumentParser(description="display system data in the i3bar") parser.add_argument("-m", "--modules", nargs="+", default=[], - help=MODULE_HELP) + help=MODULE_HELP) + parser.add_argument("-t", "--theme", default="default", help=THEME_HELP) return parser class Config(object): @@ -32,4 +34,8 @@ class Config(object): "name": x if not ":" in x else x.split(":")[1], } for x in self._args.modules] + def theme(self): + """Return the name of the selected theme""" + return self._args.theme + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/bumblebee/output.py b/bumblebee/output.py index 47289ce..f614447 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -19,8 +19,8 @@ class Widget(object): class I3BarOutput(object): """Manage output according to the i3bar protocol""" - def __init__(self): - pass + def __init__(self, theme): + self._theme = theme def start(self): """Print start preamble for i3bar protocol""" @@ -32,8 +32,13 @@ 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()) result.append({ - u"full_text": widget.full_text() + u"full_text": "{}".format(full_text) }) def draw(self, widgets, engine=None): diff --git a/bumblebee/theme.py b/bumblebee/theme.py index e07c1b8..f056c85 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -14,6 +14,14 @@ class Theme(object): def __init__(self, name): self._theme = self.load(name) + def prefix(self): + """Return the theme prefix for a widget's full text""" + return None + + def suffix(self): + """Return the theme suffix for a widget's full text""" + return None + def load(self, name): """Load and parse a theme file""" path = theme_path() diff --git a/tests/test_i3baroutput.py b/tests/test_i3baroutput.py index 4a277ae..067cf42 100644 --- a/tests/test_i3baroutput.py +++ b/tests/test_i3baroutput.py @@ -10,10 +10,12 @@ except ImportError: from bumblebee.output import I3BarOutput from tests.util import MockWidget +from tests.util import MockTheme class TestI3BarOutput(unittest.TestCase): def setUp(self): - self.output = I3BarOutput() + self.theme = MockTheme() + self.output = I3BarOutput(self.theme) self.expectedStart = json.dumps({"version": 1, "click_events": True}) + "[\n" self.expectedStop = "]\n" self.someWidget = MockWidget("foo bar baz") @@ -46,5 +48,32 @@ class TestI3BarOutput(unittest.TestCase): self.output.flush() self.assertEquals(",\n", stdout.getvalue()) + @mock.patch("sys.stdout", new_callable=StringIO) + def test_prefix(self, stdout): + self.theme.set_prefix(" - ") + self.output.draw(self.someWidget) + result = json.loads(stdout.getvalue())[0] + self.assertEquals(result["full_text"], "{}{}".format( + self.theme.prefix(), self.someWidget.full_text()) + ) + + @mock.patch("sys.stdout", new_callable=StringIO) + def test_suffix(self, stdout): + self.theme.set_suffix(" - ") + self.output.draw(self.someWidget) + result = json.loads(stdout.getvalue())[0] + self.assertEquals(result["full_text"], "{}{}".format( + self.someWidget.full_text(), self.theme.suffix()) + ) + + @mock.patch("sys.stdout", new_callable=StringIO) + def test_bothfix(self, stdout): + self.theme.set_suffix(" - ") + self.theme.set_prefix(" * ") + 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()) + ) # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py index e3bef15..aa67cdc 100644 --- a/tests/util.py +++ b/tests/util.py @@ -29,4 +29,21 @@ class MockWidget(object): def full_text(self): return self._text +class MockTheme(object): + def __init__(self): + self._prefix = None + self._suffix = None + + def set_prefix(self, value): + self._prefix = value + + def set_suffix(self, value): + self._suffix = value + + def prefix(self): + return self._prefix + + def suffix(self): + return self._suffix + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4