Merge pull request #1372 from ZyX-I/pull-1366

Switch the underlying i3-IPC layer and deprecate i3bgbar bindings
This commit is contained in:
Nikolai Aleksandrovich Pavlov 2015-05-17 21:20:12 +03:00
commit bf6191d144
5 changed files with 125 additions and 30 deletions

View File

@ -51,8 +51,8 @@ Add the following to :file:`~/.config/qtile/config.py`:
.. _bar-usage: .. _bar-usage:
LemonBoys bar bar-aint-recursive
============== ==================
To run the bar simply pipe the output of the binding script into ``bar`` and To run the bar simply pipe the output of the binding script into ``bar`` and
specify appropriate options, for example like this:: specify appropriate options, for example like this::
@ -63,6 +63,9 @@ to run with i3, simply ``exec`` this in i3 config file::
exec python /path/to/powerline/bindings/bar/powerline-bar.py --i3 | bar exec python /path/to/powerline/bindings/bar/powerline-bar.py --i3 | bar
Running the binding in i3-mode will require `i3ipc <https://github.com/acrisci/i3ipc-python>`_
(or the outdated `i3-py <https://github.com/ziberna/i3-py>`_).
See the `bar documentation <https://github.com/LemonBoy/bar>`_ for more See the `bar documentation <https://github.com/LemonBoy/bar>`_ for more
information and options. information and options.

View File

@ -5,11 +5,10 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct
import sys import sys
import time import time
from threading import Lock from threading import Lock, Timer
from argparse import ArgumentParser from argparse import ArgumentParser
from powerline import Powerline from powerline import Powerline
from powerline.lib.monotonic import monotonic
from powerline.lib.encoding import get_unicode_writer from powerline.lib.encoding import get_unicode_writer
@ -20,14 +19,6 @@ class BarPowerline(Powerline):
super(BarPowerline, self).init(ext='wm', renderer_module='bar') super(BarPowerline, self).init(ext='wm', renderer_module='bar')
def render(event=None, data=None, sub=None):
global lock
with lock:
write(powerline.render())
write('\n')
sys.stdout.flush()
if __name__ == '__main__': if __name__ == '__main__':
parser = ArgumentParser(description='Powerline BAR bindings.') parser = ArgumentParser(description='Powerline BAR bindings.')
parser.add_argument( parser.add_argument(
@ -36,17 +27,37 @@ if __name__ == '__main__':
) )
args = parser.parse_args() args = parser.parse_args()
powerline = BarPowerline() powerline = BarPowerline()
interval = 0.5
lock = Lock() lock = Lock()
modes = ['default']
write = get_unicode_writer(encoding='utf-8') write = get_unicode_writer(encoding='utf-8')
def render(reschedule=False):
if reschedule:
Timer(0.5, render, kwargs={'reschedule': True}).start()
global lock
with lock:
write(powerline.render(mode=modes[0]))
write('\n')
sys.stdout.flush()
def update(evt):
modes[0] = evt.change
render()
render(reschedule=True)
if args.i3: if args.i3:
import i3 try:
sub = i3.Subscription(render, 'workspace') import i3ipc
except ImportError:
import i3
i3.Subscription(lambda evt, data, sub: print(render()), 'workspace')
else:
conn = i3ipc.Connection()
conn.on('workspace::focus', lambda conn, evt: render())
conn.on('mode', lambda conn, evt: update(evt))
conn.main()
while True: while True:
start_time = monotonic() time.sleep(1e10)
render()
time.sleep(max(interval - (monotonic() - start_time), 0.1))

View File

@ -35,10 +35,10 @@ class BarRenderer(Renderer):
return text + contents + '%{F-B--u}' return text + contents + '%{F-B--u}'
def render(self): def render(self, *args, **kwargs):
return '%{{l}}{0}%{{r}}{1}'.format( return '%{{l}}{0}%{{r}}{1}'.format(
super(BarRenderer, self).render(side='left'), super(BarRenderer, self).render(side='left', *args, **kwargs),
super(BarRenderer, self).render(side='right'), super(BarRenderer, self).render(side='right', *args, **kwargs),
) )

View File

