[core/theme] Add prefix/postfix methods

Add a way to specify prefix and postfix strings to the full text of a
widget's text. Currently, the theme does not fill those yet.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-08 11:31:20 +01:00
parent e6666becb3
commit 64f5fc100e
6 changed files with 73 additions and 6 deletions

View file

@ -1,13 +1,15 @@
#!/usr/bin/env python
import sys
import bumblebee.theme
import bumblebee.engine
import bumblebee.config
import bumblebee.output
def main():
config = bumblebee.config.Config(sys.argv[1:])
output = bumblebee.output.I3BarOutput()
theme = bumblebee.theme.Theme(config.theme())
output = bumblebee.output.I3BarOutput(theme=theme)
engine = bumblebee.engine.Engine(
config=config,
output=output,

View file

@ -7,12 +7,14 @@ module parameters, etc.) to all other components
import argparse
MODULE_HELP = ""
THEME_HELP = ""
def create_parser():
"""Create the argument parser"""
parser = argparse.ArgumentParser(description="display system data in the i3bar")
parser.add_argument("-m", "--modules", nargs="+", default=[],
help=MODULE_HELP)
parser.add_argument("-t", "--theme", default="default", help=THEME_HELP)
return parser
class Config(object):
@ -32,4 +34,8 @@ class Config(object):
"name": x if not ":" in x else x.split(":")[1],
} for x in self._args.modules]
def theme(self):
"""Return the name of the selected theme"""
return self._args.theme
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -19,8 +19,8 @@ class Widget(object):
class I3BarOutput(object):
"""Manage output according to the i3bar protocol"""
def __init__(self):
pass
def __init__(self, theme):
self._theme = theme
def start(self):
"""Print start preamble for i3bar protocol"""
@ -32,8 +32,13 @@ class I3BarOutput(object):
def draw_widget(self, result, widget):
"""Draw a single widget"""
full_text = widget.full_text()
if self._theme.prefix():
full_text = "{}{}".format(self._theme.prefix(), full_text)
if self._theme.suffix():
full_text = "{}{}".format(full_text, self._theme.suffix())
result.append({
u"full_text": widget.full_text()
u"full_text": "{}".format(full_text)
})
def draw(self, widgets, engine=None):

View file

@ -14,6 +14,14 @@ class Theme(object):
def __init__(self, name):
self._theme = self.load(name)
def prefix(self):
"""Return the theme prefix for a widget's full text"""
return None
def suffix(self):
"""Return the theme suffix for a widget's full text"""
return None
def load(self, name):
"""Load and parse a theme file"""
path = theme_path()

View file

@ -10,10 +10,12 @@ except ImportError:
from bumblebee.output import I3BarOutput
from tests.util import MockWidget
from tests.util import MockTheme
class TestI3BarOutput(unittest.TestCase):
def setUp(self):
self.output = I3BarOutput()
self.theme = MockTheme()
self.output = I3BarOutput(self.theme)
self.expectedStart = json.dumps({"version": 1, "click_events": True}) + "[\n"
self.expectedStop = "]\n"
self.someWidget = MockWidget("foo bar baz")
@ -46,5 +48,32 @@ class TestI3BarOutput(unittest.TestCase):
self.output.flush()
self.assertEquals(",\n", stdout.getvalue())
@mock.patch("sys.stdout", new_callable=StringIO)
def test_prefix(self, stdout):
self.theme.set_prefix(" - ")
self.output.draw(self.someWidget)
result = json.loads(stdout.getvalue())[0]
self.assertEquals(result["full_text"], "{}{}".format(
self.theme.prefix(), self.someWidget.full_text())
)
@mock.patch("sys.stdout", new_callable=StringIO)
def test_suffix(self, stdout):
self.theme.set_suffix(" - ")
self.output.draw(self.someWidget)
result = json.loads(stdout.getvalue())[0]
self.assertEquals(result["full_text"], "{}{}".format(
self.someWidget.full_text(), self.theme.suffix())
)
@mock.patch("sys.stdout", new_callable=StringIO)
def test_bothfix(self, stdout):
self.theme.set_suffix(" - ")
self.theme.set_prefix(" * ")
self.output.draw(self.someWidget)
result = json.loads(stdout.getvalue())[0]
self.assertEquals(result["full_text"], "{}{}{}".format(
self.theme.prefix(), self.someWidget.full_text(), self.theme.suffix())
)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -29,4 +29,21 @@ class MockWidget(object):
def full_text(self):
return self._text
class MockTheme(object):
def __init__(self):
self._prefix = None
self._suffix = None
def set_prefix(self, value):
self._prefix = value
def set_suffix(self, value):
self._suffix = value
def prefix(self):
return self._prefix
def suffix(self):
return self._suffix
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4