-
Notifications
You must be signed in to change notification settings - Fork 176
Expand file tree
/
Copy pathbattery_indicator.py
More file actions
41 lines (30 loc) · 948 Bytes
/
battery_indicator.py
File metadata and controls
41 lines (30 loc) · 948 Bytes
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
#!/usr/bin/env python
# coding=UTF-8
import math
import subprocess
p = subprocess.Popen(["ioreg", "-rc", "AppleSmartBattery"],
stdout=subprocess.PIPE)
output = p.communicate()[0]
o_max = [l for l in output.splitlines() if 'MaxCapacity' in l][0]
o_cur = [l for l in output.splitlines() if 'CurrentCapacity' in l][0]
b_max = float(o_max.rpartition('=')[-1].strip())
b_cur = float(o_cur.rpartition('=')[-1].strip())
charge = b_cur / b_max
charge_threshold = int(math.ceil(10 * charge))
# Output
total_slots, slots = 10, []
filled = int(math.ceil(charge_threshold * (total_slots / 10.0))) * u'▸'
empty = (total_slots - len(filled)) * u'▹'
out = (filled + empty).encode('utf-8')
import sys
green = '\033[01;32m'
red = '\033[01;31m'
yello = '\033[01;33m'
reset = '\033[00m'
color_out = (
green if len(filled) > 6
else yello if len(filled) > 4
else red
)
out = color_out + out + reset
sys.stdout.write(out)