[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
This commit is contained in:
Tobi-wan Kenobi 2016-12-04 12:53:18 +01:00
parent 6f52825ef0
commit 712d958e18
5 changed files with 41 additions and 1 deletions

View file

@ -48,7 +48,10 @@ class Engine(object):
"""Start the event loop""" """Start the event loop"""
self._output.start() self._output.start()
while self.running(): while self.running():
pass widgets = []
for module in self._modules:
widgets += module.widgets()
self._output.draw(widgets)
self._output.stop() self._output.stop()

View file

@ -18,4 +18,15 @@ class I3BarOutput(object):
"""Finish i3bar protocol""" """Finish i3bar protocol"""
sys.stdout.write("]\n") 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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

0
tests/__init__.py Normal file
View file

View file

@ -1,4 +1,5 @@
# pylint: disable=C0103,C0111 # pylint: disable=C0103,C0111
import json import json
import unittest import unittest
import mock import mock
@ -8,12 +9,14 @@ except ImportError:
from io import StringIO from io import StringIO
from bumblebee.output import I3BarOutput from bumblebee.output import I3BarOutput
from tests.util import MockWidget
class TestI3BarOutput(unittest.TestCase): class TestI3BarOutput(unittest.TestCase):
def setUp(self): def setUp(self):
self.output = I3BarOutput() self.output = I3BarOutput()
self.expectedStart = json.dumps({"version": 1, "click_events": True}) + "[\n" self.expectedStart = json.dumps({"version": 1, "click_events": True}) + "[\n"
self.expectedStop = "]\n" self.expectedStop = "]\n"
self.someWidget = MockWidget("foo bar baz")
@mock.patch("sys.stdout", new_callable=StringIO) @mock.patch("sys.stdout", new_callable=StringIO)
def test_start(self, stdout): def test_start(self, stdout):
@ -25,4 +28,17 @@ class TestI3BarOutput(unittest.TestCase):
self.output.stop() self.output.stop()
self.assertEquals(self.expectedStop, stdout.getvalue()) 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 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

10
tests/util.py Normal file
View file

@ -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