battery.py
836 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
#!/usr/bin/env python3
import subprocess
bat_icons = [
"\uf58d", # 0%
"\uf579",
"\uf57a",
"\uf57b",
"\uf57c",
"\uf57d", # 50%
"\uf57e",
"\uf57f",
"\uf580",
"\uf581",
"\uf578", # 100%
]
# bat_alert = f582
# bat_plus = f58e
# bat_minus = f58b
# bat_unknown = f590
# bat_charging = f583
out = str(
subprocess.run(["sysctl", "hw.acpi.battery"], capture_output=True).stdout, "UTF-8"
)
bat = dict(line.split(": ") for line in out.splitlines())
state = int(bat["hw.acpi.battery.state"])
life = int(bat["hw.acpi.battery.life"])
if state == 0: # fully charged
print(f"\uf583 Full")
elif state == 1: # discharging
print(f"{bat_icons[life // 10]} {life}%")
elif state == 2: # charging
print(f"\uf583 {life}%")
elif state == 7: # disconnected
print("\uf590 Disconnected")