[core/themes] Add module-specific themes

Allow module-specific theme information to overload "default"
configuration. I.e. it is now possible to have specific prefix or
postfix configurations for different modules. The module name is derived
for each widget from the module (__module__) from which it was
instantiated.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-08 12:44:52 +01:00
parent 562fd85ca2
commit 2fa8d7b778
6 changed files with 56 additions and 20 deletions

View file

@ -13,6 +13,7 @@ class Module(object):
this base class.
"""
def __init__(self, engine, widgets):
self.name = self.__module__.split(".")[-1]
self._widgets = []
if widgets:
self._widgets = widgets if isinstance(widgets, list) else [widgets]
@ -59,10 +60,14 @@ class Engine(object):
"""Start the event loop"""
self._output.start()
while self.running():
self._output.begin()
for module in self._modules:
module.update(module.widgets())
self._output.draw(widgets=module.widgets(), engine=self)
for widget in module.widgets():
widget.set_module(module)
self._output.draw(widget=widget, engine=self)
self._output.flush()
self._output.end()
if self.running():
time.sleep(1)

View file

@ -9,6 +9,13 @@ class Widget(object):
"""Represents a single visible block in the status bar"""
def __init__(self, full_text):
self._full_text = full_text
self._module = None
def set_module(self, module):
self._module = module.name
def module(self):
return self._module
def full_text(self):
"""Retrieve the full text to display in the widget"""
@ -21,6 +28,7 @@ class I3BarOutput(object):
"""Manage output according to the i3bar protocol"""
def __init__(self, theme):
self._theme = theme
self._widgets = []
def start(self):
"""Print start preamble for i3bar protocol"""
@ -30,30 +38,29 @@ class I3BarOutput(object):
"""Finish i3bar protocol"""
sys.stdout.write("]\n")
def draw_widget(self, result, widget):
def draw(self, widget, engine=None):
"""Draw a single widget"""
full_text = widget.full_text()
prefix = self._theme.prefix(widget)
suffix = self._theme.suffix(widget)
if prefix:
full_text = "{}{}".format(prefix, full_text)
full_text = u"{}{}".format(prefix, full_text)
if suffix:
full_text = "{}{}".format(full_text, suffix)
result.append({
u"full_text": "{}".format(full_text)
full_text = u"{}{}".format(full_text, suffix)
self._widgets.append({
u"full_text": u"{}".format(full_text)
})
def draw(self, widgets, engine=None):
"""Draw a number of widgets"""
if not isinstance(widgets, list):
widgets = [widgets]
result = []
for widget in widgets:
self.draw_widget(result, widget)
sys.stdout.write(json.dumps(result))
def begin(self):
"""Start one output iteration"""
self._widgets = []
def flush(self):
"""Flushes output"""
sys.stdout.write(json.dumps(self._widgets))
def end(self):
"""Finalizes output"""
sys.stdout.write(",\n")
sys.stdout.flush()

View file

@ -55,9 +55,16 @@ class Theme(object):
module_theme = self._theme.get(widget.module(), {})
padding = None
if name != "padding":
padding = self._get(widget, "padding")
value = self._defaults.get(name, default)
value = module_theme.get(name, value)
if value and padding:
value = u"{}{}{}".format(padding, value, padding)
return value
# algorithm copied from