From ce4638ed71abe9aaf53d8d17e55ae34cb8c47445 Mon Sep 17 00:00:00 2001 From: Miguel BarĂ£o Date: Fri, 24 Jun 2022 16:10:51 +0100 Subject: [PATCH] Initial commit --- battery | Bin 0 -> 15432 bytes battery.c | 46 ++++++++++++++++++++++++++++++++++++++++++++++ battery.py | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 85 insertions(+), 0 deletions(-) create mode 100755 battery create mode 100644 battery.c create mode 100755 battery.py diff --git a/battery b/battery new file mode 100755 index 0000000..d6aab96 Binary files /dev/null and b/battery differ diff --git a/battery.c b/battery.c new file mode 100644 index 0000000..6239afc --- /dev/null +++ b/battery.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include + +const char *bat_icons[] = { + "\uf58d", /* 0% */ + "\uf579", + "\uf57a", + "\uf57b", + "\uf57c", + "\uf57d", /* 50% */ + "\uf57e", + "\uf57f", + "\uf580", + "\uf581", + "\uf578", /* 100% */ +}; + +int main() { + int life, state; + size_t len = sizeof(int); + + if (sysctlbyname("hw.acpi.battery.state", &state, &len, NULL, 0) || + sysctlbyname("hw.acpi.battery.life", &life, &len, NULL, 0)) { + perror("No battery?"); + exit(EXIT_FAILURE); + } + + switch (state) { + case 0: /* fully charged */ + printf("\uf583 Full"); + break; + case 1: /* discharging */ + printf("%s %d%%\n", bat_icons[life / 10], life); + break; + case 2: /* charging */ + printf("\uf583 %d\n", life); + break; + case 7: /* disconnected */ + printf("\uf590 Disconnected"); + break; + } + + return 0; +} diff --git a/battery.py b/battery.py new file mode 100755 index 0000000..c6628eb --- /dev/null +++ b/battery.py @@ -0,0 +1,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") -- libgit2 0.21.2