diff options
author | Camil Staps | 2017-02-20 10:06:38 +0100 |
---|---|---|
committer | Camil Staps | 2017-02-20 10:06:38 +0100 |
commit | 73ae3a47a410eb2db9c3c776c0252e1feaeb2d7e (patch) | |
tree | 4835551f5d769ede433ed390d3cfc2406975480c /i3/bin/i3status.py | |
parent | Cloogle stats in status bar (diff) |
Reorganise binaries
Diffstat (limited to 'i3/bin/i3status.py')
-rwxr-xr-x | i3/bin/i3status.py | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/i3/bin/i3status.py b/i3/bin/i3status.py new file mode 100755 index 0000000..1e00e62 --- /dev/null +++ b/i3/bin/i3status.py @@ -0,0 +1,72 @@ +#!/usr/bin/env python +import json +from select import select +from subprocess import Popen, PIPE +import sys + +def remove_empty_outputs(obj): + return [o for o in obj if o['full_text'].strip() != ''] + +first = True +def print_i3stat(j): + global first + j = remove_empty_outputs(j) + sys.stdout.write(('' if first else ',') + json.dumps(j) + '\n') + sys.stdout.flush() + first = False + +def parse_i3stat(s): + if s[0] == ',': + s = s[1:] + j = json.loads(s) + return j + +def parse_kbdlayout(s): + colors = { 'us(intl)' : '#66ccff', + 'us(dvorak-intl)' : '#dc68fc', + 'ru(phonetic)' : '#ff3300', + 'il(biblicalSIL)' : '#66ff66' + } + return [{'full_text': s, 'color': colors[s]}] + +def parse_cloogle(s): + return [{'full_text': s}] + +def merge_status_items(*args): + return [item for sublist in args for item in sublist] + +if __name__ == '__main__': + timeout = 0.1 + + # open subprocesses + i3stat = Popen(['i3status'], stdout=PIPE, bufsize=1, close_fds=True) + kbdlayout = Popen(['unbuffer', 'kbdlayout'], + stdout=PIPE, bufsize=1, close_fds=True) + cloogle = Popen(['unbuffer', 'cloogle_stats.py'], + stdout=PIPE, bufsize=1, close_fds=True) + + # skip i3status header + sys.stdout.write(i3stat.stdout.readline()) + sys.stdout.write(i3stat.stdout.readline()) + + stat, kbd, clg = [], [], [] + try: + stdouts = [p.stdout for p in [i3stat, kbdlayout, cloogle]] + while True: + fs = select(stdouts, [], [])[0] + for f in fs: + line = f.readline()[:-1] + if f == kbdlayout.stdout: + kbd = parse_kbdlayout(line) + elif f == cloogle.stdout: + clg = parse_cloogle(line) + elif f == i3stat.stdout: + stat = parse_i3stat(line) + + print_i3stat(merge_status_items(kbd, clg, stat)) + + except Exception, e: + print(e) + i3stat.kill() + kbdlayout.kill() + cloogle.kill() |