commit b7c58a52c0de39b53c9b5f417889a5886d324bfa
parent 03e7bd719e32f22e7a0c41f1987dc30308703fac
Author: sam-barr <samfbarr@outlook.com>
Date: Sat, 9 May 2020 09:21:15 -0500
Merge pull request #61 from srithon/master
Add Support for Battery Field
Diffstat:
5 files changed, 63 insertions(+), 13 deletions(-)
diff --git a/Makefile b/Makefile
@@ -8,7 +8,8 @@ clean:
rm -f paleofetch $(CACHE)/paleofetch
paleofetch: paleofetch.c paleofetch.h config.h
- $(CC) paleofetch.c -o paleofetch $(CFLAGS)
+ $(eval battery_path := $(shell ./config_scripts/battery_config.sh))
+ $(CC) paleofetch.c -o paleofetch $(CFLAGS) -D $(battery_path)
strip paleofetch
install: paleofetch
diff --git a/config.h b/config.h
@@ -3,25 +3,26 @@
#define CONFIG \
{ \
- /* name function cached */\
- { "", get_title, false }, \
- { "", get_bar, false }, \
- { "OS: ", get_os, true }, \
- { "Host: ", get_host, true }, \
- { "Kernel: ", get_kernel, true }, \
- { "Uptime: ", get_uptime, false }, \
+ /* name function cached */\
+ { "", get_title, false }, \
+ { "", get_bar, false }, \
+ { "OS: ", get_os, true }, \
+ { "Host: ", get_host, true }, \
+ { "Kernel: ", get_kernel, true }, \
+ { "Uptime: ", get_uptime, false }, \
+ { "Battery: ", get_battery_percentage, false }, \
SPACER \
{ "Packages: ", get_packages_pacman, false }, \
{ "Shell: ", get_shell, false }, \
{ "Resolution: ", get_resolution, false }, \
{ "Terminal: ", get_terminal, false }, \
SPACER \
- { "CPU: ", get_cpu, true }, \
- { "GPU: ", get_gpu1, true }, \
- { "Memory: ", get_memory, false }, \
+ { "CPU: ", get_cpu, true }, \
+ { "GPU: ", get_gpu1, true }, \
+ { "Memory: ", get_memory, false }, \
SPACER \
- { "", get_colors1, false }, \
- { "", get_colors2, false }, \
+ { "", get_colors1, false }, \
+ { "", get_colors2, false }, \
}
#define CPU_CONFIG \
diff --git a/config_scripts/battery_config.sh b/config_scripts/battery_config.sh
@@ -0,0 +1,5 @@
+#!/bin/sh
+
+BATTERY_DIRECTORY=`ls /sys/class/power_supply | grep -i "^bat" | head -n 1`
+
+echo "BATTERY_DIRECTORY='\"/sys/class/power_supply/$BATTERY_DIRECTORY\"'"
diff --git a/paleofetch.c b/paleofetch.c
@@ -58,6 +58,17 @@ void remove_newline(char *s) {
}
/*
+ * Replaces the first newline character with null terminator
+ * and returns the length of the string
+ */
+int remove_newline_get_length(char *s) {
+ int i;
+ for (i = 0; *s != '\0' && *s != '\n'; s++, i++);
+ *s = '\0';
+ return i;
+}
+
+/*
* Cleans up repeated spaces in a string
* Trim spaces at the front of a string
*/
@@ -221,6 +232,37 @@ static char *get_uptime() {
return uptime;
}
+// returns "<Battery Percentage>% [<Charging | Discharging | Unknown>]"
+// Credit: allisio - https://gist.github.com/allisio/1e850b93c81150124c2634716fbc4815
+static char *get_battery_percentage() {
+ int battery_capacity;
+ FILE *capacity_file, *status_file;
+ char battery_status[12] = "Unknown";
+
+ if ((capacity_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) == NULL) {
+ status = ENOENT;
+ halt_and_catch_fire("Unable to get battery information");
+ }
+
+ fscanf(capacity_file, "%d", &battery_capacity);
+ fclose(capacity_file);
+
+ if ((status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) {
+ fscanf(status_file, "%s", battery_status);
+ fclose(status_file);
+ }
+
+ // max length of resulting string is 19
+ // one byte for padding incase there is a newline
+ // 100% [Discharging]
+ // 1234567890123456789
+ char *battery = malloc(20);
+
+ snprintf(battery, 20, "%d%% [%s]", battery_capacity, battery_status);
+
+ return battery;
+}
+
static char *get_packages(const char* dirname, const char* pacname, int num_extraneous) {
int num_packages = 0;
DIR * dirp;
diff --git a/paleofetch.h b/paleofetch.h
@@ -7,6 +7,7 @@ static char *get_title(),
*get_kernel(),
*get_host(),
*get_uptime(),
+ *get_battery_percentage(),
*get_packages_pacman(),
*get_shell(),
*get_resolution(),