Copybara bot | ca5ce64 | 2024-11-08 17:38:08 +0100 | [diff] [blame^] | 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2016 James Murphy |
| 4 | # Licensed under the GPL version 2 only |
| 5 | # |
| 6 | # A battery indicator blocklet script for i3blocks |
| 7 | |
| 8 | from subprocess import check_output |
| 9 | import os |
| 10 | import re |
| 11 | |
| 12 | config = dict(os.environ) |
| 13 | status = check_output(['acpi'], universal_newlines=True) |
| 14 | |
| 15 | if not status: |
| 16 | # stands for no battery found |
| 17 | color = config.get("color_10", "red") |
| 18 | fulltext = "<span color='{}'><span font='FontAwesome'>\uf00d \uf240</span></span>".format(color) |
| 19 | percentleft = 100 |
| 20 | else: |
| 21 | # if there is more than one battery in one laptop, the percentage left is |
| 22 | # available for each battery separately, although state and remaining |
| 23 | # time for overall block is shown in the status of the first battery |
| 24 | batteries = status.split("\n") |
| 25 | state_batteries=[] |
| 26 | commasplitstatus_batteries=[] |
| 27 | percentleft_batteries=[] |
| 28 | time = "" |
| 29 | for battery in batteries: |
| 30 | if battery!='': |
| 31 | state_batteries.append(battery.split(": ")[1].split(", ")[0]) |
| 32 | commasplitstatus = battery.split(", ") |
| 33 | if not time: |
| 34 | time = commasplitstatus[-1].strip() |
| 35 | # check if it matches a time |
| 36 | time = re.match(r"(\d+):(\d+)", time) |
| 37 | if time: |
| 38 | time = ":".join(time.groups()) |
| 39 | timeleft = " ({})".format(time) |
| 40 | else: |
| 41 | timeleft = "" |
| 42 | |
| 43 | p = int(commasplitstatus[1].rstrip("%\n")) |
| 44 | if p>0: |
| 45 | percentleft_batteries.append(p) |
| 46 | commasplitstatus_batteries.append(commasplitstatus) |
| 47 | state = state_batteries[0] |
| 48 | commasplitstatus = commasplitstatus_batteries[0] |
| 49 | if percentleft_batteries: |
| 50 | percentleft = int(sum(percentleft_batteries)/len(percentleft_batteries)) |
| 51 | else: |
| 52 | percentleft = 0 |
| 53 | |
| 54 | # stands for charging |
| 55 | color = config.get("color_charging", "yellow") |
| 56 | FA_LIGHTNING = "<span color='{}'><span font='FontAwesome'>\uf0e7</span></span>".format(color) |
| 57 | |
| 58 | # stands for plugged in |
| 59 | FA_PLUG = "<span font='FontAwesome'>\uf1e6</span>" |
| 60 | |
| 61 | # stands for using battery |
| 62 | FA_BATTERY = "<span font='FontAwesome'>\uf240</span>" |
| 63 | |
| 64 | # stands for unknown status of battery |
| 65 | FA_QUESTION = "<span font='FontAwesome'>\uf128</span>" |
| 66 | |
| 67 | |
| 68 | if state == "Discharging": |
| 69 | fulltext = FA_BATTERY + " " |
| 70 | elif state == "Full": |
| 71 | fulltext = FA_PLUG + " " |
| 72 | timeleft = "" |
| 73 | elif state == "Unknown": |
| 74 | fulltext = FA_QUESTION + " " + FA_BATTERY + " " |
| 75 | timeleft = "" |
| 76 | else: |
| 77 | fulltext = FA_LIGHTNING + " " + FA_PLUG + " " |
| 78 | |
| 79 | def color(percent): |
| 80 | if percent < 10: |
| 81 | # exit code 33 will turn background red |
| 82 | return config.get("color_10", "#FFFFFF") |
| 83 | if percent < 20: |
| 84 | return config.get("color_20", "#FF3300") |
| 85 | if percent < 30: |
| 86 | return config.get("color_30", "#FF6600") |
| 87 | if percent < 40: |
| 88 | return config.get("color_40", "#FF9900") |
| 89 | if percent < 50: |
| 90 | return config.get("color_50", "#FFCC00") |
| 91 | if percent < 60: |
| 92 | return config.get("color_60", "#FFFF00") |
| 93 | if percent < 70: |
| 94 | return config.get("color_70", "#FFFF33") |
| 95 | if percent < 80: |
| 96 | return config.get("color_80", "#FFFF66") |
| 97 | return config.get("color_full", "#FFFFFF") |
| 98 | |
| 99 | form = '<span color="{}">{}%</span>' |
| 100 | fulltext += form.format(color(percentleft), percentleft) |
| 101 | fulltext += timeleft |
| 102 | |
| 103 | print(fulltext) |
| 104 | print(fulltext) |
| 105 | if percentleft < 10: |
| 106 | exit(33) |