diff --git a/powerline/bindings/i3/powerline-i3.py b/powerline/bindings/i3/powerline-i3.py index ed8920ca..f44e9281 100755 --- a/powerline/bindings/i3/powerline-i3.py +++ b/powerline/bindings/i3/powerline-i3.py @@ -7,7 +7,7 @@ import time from threading import Lock -import i3 +from powerline.bindings.wm import get_i3_connection, i3_subscribe from powerline import Powerline from powerline.lib.monotonic import monotonic @@ -43,7 +43,8 @@ if __name__ == '__main__': print (',[' + powerline.render()[:-1] + ']') sys.stdout.flush() - sub = i3.Subscription(render, 'workspace') + i3 = get_i3_connection() + i3_subscribe(i3, 'workspace', render) while True: start_time = monotonic() diff --git a/powerline/bindings/wm/__init__.py b/powerline/bindings/wm/__init__.py index c635db54..130c0e4f 100644 --- a/powerline/bindings/wm/__init__.py +++ b/powerline/bindings/wm/__init__.py @@ -14,6 +14,43 @@ DEFAULT_UPDATE_INTERVAL = 0.5 conn = None +def i3_subscribe(conn, event, callback): + '''Subscribe to i3 workspace event + + :param conn: + Connection returned by :py:func:`get_i3_connection`. + :param str event: + Event to subscribe to, e.g. ``'workspace'``. + :param func callback: + Function to run on event. + ''' + try: + import i3 + except ImportError: + pass + else: + conn.Subscription(callback, event) + return + + conn.on(event, callback) + + from threading import Thread + + class I3Thread(Thread): + daemon = True + + def __init__(self, conn): + super(I3Thread, self).__init__() + self.__conn = conn + + def run(self): + self.__conn.main() + + thread = I3Thread(conn=conn) + + thread.start() + + def get_i3_connection(): '''Return a valid, cached i3 Connection instance '''