From a69e058dd174a14d48ff1a393f77ec1501d29532 Mon Sep 17 00:00:00 2001 From: Tobias Witek Date: Sun, 23 Feb 2020 14:47:20 +0100 Subject: [PATCH] [core] Add decorators library For now, simply add a decorator that allows for scrolling of text --- core/decorators.py | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 core/decorators.py diff --git a/core/decorators.py b/core/decorators.py new file mode 100644 index 0000000..2d39521 --- /dev/null +++ b/core/decorators.py @@ -0,0 +1,36 @@ +import util.format + +def scrollable(func): + def wrapper(module, widget): + text = func(module, widget) + if not text: + return text + width = widget.get('theme.width', util.format.asint(module.parameter('width', 30))) + if util.format.asbool(module.parameter('scrolling.makewide', True)): + widget.set('theme.minwidth', 'A'*width) + if width < 0 or len(text) <= width: + return text + + bounce = util.format.asint(module.parameter('scrolling.bounce', 1)) + scroll_speed = util.format.asint(module.parameter('scrolling.speed', 1)) + start = widget.get('scrolling.start', -1) + direction = widget.get('scrolling.direction', 'right') + 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) + widget.set('scrolling.start', start) + text = text[start:width+start] + + return text + return wrapper + +# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4