diff --git a/docs/source/usage/wm-widgets.rst b/docs/source/usage/wm-widgets.rst index 0a734ea7..6a28246d 100644 --- a/docs/source/usage/wm-widgets.rst +++ b/docs/source/usage/wm-widgets.rst @@ -44,13 +44,30 @@ Add the following to :file:`~/.config/qtile/config.py`: ), ] +.. _bar-usage: + +LemonBoy’s bar +============= + +To run the bar simply pipe the output of the binding script into ``bar`` and specify appropriate +options, for example like this:: + + python powerline-bar.py | bar -f "-xos4-*" + +to run with i3, simply ``exec`` this in i3 config file:: + + exec python powerline-bar.py | bar -f "-xos4-*" + +See the `bar documentation `_ for more information and options. + I3 bar ====== .. note:: - Until the patch is done in i3 a custom ``i3bar`` build called ``i3bgbar`` is - needed. The source is available `in S0lll0s/i3bgbar github repository - `_. + As the patch to include background-colors in i3bar is likely not to be + merged, it is recommended to instead run ``bar`` (see above). The source for + i3bgbar is however still available `here + `_. Add the following to :file:`~/.i3/config`:: diff --git a/powerline/bindings/i3/powerline-bar.py b/powerline/bindings/i3/powerline-bar.py new file mode 100644 index 00000000..fe29a08b --- /dev/null +++ b/powerline/bindings/i3/powerline-bar.py @@ -0,0 +1,42 @@ +#!/usr/bin/env python +# vim:fileencoding=utf-8:noet +from __future__ import (unicode_literals, division, absolute_import, print_function) + +import sys +import time +import i3 +from threading import Lock +from powerline import Powerline +from powerline.lib.monotonic import monotonic + +if __name__ == '__main__': + name = 'wm' + if len(sys.argv) > 1: + name = sys.argv[1] + + powerline = Powerline( name, renderer_module='bar' ) + powerline.update_renderer() + + interval = 0.5 + lock = Lock() + + def encode( str ): return str.encode('utf-8') + + if sys.version_info > (3,0): + def encode( str ): return str + + def render(event=None, data=None, sub=None): + global lock + with lock: + ln = '%{l}' + ln += powerline.render(side='left') + ln += '%{r}' + ln += powerline.render(side='right') + print( encode(ln) ) + sys.stdout.flush() + + sub = i3.Subscription(render, 'workspace') + while True: + start_time = monotonic() + render() + time.sleep(max(interval - (monotonic() - start_time), 0.1)) diff --git a/powerline/renderers/bar.py b/powerline/renderers/bar.py new file mode 100644 index 00000000..e61c3cc3 --- /dev/null +++ b/powerline/renderers/bar.py @@ -0,0 +1,39 @@ +# vim:fileencoding=utf-8:noet +from __future__ import (unicode_literals, division, absolute_import, print_function) + +from powerline.renderer import Renderer +from powerline.colorscheme import ATTR_BOLD, ATTR_ITALIC, ATTR_UNDERLINE + + +class BarRenderer(Renderer): + '''bar (bar ain't recursive) renderer + + + See documentation of `bar `_ and :ref:`the usage instructions ` + ''' + + character_translations = Renderer.character_translations.copy() + character_translations[ord('%')] = '%%' + + @staticmethod + def hlstyle(*args, **kwargs): + # We don’t need to explicitly reset attributes, so skip those calls + return '' + + def hl(self, contents, fg=None, bg=None, attrs=None): + text = '' + + if fg is not None: + if fg is not False and fg[1] is not False: + text += '%{{F#ff{0:06x}}}'.format(fg[1]) + if bg is not None: + if bg is not False and bg[1] is not False: + text += '%{{B#ff{0:06x}}}'.format(bg[1]) + + if attrs & ATTR_UNDERLINE: + text += '%{+u}' + + return text + contents + '%{F-B--u}' + + +renderer = BarRenderer diff --git a/tests/test_configuration.py b/tests/test_configuration.py index 9d6af211..07d98b71 100644 --- a/tests/test_configuration.py +++ b/tests/test_configuration.py @@ -43,6 +43,10 @@ config = { 'theme': 'default', 'colorscheme': 'default', }, + 'wm': { + 'theme': 'default', + 'colorscheme': 'default', + }, }, }, 'colors': { @@ -91,6 +95,14 @@ config = { 'environment': {'fg': 'col3', 'bg': 'col4', 'attrs': ['underline']}, }, }, + 'colorschemes/wm/default': { + 'groups': { + 'hl1': {'fg': 'col1', 'bg': 'col2', 'attrs': ['underline']}, + 'hl2': {'fg': 'col2', 'bg': 'col1', 'attrs': []}, + 'hl3': {'fg': 'col3', 'bg': 'col1', 'attrs': ['underline']}, + 'hl4': {'fg': 'col2', 'bg': 'col4', 'attrs': []}, + }, + }, 'themes/test/default': { 'segments': { 'left': [ @@ -142,6 +154,19 @@ config = { ], }, }, + 'themes/wm/default': { + 'default_module': 'powerline.segments.common', + 'segments': { + 'left': [ + highlighted_string('A', 'hl1'), + highlighted_string('B', 'hl2'), + ], + 'right': [ + highlighted_string('C', 'hl3'), + highlighted_string('D', 'hl4'), + ], + }, + }, } @@ -799,6 +824,24 @@ class TestVim(TestCase): def tearDownClass(cls): sys.path.pop(0) +class TestBar(TestCase): + def test_bar(self): + import powerline as powerline_module + with swap_attributes(config, powerline_module): + with get_powerline_raw(config, powerline_module, ext='wm', renderer_module='bar') as powerline: + self.assertEqual("%{{l}}{}%{{r}}{}".format(powerline.render(side='left'), powerline.render(side='right')), "SOME_STRING_HERE") + + def test_bar_escape(self): + from powerline.shell import ShellPowerline + import powerline as powerline_module + config['themes/wm/default']['segments']['left'] = { + highlighted_string('%{asd}', 'hl1'), + highlighted_string('10% %', 'hl2'), + } + with swap_attributes(config, powerline_module): + with get_powerline_raw(config, powerline_module, ext='wm', renderer_module='bar') as powerline: + self.assertEqual(powerline.render(side='left'), "SOME_STRING_HERE") + if __name__ == '__main__': from tests import main