From 712d958e18b9f228b83231e050c130153157ad58 Mon Sep 17 00:00:00 2001 From: Tobi-wan Kenobi Date: Sun, 4 Dec 2016 12:53:18 +0100 Subject: [PATCH] [core/output] Add widget drawing Add basic drawing of widgets. Each module instance returns a list of widgets using the widgets() method which is then forwarded to the draw() method of the configured output. see #23 --- bumblebee/engine.py | 5 ++++- bumblebee/output.py | 11 +++++++++++ tests/__init__.py | 0 tests/test_i3baroutput.py | 16 ++++++++++++++++ tests/util.py | 10 ++++++++++ 5 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 tests/__init__.py create mode 100644 tests/util.py diff --git a/bumblebee/engine.py b/bumblebee/engine.py index 0213062..746556b 100644 --- a/bumblebee/engine.py +++ b/bumblebee/engine.py @@ -48,7 +48,10 @@ class Engine(object): """Start the event loop""" self._output.start() while self.running(): - pass + widgets = [] + for module in self._modules: + widgets += module.widgets() + self._output.draw(widgets) self._output.stop() diff --git a/bumblebee/output.py b/bumblebee/output.py index d09f63a..3c08671 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -18,4 +18,15 @@ class I3BarOutput(object): """Finish i3bar protocol""" sys.stdout.write("]\n") + def draw(self, widgets): + """Draw a number of widgets""" + if not isinstance(widgets, list): + widgets = [widgets] + result = [] + for widget in widgets: + result.append({ + u"full_text": widget.text() + }) + sys.stdout.write(json.dumps(result)) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/test_i3baroutput.py b/tests/test_i3baroutput.py index 9fff324..0ab88b8 100644 --- a/tests/test_i3baroutput.py +++ b/tests/test_i3baroutput.py @@ -1,4 +1,5 @@ # pylint: disable=C0103,C0111 + import json import unittest import mock @@ -8,12 +9,14 @@ except ImportError: from io import StringIO from bumblebee.output import I3BarOutput +from tests.util import MockWidget class TestI3BarOutput(unittest.TestCase): def setUp(self): self.output = I3BarOutput() self.expectedStart = json.dumps({"version": 1, "click_events": True}) + "[\n" self.expectedStop = "]\n" + self.someWidget = MockWidget("foo bar baz") @mock.patch("sys.stdout", new_callable=StringIO) def test_start(self, stdout): @@ -25,4 +28,17 @@ class TestI3BarOutput(unittest.TestCase): self.output.stop() self.assertEquals(self.expectedStop, stdout.getvalue()) + @mock.patch("sys.stdout", new_callable=StringIO) + def test_draw_single_widget(self, stdout): + self.output.draw(self.someWidget) + result = json.loads(stdout.getvalue())[0] + self.assertEquals(result["full_text"], self.someWidget.text()) + + @mock.patch("sys.stdout", new_callable=StringIO) + def test_draw_multiple_widgets(self, stdout): + self.output.draw([self.someWidget, self.someWidget]) + result = json.loads(stdout.getvalue()) + for res in result: + self.assertEquals(res["full_text"], self.someWidget.text()) + # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 diff --git a/tests/util.py b/tests/util.py new file mode 100644 index 0000000..202da49 --- /dev/null +++ b/tests/util.py @@ -0,0 +1,10 @@ +# pylint: disable=C0103,C0111 + +class MockWidget(object): + def __init__(self, text): + self._text = text + + def text(self): + return self._text + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4