From 928940d84865aba3668eec1d13a89245c5623f08 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sat, 22 Apr 2017 13:07:50 +0200 Subject: [PATCH] [core] Add a "scrollable" modifier for widget texts callbacks If a module defines a callback for a widget's text, an optional decorator "@bumblebee.output.scrollable" can be used to make the text scrollable. In those cases, the desired width is set to (in decreasing order of priority): 1. whatever the widget defines as "theme.width" 2. whatever the theme defines as "width" for the module 3. whatever the commandline parameter "width" for the module is set to 4. 30 (determined by unfair dice roll) see #27 --- bumblebee/output.py | 24 +++++++++++++++++++++++- bumblebee/theme.py | 2 +- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/bumblebee/output.py b/bumblebee/output.py index bf6a689..e8a1767 100644 --- a/bumblebee/output.py +++ b/bumblebee/output.py @@ -8,6 +8,27 @@ import uuid import bumblebee.store +def scrollable(func): + def wrapper(module, widget): + text = func(module, widget) + 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 + start = widget.get("scrolling.start", -1) + direction = widget.get("scrolling.direction", "right") + start += 1 if direction == "right" else -1 + widget.set("scrolling.start", start) + if width + start >= len(text): + widget.set("scrolling.direction", "left") + if start <= 0: + widget.set("scrolling.direction", "right") + text = text[start:width+start] + + return text + return wrapper + class Widget(bumblebee.store.Store): """Represents a single visible block in the status bar""" def __init__(self, full_text="", name=""): @@ -83,13 +104,14 @@ class I3BarOutput(object): "background": self._theme.separator_bg(widget), "separator_block_width": self._theme.separator_block_width(widget), }) + width = self._theme.minwidth(widget) self._widgets.append({ u"full_text": full_text, "color": self._theme.fg(widget), "background": self._theme.bg(widget), "separator_block_width": self._theme.separator_block_width(widget), "separator": True if separator is None else False, - "min_width": self._theme.minwidth(widget), + "min_width": width + "A"*(len(prefix) + len(suffix)) if width else None, "align": self._theme.align(widget), "instance": widget.id, "name": module.id, diff --git a/bumblebee/theme.py b/bumblebee/theme.py index a424f4a..0b2a724 100644 --- a/bumblebee/theme.py +++ b/bumblebee/theme.py @@ -139,7 +139,7 @@ class Theme(object): state_themes.append(self._get(widget, state, {})) value = self._defaults.get(name, default) - value = widget.get("theme-{}".format(name), value) + value = widget.get("theme.{}".format(name), value) value = self._cycle.get(name, value) value = module_theme.get(name, value)