[core/theme] Add support for foreground and background colors

Themes can now define "fg" and "bg" attributes that are used for
foreground (text) color and background color.

see #23
This commit is contained in:
Tobi-wan Kenobi 2016-12-09 08:58:45 +01:00
parent 0c7884d170
commit c52cb99518
6 changed files with 58 additions and 2 deletions

View file

@ -53,7 +53,9 @@ class I3BarOutput(object):
if suffix: if suffix:
full_text = u"{}{}".format(full_text, suffix) full_text = u"{}{}".format(full_text, suffix)
self._widgets.append({ self._widgets.append({
u"full_text": u"{}".format(full_text) u"full_text": u"{}".format(full_text),
"color": self._theme.fg(widget),
"background": self._theme.bg(widget),
}) })
def begin(self): def begin(self):

View file

@ -30,6 +30,14 @@ class Theme(object):
"""Return the theme suffix for a widget's full text""" """Return the theme suffix for a widget's full text"""
return self._get(widget, "suffix", None) return self._get(widget, "suffix", None)
def fg(self, widget):
"""Return the foreground color for this widget"""
return self._get(widget, "fg", None)
def bg(self, widget):
"""Return the background color for this widget"""
return self._get(widget, "bg", None)
def loads(self, data): def loads(self, data):
"""Initialize the theme from a JSON string""" """Initialize the theme from a JSON string"""
theme = json.loads(data) theme = json.loads(data)

View file

@ -19,6 +19,8 @@ class TestI3BarOutput(unittest.TestCase):
self.expectedStart = json.dumps({"version": 1, "click_events": True}) + "[\n" self.expectedStart = json.dumps({"version": 1, "click_events": True}) + "[\n"
self.expectedStop = "]\n" self.expectedStop = "]\n"
self.someWidget = MockWidget("foo bar baz") self.someWidget = MockWidget("foo bar baz")
self.anyColor = "#ababab"
self.anotherColor = "#cccccc"
@mock.patch("sys.stdout", new_callable=StringIO) @mock.patch("sys.stdout", new_callable=StringIO)
def test_start(self, stdout): def test_start(self, stdout):
@ -89,4 +91,14 @@ class TestI3BarOutput(unittest.TestCase):
self.theme.suffix(self.someWidget) self.theme.suffix(self.someWidget)
)) ))
@mock.patch("sys.stdout", new_callable=StringIO)
def test_colors(self, stdout):
self.theme.set_fg(self.anyColor)
self.theme.set_bg(self.anotherColor)
self.output.draw(self.someWidget)
self.output.flush()
result = json.loads(stdout.getvalue())[0]
self.assertEquals(result["color"], self.anyColor)
self.assertEquals(result["background"], self.anotherColor)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -14,10 +14,14 @@ class TestTheme(unittest.TestCase):
self.theme = Theme(self.validThemeName) self.theme = Theme(self.validThemeName)
self.widgetTheme = "test-widget" self.widgetTheme = "test-widget"
self.defaultColor = "#000000"
self.defaultBgColor = "#111111"
self.widgetBgColor = "#222222"
self.defaultPrefix = "default-prefix" self.defaultPrefix = "default-prefix"
self.defaultSuffix = "default-suffix" self.defaultSuffix = "default-suffix"
self.widgetPrefix = "widget-prefix" self.widgetPrefix = "widget-prefix"
self.widgetSuffix = "widget-suffix" self.widgetSuffix = "widget-suffix"
self.widgetColor = "#ababab"
def test_load_valid_theme(self): def test_load_valid_theme(self):
try: try:
@ -43,4 +47,14 @@ class TestTheme(unittest.TestCase):
self.someWidget.set_module(self.widgetTheme) self.someWidget.set_module(self.widgetTheme)
self.assertEquals(self.theme.prefix(self.someWidget), self.widgetPrefix) self.assertEquals(self.theme.prefix(self.someWidget), self.widgetPrefix)
def test_widget_fg(self):
self.assertEquals(self.theme.fg(self.someWidget), self.defaultColor)
self.someWidget.set_module(self.widgetTheme)
self.assertEquals(self.theme.fg(self.someWidget), self.widgetColor)
def test_widget_bg(self):
self.assertEquals(self.theme.bg(self.someWidget), self.defaultBgColor)
self.someWidget.set_module(self.widgetTheme)
self.assertEquals(self.theme.bg(self.someWidget), self.widgetBgColor)
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -49,6 +49,8 @@ class MockTheme(object):
def __init__(self): def __init__(self):
self._prefix = None self._prefix = None
self._suffix = None self._suffix = None
self._fg = None
self._bg = None
def set_prefix(self, value): def set_prefix(self, value):
self._prefix = value self._prefix = value
@ -56,10 +58,22 @@ class MockTheme(object):
def set_suffix(self, value): def set_suffix(self, value):
self._suffix = value self._suffix = value
def set_fg(self, value):
self._fg = value
def set_bg(self, value):
self._bg = value
def prefix(self, widget): def prefix(self, widget):
return self._prefix return self._prefix
def suffix(self, widget): def suffix(self, widget):
return self._suffix return self._suffix
def fg(self, widget):
return self._fg
def bg(self, widget):
return self._bg
# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4

View file

@ -2,6 +2,12 @@
"icons": [ "test" ], "icons": [ "test" ],
"defaults": { "defaults": {
"prefix": "default-prefix", "prefix": "default-prefix",
"suffix": "default-suffix" "suffix": "default-suffix",
"fg": "#000000",
"bg": "#111111"
},
"test-widget": {
"fg": "#ababab",
"bg": "#222222"
} }
} }