2014-10-21 00:49:47 -04:00

114 lines
3.1 KiB
Python

#!/usr/bin/env python
import json
import subprocess
import sys
import threading
from queue import Queue
def format_color(text, color):
text = text.replace(r'%', r'%%')
color = color.replace("#", "#ff")
return r"%{F" + color + r"}" + text + r"%{F-}"
def format_output(datum):
text = datum['full_text']
if 'color' in datum:
text = format_color(text, datum['color'])
return text
def send_output(text):
print(text)
def process_status(q):
status = subprocess.Popen(
["python", "/home/ian/.i3/status.py"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
# get the version and newline
for i in range(2):
status.stdout.readline()
# continue to process each line
while not status.stdout.closed:
line = status.stdout.readline().decode("utf-8")
if line.startswith(','):
line = line[1:]
center = []
right = []
data = json.loads(line)
for datum in data:
if 'Clock' in datum['name']:
center.append(format_output(datum))
else:
right.append(format_output(datum))
separator = ' | '
q.put_nowait(("", separator.join(center), separator.join(right)))
def process_bspwm(q):
bspwm = subprocess.Popen(
["bspc", "control", "--subscribe"],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
# continue to process each line
while not bspwm.stdout.closed:
line = bspwm.stdout.readline().decode("utf-8")
left = []
desktops = line.strip().split(':')
# ignore first and last bit from bspc output
desktops = desktops[1:len(desktops) - 1]
for i, desktop in enumerate(desktops):
if desktop.startswith('o'):
left.append(
"%{{A:bspc desktop -f ^{i}:}}[{i}]%{{A}}".format(i=i + 1))
elif desktop.startswith('O') or desktop.startswith('F'):
left.append(
("%{{F#fff92672}}"
"%{{A:bspc desktop -f ^{i}:}}[{i}]%{{A}}"
"%{{F-}}").format(i=i + 1))
# elif desktop.startswith('f'):
# left.append("[{}]".format(i))
q.put_nowait((''.join(left), "", ""))
def get_output():
left = ""
center = ""
right = ""
q = Queue()
status_thread = threading.Thread(target=process_status, args=[q])
status_thread.start()
bspwm_thread = threading.Thread(target=process_bspwm, args=[q])
bspwm_thread.start()
while True:
new_left, new_center, new_right = q.get()
if new_left:
left = new_left
if new_center:
center = new_center
if new_right:
right = new_right
yield left, center, right
def main():
for left, center, right in get_output():
output = '%{{l}} {} %{{c}} {} %{{r}} {}'.format(left, center, right)
send_output(output)
# sys.stderr.write(output + '\n')
sys.stderr.flush()
sys.stdout.flush()
if __name__ == '__main__':
main()