[core/input] Separate module/widget update and retrieval

To make it easier to update individual modules, separate the call to
update() and the call to actually drawing the status.

Additionally, this avoids the "side effect" of updating when drawing the
status line.
This commit is contained in:
Tobias Witek 2020-02-09 13:25:34 +01:00
parent 5810a12944
commit 4e2a645bd3
3 changed files with 15 additions and 14 deletions

View file

@ -31,7 +31,8 @@ def handle_input(output):
modules[event['name']] = True
except ValueError:
pass
output.draw('patch', modules.keys())
output.update(modules.keys())
output.draw('statusline')
poll.unregister(sys.stdin.fileno())
@ -55,8 +56,7 @@ def main():
output.modules(modules)
output.draw('start')
while True:
for module in modules:
module.update()
output.update()
output.draw('statusline')
output.wait(config.interval())
output.draw('stop')

View file

@ -5,7 +5,7 @@ import time
class i3(object):
def __init__(self):
self._modules = []
self._status = []
self._status = {}
def modules(self, modules=None):
if not modules:
@ -31,20 +31,23 @@ class i3(object):
def stop(self):
return { 'suffix': '\n]' }
def patch(self, affected_modules):
pass # TODO
def statusline(self):
self._status = []
def update(self, affected_modules=None):
for module in self._modules:
module.update()
self._status[module] = []
for widget in module.widgets():
self._status.append({
self._status[module].append({
'full_text': widget.full_text(),
'instance': widget.id(),
'name': module.id(),
})
def statusline(self):
widgets = []
for module in self._modules:
widgets += self._status[module]
return {
'data': self._status,
'data': widgets,
'suffix': ','
}

View file

@ -14,9 +14,6 @@ class i3(unittest.TestCase):
widget.full_text.return_value = "test"
self.someModule = TestModule(widgets=[widget, widget, widget])
def tearDown(self):
pass
def test_start(self):
all_data = self.i3.start()
data = all_data['data']
@ -52,6 +49,7 @@ class i3(unittest.TestCase):
def test_statusline(self):
self.i3.modules([ self.someModule, self.someModule, self.someModule ])
self.i3.update()
data = self.i3.statusline()
self.assertEqual(len(self.someModule.widgets())*3, len(data['data']), 'wrong number of widgets')