2016-12-04 12:26:20 +01:00
|
|
|
# pylint: disable=R0201
|
|
|
|
|
|
|
|
"""Output classes"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import json
|
|
|
|
|
2016-12-04 17:45:42 +01:00
|
|
|
class Widget(object):
|
|
|
|
"""Represents a single visible block in the status bar"""
|
|
|
|
def __init__(self, full_text):
|
|
|
|
self._full_text = full_text
|
|
|
|
|
|
|
|
def full_text(self):
|
|
|
|
"""Retrieve the full text to display in the widget"""
|
2016-12-08 08:44:54 +01:00
|
|
|
if callable(self._full_text):
|
|
|
|
return self._full_text()
|
|
|
|
else:
|
|
|
|
return self._full_text
|
2016-12-04 17:45:42 +01:00
|
|
|
|
2016-12-04 12:26:20 +01:00
|
|
|
class I3BarOutput(object):
|
|
|
|
"""Manage output according to the i3bar protocol"""
|
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Print start preamble for i3bar protocol"""
|
|
|
|
sys.stdout.write(json.dumps({"version": 1, "click_events": True}) + "[\n")
|
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""Finish i3bar protocol"""
|
|
|
|
sys.stdout.write("]\n")
|
|
|
|
|
2016-12-08 09:04:47 +01:00
|
|
|
def draw_widget(self, result, widget):
|
|
|
|
"""Draw a single widget"""
|
|
|
|
result.append({
|
|
|
|
u"full_text": widget.full_text()
|
|
|
|
})
|
|
|
|
|
2016-12-04 17:45:42 +01:00
|
|
|
def draw(self, widgets, engine=None):
|
2016-12-04 12:53:18 +01:00
|
|
|
"""Draw a number of widgets"""
|
|
|
|
if not isinstance(widgets, list):
|
|
|
|
widgets = [widgets]
|
|
|
|
result = []
|
|
|
|
for widget in widgets:
|
2016-12-08 09:04:47 +01:00
|
|
|
self.draw_widget(result, widget)
|
2016-12-04 12:53:18 +01:00
|
|
|
sys.stdout.write(json.dumps(result))
|
|
|
|
|
2016-12-04 16:14:43 +01:00
|
|
|
def flush(self):
|
|
|
|
"""Flushes output"""
|
|
|
|
sys.stdout.write(",\n")
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2016-12-04 12:26:20 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|