2016-12-04 12:26:20 +01:00
|
|
|
# pylint: disable=R0201
|
|
|
|
|
|
|
|
"""Output classes"""
|
|
|
|
|
|
|
|
import sys
|
|
|
|
import json
|
2016-12-09 19:29:16 +01:00
|
|
|
import uuid
|
2016-12-04 12:26:20 +01:00
|
|
|
|
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
|
2016-12-09 11:49:59 +01:00
|
|
|
self.module = None
|
2016-12-09 19:29:16 +01:00
|
|
|
self.id = str(uuid.uuid4())
|
2016-12-08 12:44:52 +01:00
|
|
|
|
2016-12-09 11:49:59 +01:00
|
|
|
def link_module(self, module):
|
2016-12-09 07:11:23 +01:00
|
|
|
"""Set the module that spawned this widget
|
|
|
|
|
|
|
|
This is done outside the constructor to avoid having to
|
|
|
|
pass in the module name in every concrete module implementation"""
|
2016-12-09 11:49:59 +01:00
|
|
|
self.module = module.name
|
2016-12-04 17:45:42 +01:00
|
|
|
|
2016-12-09 13:32:22 +01:00
|
|
|
def state(self):
|
2016-12-09 16:33:29 +01:00
|
|
|
"""Return the widget's state"""
|
2016-12-09 13:32:22 +01:00
|
|
|
return "state-default"
|
|
|
|
|
2016-12-04 17:45:42 +01:00
|
|
|
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"""
|
2016-12-08 11:31:20 +01:00
|
|
|
def __init__(self, theme):
|
|
|
|
self._theme = theme
|
2016-12-08 12:44:52 +01:00
|
|
|
self._widgets = []
|
2016-12-04 12:26:20 +01:00
|
|
|
|
|
|
|
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-09 19:29:16 +01:00
|
|
|
def draw(self, widget, module=None, engine=None):
|
2016-12-08 09:04:47 +01:00
|
|
|
"""Draw a single widget"""
|
2016-12-08 11:31:20 +01:00
|
|
|
full_text = widget.full_text()
|
2016-12-09 13:06:08 +01:00
|
|
|
padding = self._theme.padding(widget)
|
|
|
|
prefix = self._theme.prefix(widget, padding)
|
|
|
|
suffix = self._theme.suffix(widget, padding)
|
2016-12-08 11:52:47 +01:00
|
|
|
if prefix:
|
2016-12-08 12:44:52 +01:00
|
|
|
full_text = u"{}{}".format(prefix, full_text)
|
2016-12-08 11:52:47 +01:00
|
|
|
if suffix:
|
2016-12-08 12:44:52 +01:00
|
|
|
full_text = u"{}{}".format(full_text, suffix)
|
2016-12-09 12:55:16 +01:00
|
|
|
separator = self._theme.separator(widget)
|
|
|
|
if separator:
|
|
|
|
self._widgets.append({
|
|
|
|
u"full_text": separator,
|
|
|
|
"separator": False,
|
|
|
|
"color": self._theme.separator_fg(widget),
|
|
|
|
"background": self._theme.separator_bg(widget),
|
|
|
|
"separator_block_width": self._theme.separator_block_width(widget),
|
|
|
|
})
|
2016-12-08 12:44:52 +01:00
|
|
|
self._widgets.append({
|
2016-12-09 12:55:16 +01:00
|
|
|
u"full_text": full_text,
|
2016-12-09 08:58:45 +01:00
|
|
|
"color": self._theme.fg(widget),
|
|
|
|
"background": self._theme.bg(widget),
|
2016-12-09 12:55:16 +01:00
|
|
|
"separator_block_width": self._theme.separator_block_width(widget),
|
|
|
|
"separator": True if separator is None else False,
|
2016-12-09 19:29:16 +01:00
|
|
|
"instance": widget.id,
|
|
|
|
"name": module.id,
|
2016-12-08 09:04:47 +01:00
|
|
|
})
|
|
|
|
|
2016-12-08 12:44:52 +01:00
|
|
|
def begin(self):
|
|
|
|
"""Start one output iteration"""
|
|
|
|
self._widgets = []
|
2016-12-09 12:28:39 +01:00
|
|
|
self._theme.reset()
|
2016-12-04 12:53:18 +01:00
|
|
|
|
2016-12-04 16:14:43 +01:00
|
|
|
def flush(self):
|
|
|
|
"""Flushes output"""
|
2016-12-08 12:44:52 +01:00
|
|
|
sys.stdout.write(json.dumps(self._widgets))
|
|
|
|
|
|
|
|
def end(self):
|
|
|
|
"""Finalizes output"""
|
2016-12-04 16:14:43 +01:00
|
|
|
sys.stdout.write(",\n")
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
2016-12-04 12:26:20 +01:00
|
|
|
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|