[core/output] extract I3BarOutput.draw() into a class

This commit is contained in:
me 2020-01-31 11:16:26 +02:00
parent 11f16bd1aa
commit f5f09bdb1b

View file

@ -321,33 +321,26 @@ class Widget(bumblebee.store.Store):
else: else:
return self._full_text return self._full_text
class I3BarOutput(object):
"""Manage output according to the i3bar protocol""" class WidgetDrawer(object):
"""
Wrapper for I3BarOutput.draw(),
because that function is getting too big
"""
def __init__(self, theme, config=None): def __init__(self, theme, config=None):
"""
Keep the same signature as I3BarOutput.__init__()
"""
self._theme = theme self._theme = theme
self._widgets = []
self._started = False
self._config = config self._config = config
self._widgets = []
def started(self):
return self._started
def start(self):
"""Print start preamble for i3bar protocol"""
self._started = True
sys.stdout.write(json.dumps({"version": 1, "click_events": True}) + "\n[\n")
def stop(self):
"""Finish i3bar protocol"""
sys.stdout.write("]\n")
def draw(self, widget, module=None, engine=None): def draw(self, widget, module=None, engine=None):
""" """
Draw a single widget Keep the same argument signature as I3BarOutput.draw()
Return: list
Note: technically, this method doesn't draw anything. It only adds list[0] - optional if the theme has a separator
blocks of JSON text to self._widgets: one for separator, if the list[1] - JSON text for the widget
theme contains a separator and one for the widget itself
""" """
full_text = widget.full_text() full_text = widget.full_text()
if widget.get_module() and widget.get_module().hidden(): if widget.get_module() and widget.get_module().hidden():
@ -407,6 +400,39 @@ class I3BarOutput(object):
"name": module.id, "name": module.id,
"markup": markup, "markup": markup,
}) })
return self._widgets
class I3BarOutput(object):
"""Manage output according to the i3bar protocol"""
def __init__(self, theme, config=None):
self._theme = theme
self._widgets = []
self._started = False
self._config = config
def started(self):
return self._started
def start(self):
"""Print start preamble for i3bar protocol"""
self._started = True
sys.stdout.write(json.dumps({"version": 1, "click_events": True}) + "\n[\n")
def stop(self):
"""Finish i3bar protocol"""
sys.stdout.write("]\n")
def draw(self, widget, module=None, engine=None):
"""
Draw a single widget
Note: technically, this method doesn't draw anything. It only adds
blocks of JSON text to self._widgets: one for separator, if the
theme contains a separator and one for the widget itself
"""
widget_drawer = WidgetDrawer(self._theme, self._config)
self._widgets.extend(widget_drawer.draw(widget, module, engine))
def begin(self): def begin(self):
"""Start one output iteration""" """Start one output iteration"""