-
Notifications
You must be signed in to change notification settings - Fork 1
/
i3status-plus.py
executable file
·85 lines (73 loc) · 2.6 KB
/
i3status-plus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/usr/bin/python
#
# Hacked from http://code.stapelberg.de/git/i3status/tree/contrib/wrapper.py
# Initial credits go to Valentin Haenel
#
# 2013 syl20bnr <[email protected]>
#
# This program is free software. It comes without any warranty, to the extent
# permitted by applicable law. You can redistribute it and/or modify it under
# the terms of the Do What The Fuck You Want To Public License (WTFPL), Version
# 2, as published by Sam Hocevar. See http://sam.zoy.org/wtfpl/COPYING for more
# details.
from subprocess import Popen, PIPE
import sys
import re
import json
import time
I3STATUS_CMD = "i3status"
LED_STATUSES_CMD = 'xset q | grep "LED mask"'
LED_MASKS = [
("caps", 0b0000000001, "CAPS", "#DC322F"),
("num", 0b0000000010, "NUM", "#859900"),
("scroll", 0b0000000100, "SCROLL", "#2AA198"),
("altgr", 0b1111101000, "ALTGR", "#B58900"),
]
def get_led_statuses():
""" Return a list of dictionaries representing the current keyboard LED
statuses """
try:
p = Popen(LED_STATUSES_CMD, stdout=PIPE, shell=True)
mask = re.search(r"[0-9]{8}", p.stdout.read())
if mask:
v = int(mask.group(0))
return [to_dict(n, t, c) for n, m, t, c in reversed(LED_MASKS) if v & m]
except Exception:
return ""
def to_dict(name, text, color):
""" Returns a dictionary with given information """
return {"full_text": text, "name": name, "color": color}
def print_line(message):
""" Non-buffered printing to stdout. """
sys.stdout.write(message + "\n")
sys.stdout.flush()
def read_line(process):
""" Interrupted respecting reader for stdin. """
try:
line = process.stdout.readline().strip()
if not line:
sys.exit(3)
return line
# exit on ctrl-c
except KeyboardInterrupt:
sys.exit()
if __name__ == "__main__":
p = Popen(I3STATUS_CMD, stdout=PIPE, shell=True, universal_newlines=True)
# waiting 1 second to get the first lines
time.sleep(1)
if p.poll() is None:
# Skip the first line which contains the version header.
print_line(read_line(p))
# The second line contains the start of the infinite array.
print_line(read_line(p))
while p.poll() is None:
line, prefix = read_line(p), ""
# ignore comma at start of lines
if line.startswith(","):
line, prefix = line[1:], ","
# prepend led statuses
j = json.loads(line)
leds = get_led_statuses()
[(lambda x: j.insert(0, x))(x) for x in leds]
# and echo back new encoded json
print_line(prefix + json.dumps(j))