Created
December 29, 2015 02:58
-
-
Save Luminarys/59c7f561685a7dbaf336 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import datetime | |
import time | |
import sys | |
import asyncio | |
import i3ipc | |
def getDate(): | |
today = datetime.date.today() | |
return today.strftime("%A %B %d, %Y") | |
def getTime(): | |
now = datetime.datetime.now() | |
return now.strftime("%I:%M %p") | |
def getWorkspaces(i3): | |
output = sys.argv[1] | |
workspaces = i3.get_workspaces() | |
names = [workspace["name"] for workspace in filter(lambda ws: ws.output==output, workspaces)] | |
return " ".join(names) | |
def run_async(fn, args, vals): | |
future = asyncio.Future() | |
args.insert(0, future) | |
args.insert(1, vals) | |
asyncio.ensure_future(fn(*args)) | |
future.add_done_callback(lambda fut: print_bar(fut, vals)) | |
@asyncio.coroutine | |
def asyncDate(future, vals): | |
yield from asyncio.sleep(500) | |
future.set_result({"date": getDate()}) | |
run_async(asyncDate, [], vals) | |
@asyncio.coroutine | |
def asyncTime(future, vals): | |
yield from asyncio.sleep(15) | |
future.set_result({"time": getTime()}) | |
run_async(asyncTime, [], vals) | |
@asyncio.coroutine | |
def asyncWorkspaces(future, vals, i3): | |
yield from asyncio.sleep(0.1) | |
future.set_result({"workspaces": getWorkspaces(i3)}) | |
run_async(asyncWorkspaces, [i3], vals) | |
def print_bar(future, vals): | |
vals.update(future.result()) | |
output = "{left}{f_white}{b_black} {date} {center} {workspaces} {right} {time} {f_clear}".format(**vals) | |
print(output) | |
sys.stdout.flush() | |
i3 = i3ipc.Connection() | |
loop = asyncio.get_event_loop() | |
vals = { | |
"left": "%{l}", | |
"right": "%{r}", | |
"center": "%{c}", | |
"f_white": "%{F#FF000000}", | |
"f_clear": "%{F-}", | |
"b_black": "%{B#FFFFFFFF}", | |
"date": getDate(), | |
"time": getTime(), | |
"workspaces": getWorkspaces(i3) | |
} | |
output = "{left}{f_white}{b_black} {date} {center} {workspaces} {right} {time} {f_clear}".format(**vals) | |
print(output) | |
sys.stdout.flush() | |
run_async(asyncDate, [], vals) | |
run_async(asyncTime, [], vals) | |
run_async(asyncWorkspaces, [i3], vals) | |
loop.run_forever() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment