From 918fd8227d3dbcb281e787e17894fd2cf61d8d18 Mon Sep 17 00:00:00 2001 From: S0lll0s Date: Thu, 14 May 2015 16:08:52 +0200 Subject: [PATCH] (Optionally) switch from "i3-py" to "i3ipc" for i3 bindings i3-py is no longer maintained --- powerline/bindings/bar/powerline-bar.py | 25 +++++++++++++++---------- powerline/segments/i3wm.py | 21 ++++++++++++++++----- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/powerline/bindings/bar/powerline-bar.py b/powerline/bindings/bar/powerline-bar.py index 898c5cc6..c9badb3a 100755 --- a/powerline/bindings/bar/powerline-bar.py +++ b/powerline/bindings/bar/powerline-bar.py @@ -5,7 +5,7 @@ from __future__ import (unicode_literals, division, absolute_import, print_funct import sys import time -from threading import Lock +from threading import Lock, Timer from argparse import ArgumentParser from powerline import Powerline @@ -20,14 +20,16 @@ class BarPowerline(Powerline): super(BarPowerline, self).init(ext='wm', renderer_module='bar') -def render(event=None, data=None, sub=None): +def render(reschedule=False): + if reschedule: + Timer(0.5, render, kwargs={"reschedule": True}).start() + global lock with lock: write(powerline.render()) write('\n') sys.stdout.flush() - if __name__ == '__main__': parser = ArgumentParser(description='Powerline BAR bindings.') parser.add_argument( @@ -37,16 +39,19 @@ if __name__ == '__main__': args = parser.parse_args() powerline = BarPowerline() - interval = 0.5 lock = Lock() write = get_unicode_writer(encoding='utf-8') + render(reschedule=True) if args.i3: - import i3 - sub = i3.Subscription(render, 'workspace') + try: + import i3ipc + conn = i3ipc.Connection() + conn.on('workspace::focus', lambda conn, evt: render()) + conn.main() + except ImportError: + import i3 + i3.Subscription(lambda evt, data, sub: print(render()), 'workspace') - while True: - start_time = monotonic() - render() - time.sleep(max(interval - (monotonic() - start_time), 0.1)) + while True: pass diff --git a/powerline/segments/i3wm.py b/powerline/segments/i3wm.py index f3987adf..3132b087 100644 --- a/powerline/segments/i3wm.py +++ b/powerline/segments/i3wm.py @@ -1,7 +1,11 @@ # vim:fileencoding=utf-8:noet from __future__ import (unicode_literals, division, absolute_import, print_function) -import i3 +conn = None +try: + import i3ipc +except ImportError: + import i3 as conn def calcgrp(w): @@ -16,12 +20,19 @@ def calcgrp(w): return group -def workspaces(pl): - '''Return workspace list +def workspaces(pl, strip=0): + '''Return list of used workspaces + + :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``, ``w_visible``, ``w_focused``, ``w_urgent`` ''' + global conn + if not conn: conn = i3ipc.Connection() + return [{ - 'contents': w['name'], + 'contents': w['name'][min(len(w['name']),strip):], 'highlight_groups': calcgrp(w) - } for w in i3.get_workspaces()] + } for w in conn.get_workspaces()]