@ -1,7 +1,10 @@
# vim:fileencoding=utf-8:noet # vim:fileencoding=utf-8:noet
from __future__ import (unicode_literals, division, absolute_import, print_function) from __future__ import (unicode_literals, division, absolute_import, print_function)
import i3 from powerline.theme import requires_segment_info
conn = None
def calcgrp(w): def calcgrp(w):
@ -16,12 +19,46 @@ def calcgrp(w):
return group return group
def workspaces(pl): def workspaces(pl, only_show=None, strip=0):
'''Return workspace list '''Return list of used workspaces
Highlight groups used: ``workspace``, ``w_visible``, ``w_focused``, ``w_urgent`` :param list only_show:
Specifies which workspaces to show. Valid entries are ``"visible"``,
``"urgent"`` and ``"focused"``. If omitted or ``null`` all workspaces
are shown.
:param int strip:
Specifies how many characters from the front of each workspace name
should be stripped (e.g. to remove workspace numbers). Defaults to zero.
Highlight groups used: ``workspace`` or ``w_visible``, ``workspace`` or ``w_focused``, ``workspace`` or ``w_urgent``.
''' '''
global conn
if not conn:
try:
import i3ipc
except ImportError:
import i3 as conn
else:
conn = i3ipc.Connection()
return [{ return [{
'contents': w['name'], 'contents': w['name'][min(len(w['name']), strip):],
'highlight_groups': calcgrp(w) 'highlight_groups': calcgrp(w)
} for w in i3.get_workspaces()] } for w in conn.get_workspaces() if not only_show or any((w[typ] for typ in only_show))]
@requires_segment_info
def mode(pl, segment_info, names={'default': None}):
'''Returns current i3 mode
:param dict names:
Specifies the string to show for various modes.
Use ``null`` to hide a mode (``default`` is hidden by default).
Highligh groups used: ``mode``
'''
mode = segment_info['mode']
if mode in names:
return names[mode]
return mode

View File

@ -8,7 +8,7 @@ from functools import partial
from collections import namedtuple from collections import namedtuple
from time import sleep from time import sleep
from powerline.segments import shell, tmux, pdb from powerline.segments import shell, tmux, pdb, i3wm
from powerline.lib.vcs import get_fallback_create_watcher from powerline.lib.vcs import get_fallback_create_watcher
from powerline.lib.unicode import out_u from powerline.lib.unicode import out_u
@ -815,6 +815,50 @@ class TestWthr(TestCommon):
self.module.weather.shutdown() self.module.weather.shutdown()
class TestI3WM(TestCase):
def test_workspaces(self):
pl = Pl()
with replace_attr(i3wm, 'conn', Args(get_workspaces=lambda: iter([
{'name': '1: w1', 'focused': False, 'urgent': False, 'visible': False},
{'name': '2: w2', 'focused': False, 'urgent': False, 'visible': True},
{'name': '3: w3', 'focused': False, 'urgent': True, 'visible': True},
{'name': '4: w4', 'focused': True, 'urgent': True, 'visible': True},
]))):
self.assertEqual(i3wm.workspaces(pl=pl), [
{'contents': '1: w1', 'highlight_groups': ['workspace']},
{'contents': '2: w2', 'highlight_groups': ['w_visible', 'workspace']},
{'contents': '3: w3', 'highlight_groups': ['w_urgent', 'w_visible', 'workspace']},
{'contents': '4: w4', 'highlight_groups': ['w_focused', 'w_urgent', 'w_visible', 'workspace']},
])
self.assertEqual(i3wm.workspaces(pl=pl, only_show=None), [
{'contents': '1: w1', 'highlight_groups': ['workspace']},
{'contents': '2: w2', 'highlight_groups': ['w_visible', 'workspace']},
{'contents': '3: w3', 'highlight_groups': ['w_urgent', 'w_visible', 'workspace']},
{'contents': '4: w4', 'highlight_groups': ['w_focused', 'w_urgent', 'w_visible', 'workspace']},
])
self.assertEqual(i3wm.workspaces(pl=pl, only_show=['focused', 'urgent']), [
{'contents': '3: w3', 'highlight_groups': ['w_urgent', 'w_visible', 'workspace']},
{'contents': '4: w4', 'highlight_groups': ['w_focused', 'w_urgent', 'w_visible', 'workspace']},
])
self.assertEqual(i3wm.workspaces(pl=pl, only_show=['visible']), [
{'contents': '2: w2', 'highlight_groups': ['w_visible', 'workspace']},
{'contents': '3: w3', 'highlight_groups': ['w_urgent', 'w_visible', 'workspace']},
{'contents': '4: w4', 'highlight_groups': ['w_focused', 'w_urgent', 'w_visible', 'workspace']},
])
self.assertEqual(i3wm.workspaces(pl=pl, only_show=['visible'], strip=3), [
{'contents': 'w2', 'highlight_groups': ['w_visible', 'workspace']},
{'contents': 'w3', 'highlight_groups': ['w_urgent', 'w_visible', 'workspace']},
{'contents': 'w4', 'highlight_groups': ['w_focused', 'w_urgent', 'w_visible', 'workspace']},
])
def test_mode(self):
pl = Pl()
self.assertEqual(i3wm.mode(pl=pl, segment_info={'mode': 'default'}), None)
self.assertEqual(i3wm.mode(pl=pl, segment_info={'mode': 'test'}), 'test')
self.assertEqual(i3wm.mode(pl=pl, segment_info={'mode': 'default'}, names={'default': 'test'}), 'test')
self.assertEqual(i3wm.mode(pl=pl, segment_info={'mode': 'test'}, names={'default': 'test', 'test': 't'}), 't')
class TestMail(TestCommon): class TestMail(TestCommon):
module_name = 'mail' module_name = 'mail'