From 445c5a65f14f2131d0a124961c53a8d974ed949d Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 2 Feb 2020 14:18:13 +0100 Subject: [PATCH] [core/output] Move widget/module handling inside output The core.output module now manages the list of modules and retrieves the widgets inside draw() itself. That way, details of drawing/updating widgets are not visible from the outside anymore. --- bumblebee-status | 2 +- core/output.py | 20 +++++++++++++------- doc/NOTES.md | 2 ++ tests/core/test_output.py | 39 ++++++++++++++++++++++++++++++++++++--- 4 files changed, 52 insertions(+), 11 deletions(-) diff --git a/bumblebee-status b/bumblebee-status index e461758..29e7876 100755 --- a/bumblebee-status +++ b/bumblebee-status @@ -11,12 +11,12 @@ def main(): modules = [] for module in config.modules(): modules.append(core.module.load(module)) + output.modules(modules) output.draw('start') while True: output.clear() for module in modules: module.update() - output.append(module) output.draw('statusline') output.wait(config.interval()) output.draw('stop') diff --git a/core/output.py b/core/output.py index 9a6b898..f10b486 100644 --- a/core/output.py +++ b/core/output.py @@ -4,8 +4,14 @@ import time class i3(object): def __init__(self): + self._modules = [] self.clear() + def modules(self, modules=None): + if not modules: + return self._modules + self._modules = modules if isinstance(modules, list) else [ modules ] + def draw(self, what): data = getattr(self, what)() if 'data' in data: @@ -26,15 +32,15 @@ class i3(object): def clear(self): self._statusline = [] - def append(self, module): - for widget in module.widgets(): - self._statusline.append({ - 'full_text': widget.full_text() - }) - def statusline(self): + status = [] + for module in self._modules: + for widget in module.widgets(): + status.append({ + 'full_text': widget.full_text() + }) return { - 'data': self._statusline, + 'data': status, 'suffix': ',' } diff --git a/doc/NOTES.md b/doc/NOTES.md index 5ae90e5..f437dae 100644 --- a/doc/NOTES.md +++ b/doc/NOTES.md @@ -16,3 +16,5 @@ ## Improvements - pango output (improve - maybe autodetect? see #531) +- only update specific, affected modules when clicking +- allow handlers to specify whether to update or not (e.g. scroll) diff --git a/tests/core/test_output.py b/tests/core/test_output.py index e32839a..c751ed9 100644 --- a/tests/core/test_output.py +++ b/tests/core/test_output.py @@ -1,11 +1,18 @@ -import unittest import json +import unittest import core.output +import core.module + +class TestModule(core.module.Module): + pass class i3(unittest.TestCase): def setUp(self): self.i3 = core.output.i3() + widget = unittest.mock.MagicMock() + widget.full_text.return_value = "test" + self.someModule = TestModule([widget, widget, widget]) def tearDown(self): pass @@ -18,8 +25,34 @@ class i3(unittest.TestCase): self.assertEqual('\n[', all_data['suffix']) def test_stop(self): - self.assertEqual('\n]', self.i3.stop()['suffix']) + self.assertEqual('\n]', self.i3.stop()['suffix'], 'wrong i3bar protocol during stop') - # TODO: mock a "draw" call + def test_no_modules_by_default(self): + self.assertEqual(0, len(self.i3.modules()), 'module list should be empty by default') + + def test_register_single_module(self): + self.i3.modules(self.someModule) + self.assertEqual(1, len(self.i3.modules()), 'setting single module does not work') + + def test_register_multiple_modules(self): + self.i3.modules([ self.someModule, self.someModule, self.someModule ]) + self.assertEqual(3, len(self.i3.modules()), 'setting module list does not work') + + def test_draw_existing_module(self): + self.i3.test_draw = unittest.mock.MagicMock(return_value={ + 'data': { 'test': True }, 'suffix': 'end' + }) + self.i3.draw('test_draw') + self.i3.test_draw.assert_called_once_with() + + def test_empty_status_line(self): + data = self.i3.statusline() + self.assertEqual([], data['data'], 'expected empty list of status line entries') + self.assertEqual(',', data['suffix'], 'expected "," as suffix') + + def test_statusline(self): + self.i3.modules([ self.someModule, self.someModule, self.someModule ]) + data = self.i3.statusline() + self.assertEqual(len(self.someModule.widgets())*3, len(data['data']), 'wrong number of widgets') # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4