From 2a35905b89f9ec12e46d846823d1bd22809834cf Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Mon, 31 Oct 2016 10:45:15 +0100 Subject: [PATCH] [themes] Add "cycle" theme capability It is now possible to add a list of theme configurations in the "default" section called "cycle". These configuration items will be cycled through module by module. to create "alternate style" effects. This is *only* possible in the "default" configuration part, but any module-specific configurations still take precedence. Also, removed the capability of per-widget themes. That simply complicates things and probably doesn't really bring any benefits. --- bumblebee/module.py | 7 ++----- bumblebee/modules/battery.py | 4 ++-- bumblebee/modules/time.py | 4 ++-- bumblebee/output.py | 6 ++++++ bumblebee/outputs/i3.py | 10 +++++----- bumblebee/theme.py | 27 ++++++++++++++++++++++++++- bumblebee/themes/default.json | 10 +++++++++- i3bumblebee | 8 +++++--- 8 files changed, 57 insertions(+), 19 deletions(-) diff --git a/bumblebee/module.py b/bumblebee/module.py index e848ea4..4b3811b 100644 --- a/bumblebee/module.py +++ b/bumblebee/module.py @@ -1,10 +1,7 @@ class Module(object): - def __init__(self, theme, args): - self._theme = theme - - def theme(self): - return self._theme + def __init__(self, args): + pass def data(self): pass diff --git a/bumblebee/modules/battery.py b/bumblebee/modules/battery.py index d4367f8..abe0a68 100644 --- a/bumblebee/modules/battery.py +++ b/bumblebee/modules/battery.py @@ -2,8 +2,8 @@ import datetime import bumblebee.module class Module(bumblebee.module.Module): - def __init__(self, theme, args): - super(Module, self).__init__(theme, args) + def __init__(self, args): + super(Module, self).__init__(args) self._battery = "BAT0" if not args else args[0] self._capacity = 0 self._status = "Unknown" diff --git a/bumblebee/modules/time.py b/bumblebee/modules/time.py index 2259823..0fec842 100644 --- a/bumblebee/modules/time.py +++ b/bumblebee/modules/time.py @@ -2,8 +2,8 @@ import datetime import bumblebee.module class Module(bumblebee.module.Module): - def __init__(self, theme, args): - super(Module, self).__init__(theme, args) + def __init__(self, args): + super(Module, self).__init__(args) def data(self): return datetime.datetime.now().strftime("%x %X") diff --git a/bumblebee/output.py b/bumblebee/output.py index fa2e042..e0d9348 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -1,5 +1,11 @@ class Output(object): + def __init__(self, theme): + self._theme = theme + + def theme(self): + return self._theme + def start(self): pass diff --git a/bumblebee/outputs/i3.py b/bumblebee/outputs/i3.py index 329e6d7..eec156e 100644 --- a/bumblebee/outputs/i3.py +++ b/bumblebee/outputs/i3.py @@ -4,15 +4,16 @@ import json import bumblebee.output class i3bar(bumblebee.output.Output): - def __init__(self): + def __init__(self, theme): + + super(i3bar, self).__init__(theme) self._data = [] - self._previous_background = None def start(self): return json.dumps({ "version": 1 }) + "[" def add(self, obj): - theme = obj.theme() + theme = self.theme() data = { u"full_text": "{}{}{}".format(theme.prefix(obj), obj.data(), theme.suffix(obj)), @@ -26,13 +27,12 @@ class i3bar(bumblebee.output.Output): self._data.append({ u"full_text": theme.separator(obj), "color": theme.background(obj), - "background": self._previous_background, + "background": theme.previous_background(), "separator": False, "separator_block_width": 0, }) self._data.append(data) - self._previous_background = theme.background(obj) def get(self): data = json.dumps(self._data) diff --git a/bumblebee/theme.py b/bumblebee/theme.py index dea942d..aad919d 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -1,13 +1,20 @@ import os import json + + class Theme: + _cycle_index = 0 + _cycle = None def __init__(self, name="default"): self._data = None path = os.path.dirname(os.path.realpath(__file__)) with open("{}/themes/{}.json".format(path, name)) as f: self._data = json.load(f) self._defaults = self._data.get("defaults", {}) + self._cycle = self._defaults.get("cycle", []) + + self.reset() def _gettheme(self, obj, key): module = obj.__module__.split(".")[-1] @@ -17,6 +24,9 @@ class Theme: value = self._defaults.get(key, value) value = module_theme.get(key, value) + if len(self._cycle) > 0: + value = self._defaults["cycle"][self._cycle_index].get(key, value) + if hasattr(obj, "state"): state = getattr(obj, "state")() state_theme = module_theme.get("states", {}).get(state, {}) @@ -25,11 +35,26 @@ class Theme: return value + def reset(self): + self._cycle_index = 0 + self._previous_background = None + self._background = None + + def next(self): + self._cycle_index += 1 + self._previous_background = self._background + if self._cycle_index >= len(self._cycle): + self._cycle_index = 0 + def color(self, obj): return self._gettheme(obj, "fg") def background(self, obj): - return self._gettheme(obj, "bg") + self._background = self._gettheme(obj, "bg") + return self._background + + def previous_background(self): + return self._previous_background def separator(self, obj): return self._gettheme(obj, "separator") diff --git a/bumblebee/themes/default.json b/bumblebee/themes/default.json index 3a9a508..09fdcdd 100644 --- a/bumblebee/themes/default.json +++ b/bumblebee/themes/default.json @@ -1,7 +1,15 @@ { "defaults": { "prefix": " ", - "suffix" : " " + "suffix" : " ", + "cycle": [ + { + "fg": "red" + }, + { + "fg": "lime" + } + ] }, "battery": { "prefix": "bat ", diff --git a/i3bumblebee b/i3bumblebee index 9ef8bce..0fdab02 100755 --- a/i3bumblebee +++ b/i3bumblebee @@ -24,23 +24,25 @@ def main(): sys.exit(0) modules = [] - theme = bumblebee.theme.Theme(args.theme) if args.theme else bumblebee.theme.Theme() for m in args.modules: # TODO: how to cleanly handle errors here? # (useful error messages) module_name = m if not ":" in m else m.split(":")[0] module_args = None if not ":" in m else m.split(":")[1:] module = importlib.import_module("bumblebee.modules.{}".format(module_name)) - modules.append(getattr(module, "Module")(theme, module_args)) + modules.append(getattr(module, "Module")(module_args)) - output = bumblebee.outputs.i3.i3bar() + theme = bumblebee.theme.Theme(args.theme) if args.theme else bumblebee.theme.Theme() + output = bumblebee.outputs.i3.i3bar(theme) print output.start() sys.stdout.flush() while True: + theme.reset() for m in modules: output.add(m) + theme.next() print output.get() sys.stdout.flush() time.sleep(1)