diff options
Diffstat (limited to 'minibattery.c')
| -rw-r--r-- | minibattery.c | 54 | 
1 files changed, 54 insertions, 0 deletions
| diff --git a/minibattery.c b/minibattery.c new file mode 100644 index 0000000..c30712c --- /dev/null +++ b/minibattery.c @@ -0,0 +1,54 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <unistd.h> + +#define STRLEN_CAPACITY 4 +#define STRLEN_STATUS 16 + +#define COLOR_FULL "#839496" +#define COLOR_LOW "#b58900" +#define COLOR_CRITICAL "#dc322f" + +void dump_file(const char *path, size_t size, char *out) { +    FILE *file = fopen(path, "r"); +    fgets(out, size, file); +    fclose(file); +} + +int main() { +    char capacity[STRLEN_CAPACITY]; +    char status[STRLEN_STATUS]; + +    for (;;) { +        dump_file("/sys/class/power_supply/BAT0/capacity", STRLEN_CAPACITY, capacity); +        dump_file("/sys/class/power_supply/BAT0/status", STRLEN_STATUS, status); + +        // select color based on charge level +        int capacity_int = atoi(capacity); +        const char *color = NULL; + +        if (capacity_int <= 10) { +            color = COLOR_CRITICAL; +        } else if (capacity_int <= 20) { +            color = COLOR_LOW; +        } else { +            color = COLOR_FULL; +        } + +        // select icon based on whether AC is plugged in +        const char *icon = NULL; + +        if (strstr(status, "Discharging")) { +            icon = "f0079"; +        } else { +            icon = "f06a5"; +        } + +        printf("<span color=\"%s\"><span font=\"Symbols Nerd Font\">&#x%s;</span> %d%</span>\n", color, icon, capacity_int); +        fflush(stdout); +        sleep(1); +    } + +    return 0; +} | 
