battery.py 836 Bytes
#!/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")