From b20a707222bb613b798994a3539d8da90f838256 Mon Sep 17 00:00:00 2001 From: S0lll0s Date: Fri, 15 May 2015 23:33:14 +0200 Subject: [PATCH] Add powerline.segments.i3wm.mode also make powerline-bar.py redraw on mode change --- powerline/bindings/bar/powerline-bar.py | 12 ++++++--- powerline/segments/i3wm.py | 34 +++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/powerline/bindings/bar/powerline-bar.py b/powerline/bindings/bar/powerline-bar.py index c9badb3a..3e000da5 100755 --- a/powerline/bindings/bar/powerline-bar.py +++ b/powerline/bindings/bar/powerline-bar.py @@ -30,6 +30,7 @@ def render(reschedule=False): write('\n') sys.stdout.flush() + if __name__ == '__main__': parser = ArgumentParser(description='Powerline BAR bindings.') parser.add_argument( @@ -47,11 +48,14 @@ if __name__ == '__main__': if args.i3: 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') + else: + conn = i3ipc.Connection() + conn.on('workspace::focus', lambda conn, evt: render()) + conn.on('mode', lambda conn, evt: render()) + conn.main() - while True: pass + while True: + time.sleep(0.5) diff --git a/powerline/segments/i3wm.py b/powerline/segments/i3wm.py index 3132b087..87ecfdb8 100644 --- a/powerline/segments/i3wm.py +++ b/powerline/segments/i3wm.py @@ -1,6 +1,11 @@ # vim:fileencoding=utf-8:noet from __future__ import (unicode_literals, division, absolute_import, print_function) +from threading import Thread + +from powerline.segments import Segment, with_docstring + + conn = None try: import i3ipc @@ -36,3 +41,32 @@ def workspaces(pl, strip=0): 'contents': w['name'][min(len(w['name']),strip):], 'highlight_groups': calcgrp(w) } for w in conn.get_workspaces()] + +class ModeSegment(Segment): + def startup(self, pl, shutdown_event): + self.mode = 'default' + + def callback(conn, e): + self.mode = e.change + + conn = i3ipc.Connection() + conn.on('mode', callback) + self.thread = Thread(target=conn.main) + self.thread.daemon = True + self.thread.start() + + def __call__(self, pl, default=None): + if self.mode == 'default': + return default + return self.mode + + +mode = with_docstring(ModeSegment(), +'''Returns the current i3 mode + +:param str default: + Specifies the name to be displayed instead of "default". + By default the segment is left out in the default mode. + +Highligh groups used: ``mode`` +''')