[core/theme] Add support for default -> prefix/suffix in themes
Themes can now define default prefix and suffix strings. see #23
This commit is contained in:
parent
64f5fc100e
commit
394ef61760
5 changed files with 45 additions and 15 deletions
|
@ -33,10 +33,12 @@ 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())
|
||||
prefix = self._theme.prefix(widget)
|
||||
suffix = self._theme.suffix(widget)
|
||||
if prefix:
|
||||
full_text = "{}{}".format(prefix, full_text)
|
||||
if suffix:
|
||||
full_text = "{}{}".format(full_text, suffix)
|
||||
result.append({
|
||||
u"full_text": "{}".format(full_text)
|
||||
})
|
||||
|
|
|
@ -12,15 +12,24 @@ def theme_path():
|
|||
class Theme(object):
|
||||
"""Represents a collection of icons and colors"""
|
||||
def __init__(self, name):
|
||||
self._theme = self.load(name)
|
||||
theme = self.load(name)
|
||||
self._init(self.load(name))
|
||||
|
||||
def prefix(self):
|
||||
def _init(self, data):
|
||||
"""Initialize theme from data structure"""
|
||||
self._defaults = data.get("defaults", {})
|
||||
|
||||
def prefix(self, widget):
|
||||
"""Return the theme prefix for a widget's full text"""
|
||||
return None
|
||||
return self._get(widget, "prefix", None)
|
||||
|
||||
def suffix(self):
|
||||
def suffix(self, widget):
|
||||
"""Return the theme suffix for a widget's full text"""
|
||||
return None
|
||||
return self._get(widget, "suffix", None)
|
||||
|
||||
def loads(self, data):
|
||||
theme = json.loads(data)
|
||||
self._init(theme)
|
||||
|
||||
def load(self, name):
|
||||
"""Load and parse a theme file"""
|
||||
|
@ -35,4 +44,10 @@ class Theme(object):
|
|||
else:
|
||||
raise bumblebee.error.ThemeLoadError("no such theme: {}".format(name))
|
||||
|
||||
def _get(self, widget, name,default=None):
|
||||
value = default
|
||||
value = self._defaults.get(name, value)
|
||||
|
||||
return value
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -54,7 +54,7 @@ class TestI3BarOutput(unittest.TestCase):
|
|||
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.prefix(self.someWidget), self.someWidget.full_text())
|
||||
)
|
||||
|
||||
@mock.patch("sys.stdout", new_callable=StringIO)
|
||||
|
@ -63,7 +63,7 @@ class TestI3BarOutput(unittest.TestCase):
|
|||
self.output.draw(self.someWidget)
|
||||
result = json.loads(stdout.getvalue())[0]
|
||||
self.assertEquals(result["full_text"], "{}{}".format(
|
||||
self.someWidget.full_text(), self.theme.suffix())
|
||||
self.someWidget.full_text(), self.theme.suffix(self.someWidget))
|
||||
)
|
||||
|
||||
@mock.patch("sys.stdout", new_callable=StringIO)
|
||||
|
@ -73,7 +73,7 @@ class TestI3BarOutput(unittest.TestCase):
|
|||
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())
|
||||
self.theme.prefix(self.someWidget), self.someWidget.full_text(), self.theme.suffix(self.someWidget))
|
||||
)
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -3,15 +3,18 @@
|
|||
import unittest
|
||||
from bumblebee.theme import Theme
|
||||
from bumblebee.error import ThemeLoadError
|
||||
from tests.util import MockWidget
|
||||
|
||||
class TestTheme(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.nonexistentThemeName = "no-such-theme"
|
||||
self.invalidThemeName = "invalid"
|
||||
self.validThemeName = "solarized-powerline"
|
||||
self.someWidget = MockWidget("foo")
|
||||
|
||||
def test_load_valid_theme(self):
|
||||
try:
|
||||
Theme("solarized-powerline")
|
||||
Theme(self.validThemeName)
|
||||
except Exception as e:
|
||||
self.fail(e)
|
||||
|
||||
|
@ -23,4 +26,14 @@ class TestTheme(unittest.TestCase):
|
|||
with self.assertRaises(ThemeLoadError):
|
||||
Theme(self.invalidThemeName)
|
||||
|
||||
def test_prefix(self):
|
||||
theme = Theme(self.validThemeName)
|
||||
theme.loads('{"defaults": { "prefix": "test" }}')
|
||||
self.assertEquals(theme.prefix(self.someWidget), "test")
|
||||
|
||||
def test_suffix(self):
|
||||
theme = Theme(self.validThemeName)
|
||||
theme.loads('{"defaults": { "suffix": "test" }}')
|
||||
self.assertEquals(theme.suffix(self.someWidget), "test")
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
|
@ -40,10 +40,10 @@ class MockTheme(object):
|
|||
def set_suffix(self, value):
|
||||
self._suffix = value
|
||||
|
||||
def prefix(self):
|
||||
def prefix(self, widget):
|
||||
return self._prefix
|
||||
|
||||
def suffix(self):
|
||||
def suffix(self, widget):
|
||||
return self._suffix
|
||||
|
||||
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4
|
||||
|
|
Loading…
Reference in a new issue