slstatus

My fork of https://tools.suckless.org/slstatus/
git clone https://git.inz.fi/slstatus
Log | Files | Refs | README | LICENSE

commit 15bce67ce796a326f6640053545d55411f9920f2
parent a4d910945cb842864155e512e9a7641600b3c377
Author: Santtu Lakkala <inz@inz.fi>
Date:   Sat, 26 Feb 2022 11:41:47 +0200

Add battery icon/emoji

Diffstat:
Mcomponents/battery.c | 44+++++++++++++++++++++++++++++++++++++-------
Mconfig.mk | 2+-
Mslstatus.h | 1+
3 files changed, 39 insertions(+), 8 deletions(-)

diff --git a/components/battery.c b/components/battery.c @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ #include <stdio.h> #include <string.h> +#include <grapheme.h> #include "../util.h" @@ -32,8 +33,8 @@ return NULL; } - const char * - battery_perc(const char *bat) + static int + _battery_perc(const char *bat) { int perc; char path[PATH_MAX]; @@ -41,10 +42,10 @@ if (bat) { if (esnprintf(path, sizeof(path), "/sys/class/power_supply/%s/capacity", bat) < 0) { - return NULL; + return -1; } if (pscanf(path, "%d", &perc) != 1) { - return NULL; + return -1; } } else { int bi; @@ -59,22 +60,51 @@ path, sizeof(path), bi)) break; if (pscanf(path, "%d", &f) != 1) - return NULL; + return -1; if (!pick("/sys/class/power_supply/BAT%d/charge_now", "/sys/class/power_supply/BAT%d/energy_now", path, sizeof(path), bi)) break; if (pscanf(path, "%d", &n) != 1) - return NULL; + return -1; ef += f; en += n; } if (ef == 0) - return NULL; + return -1; perc = (en + ef / 200) / (ef / 100); } + return perc; + } + + const char * + battery_icon(const char *str) + { + int perc = _battery_perc(NULL); + size_t p; + size_t l; + int i = 0; + + for (p = 0; str[p]; p += l, i++) + l = grapheme_next_character_break(str + p, SIZE_MAX); + + i = i * perc / 101; + + for (p = 0; i--; p += l) + l = grapheme_next_character_break(str + p, SIZE_MAX); + l = grapheme_next_character_break(str + p, SIZE_MAX); + + return bprintf("%.*s", (int)l, str + p); + } + + const char * + battery_perc(const char *bat) + { + int perc = _battery_perc(bat); + if (perc < 0) + return NULL; return bprintf("%d", perc); } diff --git a/config.mk b/config.mk @@ -16,7 +16,7 @@ CFLAGS = -std=c99 -pedantic -Wall -Wextra -Os LDFLAGS = -L$(X11LIB) -s # OpenBSD: add -lsndio # FreeBSD: add -lkvm -LDLIBS = -lX11 +LDLIBS = -lX11 -lgrapheme # compiler and linker CC = cc diff --git a/slstatus.h b/slstatus.h @@ -1,6 +1,7 @@ /* See LICENSE file for copyright and license details. */ /* battery */ +const char *battery_icon(const char *); const char *battery_perc(const char *); const char *battery_state(const char *); const char *battery_remaining(const char *);