summaryrefslogtreecommitdiff
path: root/local/bin/bat-symbol
blob: 1ae28f845f276b8413ff8263dacb907ae29d5310 (plain)
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 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()