Introduction
Introduction Statistics Contact Development Disclaimer Help
[slstatus][patch][dyn-battery]Created dyn-battery patch, for displaying status …
git clone git://git.suckless.org/sites
Log
Files
Refs
---
commit 28b1a4a1dabbf5d8832f69b107fae2389269dbbc
parent d482535ccc73bd8da46dba58cc952bda9a108508
Author: Madison Lynch <[email protected]>
Date: Mon, 14 Apr 2025 21:14:40 -0700
[slstatus][patch][dyn-battery]Created dyn-battery patch, for displaying status
of multiple batteries
Diffstat:
A tools.suckless.org/slstatus/patche… | 0
A tools.suckless.org/slstatus/patche… | 22 ++++++++++++++++++++++
A tools.suckless.org/slstatus/patche… | 141 +++++++++++++++++++++++++++…
3 files changed, 163 insertions(+), 0 deletions(-)
---
diff --git a/tools.suckless.org/slstatus/patches/dyn-battery/image.png b/tools.…
Binary files differ.
diff --git a/tools.suckless.org/slstatus/patches/dyn-battery/index.md b/tools.s…
@@ -0,0 +1,21 @@
+dyn-battery
+===========
+
+Description
+-----------
+This patch implements a function that displays the status and capacity of
+all present batteries (only supported for Linux).
+
+The order of the format identifiers is hard-coded: battery number (%u),
+state (%s), capacity (%s). Example:
+"[BAT%u: %s%s]" -> [BAT0: +58]
+
+![Example](image.png)
+
+Download
+--------
+* [slstatus-dyn_battery-20250414-f68f492.diff](slstatus-dyn_battery-20250414-f…
+
+Author
+------
+* Madison Lynch <[email protected]>
+\ No newline at end of file
diff --git a/tools.suckless.org/slstatus/patches/dyn-battery/slstatus-dyn_batte…
@@ -0,0 +1,141 @@
+From b96a72c277a72c77feaf94fac679f3ea87f78ea6 Mon Sep 17 00:00:00 2001
+From: Madison Lynch <[email protected]>
+Date: Mon, 14 Apr 2025 21:04:23 -0700
+Subject: [PATCH] Created dyn-battery patch, for displaying status of multiple
+ batteries
+
+---
+ Makefile | 1 +
+ components/dyn_battery.c | 77 ++++++++++++++++++++++++++++++++++++++++
+ config.def.h | 4 +++
+ slstatus.h | 3 ++
+ 4 files changed, 85 insertions(+)
+ create mode 100644 components/dyn_battery.c
+
+diff --git a/Makefile b/Makefile
+index 7a18274..d1b24dd 100644
+--- a/Makefile
++++ b/Makefile
+@@ -11,6 +11,7 @@ COM =\
+ components/cpu\
+ components/datetime\
+ components/disk\
++ components/dyn_battery\
+ components/entropy\
+ components/hostname\
+ components/ip\
+diff --git a/components/dyn_battery.c b/components/dyn_battery.c
+new file mode 100644
+index 0000000..2e58351
+--- /dev/null
++++ b/components/dyn_battery.c
+@@ -0,0 +1,77 @@
++/* Written by Madison Lynch <[email protected]> */
++/* Only Linux is supported */
++#include <stdio.h>
++#include <stdlib.h>
++#include <string.h>
++#include <dirent.h>
++
++#include "../slstatus.h"
++
++#define BAT_PREFIX "BAT"
++#define BAT_DIR "/sys/class/power_supply"
++
++/**
++* Counts number of batteries detected by system.
++*
++* @return unsigned integer denoting the number of detected batteries.
++* @author Madison Lynch
++*/
++static unsigned int
++battery_count(void) {
++ DIR *dir = opendir(BAT_DIR);
++ unsigned int bat_c = 0;
++
++ struct dirent *entry;
++ while((entry = readdir(dir)))
++ if(strlen(entry->d_name) > 3)
++ if(strncmp(entry->d_name, BAT_PREFIX, 3) == 0)
++ bat_c++;
++
++ (void) closedir(dir);
++ return bat_c;
++}
++
++/**
++* Displays the status and capacity of a dynamic amount of batteries (i.e.
++* laptop may have secondary external battery).
++*
++* @param fmt format string to use for each battery display. ordered key:
++* %u: battery number || %s: battery state || %s battery capacity
++* @return string containing the status and capacities of all detected batteri…
++* @author Madison Lynch
++*/
++const char *
++dyn_battery(const char *fmt) {
++ const size_t fmt_s = strlen(fmt);
++ const unsigned int bat_c = battery_count();
++
++ // Extra byte in calloc() for null byte
++ char *output = (char *)calloc(fmt_s * bat_c + 1, sizeof(char));
++ unsigned int displacement = 0; // For appending battery displays
++ for(unsigned int i=0; i<bat_c; i++) {
++ char bat[7]; // "BAT" = 3 + <=3 digit number + null byte
++ sprintf(bat, "BAT%u", i);
++
++ // Add battery display to final string to be returned
++ sprintf(
++ output + displacement,
++ fmt,
++ i,
++ battery_state(bat),
++ battery_perc(bat)
++ );
++ displacement = strlen(output);
++
++ // Add space between battery displays
++ *(output + displacement) = ' ';
++ displacement++;
++ }
++
++ // Remove extra space after last battery display
++ output[--displacement] = 0x00;
++
++ return output;
++}
++
++#undef BAT_DIR
++#undef BAT_PREFIX
+diff --git a/config.def.h b/config.def.h
+index d805331..967d63d 100644
+--- a/config.def.h
++++ b/config.def.h
+@@ -26,6 +26,10 @@ static const char unknown_str[] = "n/a";
+ * disk_perc disk usage in percent mountpoint path (/)
+ * disk_total total disk space in GB mountpoint path (/)
+ * disk_used used disk space in GB mountpoint path (/)
++ * dyn_battery displays the name, state and format string (%u, %s,
++ * capacity of all detected %s). order is importan…
++ * batteries and matches descriptio…
++ * (Linux only)
+ * entropy available entropy NULL
+ * gid GID of current user NULL
+ * hostname hostname NULL
+diff --git a/slstatus.h b/slstatus.h
+index 8ef5874..44a4b61 100644
+--- a/slstatus.h
++++ b/slstatus.h
+@@ -21,6 +21,9 @@ const char *disk_perc(const char *path);
+ const char *disk_total(const char *path);
+ const char *disk_used(const char *path);
+
++/* dyn_battery */
++const char *dyn_battery(const char *fmt);
++
+ /* entropy */
+ const char *entropy(const char *unused);
+
+--
+2.49.0
+
You are viewing proxied material from suckless.org. The copyright of proxied material belongs to its original authors. Any comments or complaints in relation to proxied material should be directed to the original authors of the content concerned. Please see the disclaimer for more details.