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-10 11:25:02 +01:00
|
|
|
import bumblebee.store
|
|
|
|
|
2017-04-22 13:07:50 +02:00
|
|
|
def scrollable(func):
|
|
|
|
def wrapper(module, widget):
|
|
|
|
text = func(module, widget)
|
2017-08-12 17:03:04 +02:00
|
|
|
if not text: return text
|
2017-04-22 13:07:50 +02:00
|
|
|
width = widget.get("theme.width", module.parameter("width", 30))
|
|
|
|
widget.set("theme.minwidth", "A"*width)
|
|
|
|
if len(text) <= width:
|
|
|
|
return text
|
|
|
|
# we need to shorten
|
2018-12-28 22:27:38 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
bounce = int(module.parameter("scrolling.bounce", 1))
|
|
|
|
except ValueError:
|
|
|
|
bounce = 1
|
|
|
|
try:
|
|
|
|
scroll_speed = int(module.parameter("scrolling.speed", 1))
|
|
|
|
except ValueError:
|
|
|
|
scroll_speed = 1
|
2017-04-22 13:07:50 +02:00
|
|
|
start = widget.get("scrolling.start", -1)
|
|
|
|
direction = widget.get("scrolling.direction", "right")
|
2018-12-28 22:27:38 +01:00
|
|
|
start += scroll_speed if direction == "right" else -(scroll_speed)
|
|
|
|
|
|
|
|
if width + start > len(text) + (scroll_speed -1):
|
|
|
|
if bounce:
|
|
|
|
widget.set("scrolling.direction", "left")
|
|
|
|
else:
|
|
|
|
start = 0
|
|
|
|
elif start <= 0:
|
|
|
|
if bounce:
|
|
|
|
widget.set("scrolling.direction", "right")
|
|
|
|
else:
|
|
|
|
start = len(text)
|
2017-04-22 13:07:50 +02:00
|
|
|
widget.set("scrolling.start", start)
|
|
|
|
text = text[start:width+start]
|
|
|
|
|
|
|
|
return text
|
|
|
|
return wrapper
|
|
|
|
|
2016-12-10 11:25:02 +01:00
|
|
|
class Widget(bumblebee.store.Store):
|
2016-12-04 17:45:42 +01:00
|
|
|
"""Represents a single visible block in the status bar"""
|
2016-12-10 07:47:24 +01:00
|
|
|
def __init__(self, full_text="", name=""):
|
2016-12-10 11:25:02 +01:00
|
|
|
super(Widget, self).__init__()
|
2016-12-04 17:45:42 +01:00
|
|
|
self._full_text = full_text
|
2016-12-09 11:49:59 +01:00
|
|
|
self.module = None
|
2016-12-10 08:09:13 +01:00
|
|
|
self._module = None
|
2018-09-15 14:40:52 +02:00
|
|
|
self._minimized = False
|
2016-12-10 07:47:24 +01:00
|
|
|
self.name = name
|
2016-12-09 19:29:16 +01:00
|
|
|
self.id = str(uuid.uuid4())
|
2016-12-08 12:44:52 +01:00
|
|
|
|
2017-08-12 17:03:04 +02:00
|
|
|
def get_module(self):
|
|
|
|
return self._module
|
|
|
|
|
2018-09-15 14:40:52 +02:00
|
|
|
def toggle_minimize(self):
|
|
|
|
self._minimized = not self._minimized
|
|
|
|
|
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-10 08:09:13 +01:00
|
|
|
self._module = module
|
2016-12-04 17:45:42 +01:00
|
|
|
|
2017-05-10 20:01:29 +02:00
|
|
|
def cls(self):
|
|
|
|
if not self._module:
|
|
|
|
return None
|
|
|
|
return self._module.__module__.replace("bumblebee.modules.", "")
|
|
|
|
|
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-10 08:09:13 +01:00
|
|
|
if self._module and hasattr(self._module, "state"):
|
2016-12-10 11:25:02 +01:00
|
|
|
states = self._module.state(self)
|
|
|
|
if not isinstance(states, list):
|
|
|
|
return [states]
|
|
|
|
return states
|
|
|
|
return []
|
2016-12-09 13:32:22 +01:00
|
|
|
|
2016-12-10 11:25:02 +01:00
|
|
|
def full_text(self, value=None):
|
|
|
|
"""Set or retrieve the full text to display in the widget"""
|
|
|
|
if value:
|
|
|
|
self._full_text = value
|
2016-12-08 08:44:54 +01:00
|
|
|
else:
|
2018-09-15 14:40:52 +02:00
|
|
|
if self._minimized:
|
|
|
|
return u"\u2026"
|
2016-12-10 11:25:02 +01:00
|
|
|
if callable(self._full_text):
|
2016-12-11 11:37:24 +01:00
|
|
|
return self._full_text(self)
|
2016-12-10 11:25:02 +01:00
|
|
|
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"""
|
2018-09-22 14:40:32 +02:00
|
|
|
def __init__(self, theme, config=None):
|
2016-12-08 11:31:20 +01:00
|
|
|
self._theme = theme
|
2016-12-08 12:44:52 +01:00
|
|
|
self._widgets = []
|
2017-04-02 08:31:23 +02:00
|
|
|
self._started = False
|
2018-09-22 14:40:32 +02:00
|
|
|
self._config = config
|
2017-04-02 08:31:23 +02:00
|
|
|
|
2019-01-19 14:07:17 +01:00
|
|
|
def replace(self, event, module, new_widget):
|
2019-01-19 14:12:32 +01:00
|
|
|
data = self.widget_data(module, new_widget)
|
|
|
|
self._widgets = [w if not "instance" in w or w["instance"] != event["instance"] else data for w in self._widgets]
|
2019-01-19 14:07:17 +01:00
|
|
|
|
2017-04-02 08:31:23 +02:00
|
|
|
def started(self):
|
|
|
|
return self._started
|
2016-12-04 12:26:20 +01:00
|
|
|
|
|
|
|
def start(self):
|
|
|
|
"""Print start preamble for i3bar protocol"""
|
2017-04-02 08:31:23 +02:00
|
|
|
self._started = True
|
2017-04-26 07:37:47 +02:00
|
|
|
sys.stdout.write(json.dumps({"version": 1, "click_events": True}) + "\n[\n")
|
2016-12-04 12:26:20 +01:00
|
|
|
|
|
|
|
def stop(self):
|
|
|
|
"""Finish i3bar protocol"""
|
|
|
|
sys.stdout.write("]\n")
|
|
|
|
|
2019-01-19 14:12:32 +01:00
|
|
|
def widget_data(self, module, 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)
|
2017-04-22 13:07:50 +02:00
|
|
|
width = self._theme.minwidth(widget)
|
2018-11-25 17:50:05 +01:00
|
|
|
|
|
|
|
if width:
|
|
|
|
full_text = full_text.ljust(len(width) + len(prefix) + len(suffix))
|
2019-01-19 14:12:32 +01:00
|
|
|
return {
|
2018-01-11 20:31:48 +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,
|
2018-11-25 17:50:05 +01:00
|
|
|
"min_width": None,
|
2017-04-22 08:24:52 +02:00
|
|
|
"align": self._theme.align(widget),
|
2016-12-09 19:29:16 +01:00
|
|
|
"instance": widget.id,
|
|
|
|
"name": module.id,
|
2019-01-19 14:12:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
def draw(self, widget, module=None, engine=None):
|
|
|
|
"""Draw a single widget"""
|
|
|
|
if widget.get_module() and widget.get_module().hidden():
|
|
|
|
return
|
2019-01-19 14:56:25 +01:00
|
|
|
if widget.get_module() and widget.get_module().name in self._config.autohide():
|
|
|
|
if not any(state in widget.state() for state in ["warning", "critical"]):
|
|
|
|
return
|
2019-01-19 14:12:32 +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),
|
|
|
|
})
|
|
|
|
|
|
|
|
self._widgets.append(self.widget_data(module, widget))
|
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"""
|
2018-09-22 14:40:32 +02:00
|
|
|
widgets = self._widgets
|
|
|
|
if self._config and self._config.reverse():
|
|
|
|
widgets = list(reversed(widgets))
|
|
|
|
sys.stdout.write(json.dumps(widgets))
|
2016-12-08 12:44:52 +01:00
|
|
|
|
|
|
|
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
|