dotfiles/local/bin/bat-symbol
2025-07-31 01:51:10 +03:30

41 lines
986 B
Python
Executable file

#!/usr/bin/env python3
def read_file(path):
try:
with open(path, "r") as f:
return f.read().strip()
except:
return None
def get_battery_info():
capacity = read_file("/sys/class/power_supply/BAT1/capacity")
status = read_file("/sys/class/power_supply/BAT1/status")
ac_online = read_file("/sys/class/power_supply/ACAD/online")
try:
percent = int(capacity)
except (TypeError, ValueError):
percent = None
charging = (status == "Charging") or (ac_online == "1")
return charging, percent
def print_symbol():
charging, percent = get_battery_info()
if percent is None:
print("[?]")
return
if charging and percent >= 90:
print("[++]")
elif charging and percent < 30:
print("[-+]")
elif charging:
print("[+]")
elif not charging and percent < 30:
print("[--]")
else:
print("[-]")
if __name__ == "__main__":
print_symbol()