From 6d15bf5b9e54046cb7f2b2a4854b5b07bfa526c3 Mon Sep 17 00:00:00 2001 From: Your Name Date: Wed, 3 Jan 2024 17:46:33 +0300 Subject: [PATCH] add GetBattery --- config.h | 24 ++++++++++++++++++------ include/fetch.h | 1 + src/fetch.c | 34 ++++++++++++++++++++++++++++++++++ themes/default.h | 6 ++++++ themes/tree.h | 8 +++++++- 5 files changed, 66 insertions(+), 7 deletions(-) diff --git a/config.h b/config.h index 34e2c86..96c980f 100644 --- a/config.h +++ b/config.h @@ -9,8 +9,13 @@ typedef struct { } PARAMETR; /* Config, EDIT THERE */ -#define COLORS " \033[31mx \033[32mx \033[33mx \033[34mx \033[35mx \033[0m" +#define COLORS "\033[31m<> \033[32m<> \033[33m<> \033[34m<> \033[35m<> \033[36m<>\033[0m" #define FONT_COLOR "\033[1;37m" +#define BAR_WIDTH 10 + +/* Full path to capacity file */ +/* Example: /sys/class/power_supply/battery/capacity */ +#define BATT_NAME NULL /* #define PRINT_TOTAL_MEM */ /* #define PRINT_USER_MEM */ @@ -18,11 +23,18 @@ typedef struct { #ifdef _MAIN_H PARAMETR CONFIG[] = { - {" ", Blank}, - {"\033[1;31m  ", PrintOs}, - {"  ", GetKernel}, - {"  ", GetUptime}, - {"  ", GetPkg}, + {" os ", PrintOs}, + {" kr ", GetKernel}, + {" usr ", GetUser}, + {" upt ", GetUptime}, + {" arch ", GetArch}, + {"shell ", GetShell}, + {" pkgs ", GetPkg}, + {" mem ", GetMem}, + {"model ", GetModel}, + {" host ", GetHostname}, + {" lAVG ", GetAVG}, + {" batt ", GetBattery}, {" ", Blank}, {COLORS, Blank} }; diff --git a/include/fetch.h b/include/fetch.h index 6d9d6ad..eba8dcd 100644 --- a/include/fetch.h +++ b/include/fetch.h @@ -46,5 +46,6 @@ int GetMem(const char *title, const FETCH fetch); int GetModel(const char *title, const FETCH fetch); int GetHostname(const char *title, const FETCH fetch); int GetAVG(const char *title, const FETCH fetch); +int GetBattery(const char *title, const FETCH fetch); #endif diff --git a/src/fetch.c b/src/fetch.c index 69269c2..d93fc7a 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -256,3 +256,37 @@ int GetAVG(const char *title, const FETCH fetch) { #endif } + +void DrawBar(char *color, int cur, int max) { + printf("["); + + int bar = max / BAR_WIDTH; + for (int i = 0; i < BAR_WIDTH; i++) { + if (i * bar < cur) + printf("%s-\033[0m", color); + + else + printf("\033[1;30m-\033[0m"); + } + + printf("]"); +} + +int GetBattery(const char *title, const FETCH fetch) { + if (BATT_NAME == NULL) + return 1; + + FILE *fp = fopen(BATT_NAME, "r"); + if (fp == NULL) + return 1; + + int capacity = 0; + if (fscanf(fp, "%d", &capacity) < 0) + return 1; + + printf("%s%s%s", fetch.color, title, fetch.font_color); + DrawBar(fetch.color, capacity, 100); + + return 0; +} + diff --git a/themes/default.h b/themes/default.h index 8d93cf3..96c980f 100644 --- a/themes/default.h +++ b/themes/default.h @@ -11,6 +11,11 @@ typedef struct { /* Config, EDIT THERE */ #define COLORS "\033[31m<> \033[32m<> \033[33m<> \033[34m<> \033[35m<> \033[36m<>\033[0m" #define FONT_COLOR "\033[1;37m" +#define BAR_WIDTH 10 + +/* Full path to capacity file */ +/* Example: /sys/class/power_supply/battery/capacity */ +#define BATT_NAME NULL /* #define PRINT_TOTAL_MEM */ /* #define PRINT_USER_MEM */ @@ -29,6 +34,7 @@ PARAMETR CONFIG[] = { {"model ", GetModel}, {" host ", GetHostname}, {" lAVG ", GetAVG}, + {" batt ", GetBattery}, {" ", Blank}, {COLORS, Blank} }; diff --git a/themes/tree.h b/themes/tree.h index d18b871..391d6b9 100644 --- a/themes/tree.h +++ b/themes/tree.h @@ -11,6 +11,11 @@ typedef struct { /* Config, EDIT THERE */ #define COLORS " \033[41;31m++ \033[42;32m++ \033[43;33m++ \033[44;34m++ \033[45;35m++ \033[46;36m++\033[0m" #define FONT_COLOR "\033[1;37m" +#define BAR_WIDTH 10 + +/* Full path to capacity file */ +/* Example: /sys/class/power_supply/battery/capacity */ +#define BATT_NAME NULL /* #define PRINT_TOTAL_MEM */ /* #define PRINT_USER_MEM */ @@ -25,12 +30,13 @@ PARAMETR CONFIG[] = { {" - mod ", GetModel}, {" - ht ", GetHostname}, {" ", Blank}, - {" - other: ", Blank}, + {" - info: ", Blank}, {" - os ", PrintOs}, {" - shl ", GetShell}, {" - pkg ", GetPkg}, {" - up ", GetUptime}, {" - avg ", GetAVG}, + {" - bat ", GetBattery}, {" ", Blank}, {COLORS, Blank} };