From 86217e323e71462dca97a5ee072027e8d0a7112f Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Mon, 11 May 2020 12:42:56 -0400 Subject: [PATCH 01/18] Allow functions to modify labels --- paleofetch.c | 65 ++++++++++++++++++++++++++-------------------------- paleofetch.h | 40 ++++++++++++++++---------------- 2 files changed, 52 insertions(+), 53 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 72ce826..a2715a0 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -122,7 +122,7 @@ void replace_substring(char *str, const char *sub_str, const char *repl_str, siz strcpy(start + repl_len, buffer); } -static char *get_title() { +static char *get_title(char** label) { // reduce the maximum size for these, so that we don't over-fill the title string char hostname[BUF_SIZE / 3]; status = gethostname(hostname, BUF_SIZE / 3); @@ -140,7 +140,7 @@ static char *get_title() { return title; } -static char *get_bar() { +static char *get_bar(char** label) { char *bar = malloc(BUF_SIZE); char *s = bar; for(int i = 0; i < title_length; i++) *(s++) = '-'; @@ -148,7 +148,7 @@ static char *get_bar() { return bar; } -static char *get_os() { +static char *get_os(char** label) { char *os = malloc(BUF_SIZE), *name = malloc(BUF_SIZE), *line = NULL; @@ -171,13 +171,13 @@ static char *get_os() { return os; } -static char *get_kernel() { +static char *get_kernel(char** label) { char *kernel = malloc(BUF_SIZE); strncpy(kernel, uname_info.release, BUF_SIZE); return kernel; } -static char *get_host() { +static char *get_host(char** label) { char *host = malloc(BUF_SIZE), buffer[BUF_SIZE/2]; FILE *product_name, *product_version, *model; @@ -210,7 +210,7 @@ static char *get_host() { return NULL; } -static char *get_uptime() { +static char *get_uptime(char** label) { long seconds = my_sysinfo.uptime; struct { char *name; int secs; } units[] = { { "day", 60 * 60 * 24 }, @@ -234,7 +234,7 @@ static char *get_uptime() { // returns "% []" // Credit: allisio - https://gist.github.com/allisio/1e850b93c81150124c2634716fbc4815 -static char *get_battery_percentage() { +static char *get_battery_percentage(char** label) { int battery_capacity; FILE *capacity_file, *status_file; char battery_status[12] = "Unknown"; @@ -288,11 +288,11 @@ static char *get_packages(const char* dirname, const char* pacname, int num_extr return packages; } -static char *get_packages_pacman() { +static char *get_packages_pacman(char** label) { return get_packages("/var/lib/pacman/local", "pacman", 0); } -static char *get_shell() { +static char *get_shell(char** label) { char *shell = malloc(BUF_SIZE); char *shell_path = getenv("SHELL"); char *shell_name = strrchr(getenv("SHELL"), '/'); @@ -305,7 +305,7 @@ static char *get_shell() { return shell; } -static char *get_resolution() { +static char *get_resolution(char** label) { int screen, width, height; char *resolution = malloc(BUF_SIZE); @@ -361,7 +361,7 @@ static char *get_resolution() { return resolution; } -static char *get_terminal() { +static char *get_terminal(char** label) { unsigned char *prop; char *terminal = malloc(BUF_SIZE); @@ -399,7 +399,7 @@ static char *get_terminal() { return terminal; } -static char *get_cpu() { +static char *get_cpu(char** label) { FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); /* read from cpu info */ if(cpuinfo == NULL) { status = -1; @@ -533,15 +533,15 @@ static char *find_gpu(int index) { return gpu; } -static char *get_gpu1() { +static char *get_gpu1(char** label) { return find_gpu(0); } -static char *get_gpu2() { +static char *get_gpu2(char** label) { return find_gpu(1); } -static char *get_memory() { +static char *get_memory(char** label) { int total_memory, used_memory; int total, shared, memfree, buffers, cached, reclaimable; @@ -597,15 +597,15 @@ static char *get_disk_usage(const char *folder) { return disk_usage; } -static char *get_disk_usage_root() { +static char *get_disk_usage_root(char** label) { return get_disk_usage("/"); } -static char *get_disk_usage_home() { +static char *get_disk_usage_home(char** label) { return get_disk_usage("/home"); } -static char *get_colors1() { +static char *get_colors1(char** label) { char *colors1 = malloc(BUF_SIZE); char *s = colors1; @@ -618,7 +618,7 @@ static char *get_colors1() { return colors1; } -static char *get_colors2() { +static char *get_colors2(char** label) { char *colors2 = malloc(BUF_SIZE); char *s = colors2; @@ -631,7 +631,7 @@ static char *get_colors2() { return colors2; } -static char *spacer() { +static char *spacer(char** label) { return calloc(1, 1); // freeable, null-terminated string of length 1 } @@ -665,18 +665,18 @@ char *search_cache(char *cache_data, char *label) { return buf; } -char *get_value(struct conf c, int read_cache, char *cache_data) { +char *get_value(struct conf *c, int read_cache, char *cache_data) { char *value; // If the user's config specifies that this value should be cached - if(c.cached && read_cache) // and we have a cache to read from - value = search_cache(cache_data, c.label); // grab it from the cache + if(c->cached && read_cache) // and we have a cache to read from + value = search_cache(cache_data, c->label); // grab it from the cache else { // Otherwise, call the associated function to get the value - value = c.function(); - if(c.cached) { // and append it to our cache data if appropriate + value = c->function(&c->label); + if(c->cached) { // and append it to our cache data if appropriate char *buf = malloc(BUF_SIZE); - sprintf(buf, "%s=%s;", c.label, value); + sprintf(buf, "%s=%s;", c->label, value); strcat(cache_data, buf); free(buf); } @@ -720,18 +720,17 @@ int main(int argc, char *argv[]) { printf(COLOR"%s\n", LOGO[i]); else { // Otherwise, we've got a bit of work to do. - char *label = config[i+offset].label, - *value = get_value(config[i+offset], read_cache, cache_data); + struct conf c = config[i + offset]; + char *value = get_value(&c, read_cache, cache_data); if (strcmp(value, "") != 0) { // check if value is an empty string - printf(COLOR"%s%s\e[0m%s\n", LOGO[i], label, value); // just print if not empty + printf(COLOR"%s%s\e[0m%s\n", LOGO[i], c.label, value); // just print if not empty } else { - if (strcmp(label, "") != 0) { // check if label is empty, otherwise it's a spacer + if (strcmp(c.label, "") != 0) { // check if label is empty, otherwise it's a spacer ++offset; // print next line of information free(value); // free memory allocated for empty value - label = config[i+offset].label; // read new label and value - value = get_value(config[i+offset], read_cache, cache_data); + value = get_value(&config[i+offset], read_cache, cache_data); } - printf(COLOR"%s%s\e[0m%s\n", LOGO[i], label, value); + printf(COLOR"%s%s\e[0m%s\n", LOGO[i], c.label, value); } free(value); diff --git a/paleofetch.h b/paleofetch.h index 0e4578f..1498545 100644 --- a/paleofetch.h +++ b/paleofetch.h @@ -1,26 +1,26 @@ /* Forward-declare our functions so users can mention them in their * configs at the top of the file rather than near the bottom. */ -static char *get_title(), - *get_bar(), - *get_os(), - *get_kernel(), - *get_host(), - *get_uptime(), - *get_battery_percentage(), - *get_packages_pacman(), - *get_shell(), - *get_resolution(), - *get_terminal(), - *get_cpu(), - *get_gpu1(), - *get_gpu2(), - *get_memory(), - *get_disk_usage_root(), - *get_disk_usage_home(), - *get_colors1(), - *get_colors2(), - *spacer(); +static char *get_title(char** label), + *get_bar(char** label), + *get_os(char** label), + *get_kernel(char** label), + *get_host(char** label), + *get_uptime(char** label), + *get_battery_percentage(char** label), + *get_packages_pacman(char** label), + *get_shell(char** label), + *get_resolution(char** label), + *get_terminal(char** label), + *get_cpu(char** label), + *get_gpu1(char** label), + *get_gpu2(char** label), + *get_memory(char** label), + *get_disk_usage_root(char** label), + *get_disk_usage_home(char** label), + *get_colors1(char** label), + *get_colors2(char** label), + *spacer(char** label); #define SPACER {"", spacer, false}, #define REMOVE(A) { (A), NULL, sizeof(A) - 1 , 0 } From 9ae29d30169d9562d1e9f218f658ab8b3841b706 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Mon, 11 May 2020 12:44:07 -0400 Subject: [PATCH 02/18] Change label to name of battery in battery function --- paleofetch.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index a2715a0..95d189e 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -234,9 +234,11 @@ static char *get_uptime(char** label) { // returns "% []" // Credit: allisio - https://gist.github.com/allisio/1e850b93c81150124c2634716fbc4815 -static char *get_battery_percentage(char** label) { +// returns percentage of the nth battery +// changes the contents of *label to the name of the battery +static char *get_nth_battery_percentage(int n, char** label) { int battery_capacity; - FILE *capacity_file, *status_file; + FILE *capacity_file, *status_file, *model_file; char battery_status[12] = "Unknown"; if ((capacity_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) == NULL) { @@ -260,9 +262,24 @@ static char *get_battery_percentage(char** label) { snprintf(battery, 20, "%d%% [%s]", battery_capacity, battery_status); + // get model name of battery + char *model_name = malloc(30); + + if ((model_file = fopen(BATTERY_DIRECTORY "/model_name", "r")) != NULL) { + fgets(model_name, 30, model_file); + remove_newline(model_name); + strcat(model_name, ": "); + *label = model_name; + } + return battery; } +static char *get_battery_percentage(char** label) +{ + return get_nth_battery_percentage(0, label); +} + static char *get_packages(const char* dirname, const char* pacname, int num_extraneous) { int num_packages = 0; DIR * dirp; From 631d4ac8f25c0ce2fc315168426e50f20747fd1e Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Tue, 12 May 2020 20:27:37 -0400 Subject: [PATCH 03/18] Write functions to get nth battery in list --- paleofetch.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/paleofetch.c b/paleofetch.c index 95d189e..7ffbd19 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -232,6 +232,70 @@ static char *get_uptime(char** label) { return uptime; } +static int directory_is_battery(const struct dirent *potential_battery) { + // filters out . and .. + if (potential_battery->d_name[0] == '.') + { + return 0; + } + else + { + // d_name can be up to 255 characters long + // 285 bytes are necessary to hold all possible strings + // in reality, the name of the battery folder probably wont be that long, + // but it's better to be safe + static char directory_buffer[BUF_SIZE * 2]; + snprintf(directory_buffer, sizeof(directory_buffer), "/sys/class/power_supply/%s/type", potential_battery->d_name); + + FILE* battery_type_file; + + if ((battery_type_file = fopen(directory_buffer, "r")) != NULL) + { + // longest possible string is "Battery", which is 7 characters long + // add 1 to buffer size for null terminating character + char battery_type[8]; + fscanf(battery_type_file, "%s", battery_type); + return strcmp(battery_type, "Battery") == 0; + } + else + { + return 0; + } + } +} + +static struct dirent *get_nth_battery(int n) +{ + static struct dirent **power_supply_directory = NULL; + static int num_dirs = -1; + + if (!power_supply_directory) { + num_dirs = scandir("/sys/class/power_supply/", &power_supply_directory, directory_is_battery, alphasort); + + /* for (int i = 0; i < num_dirs; i++) */ + /* printf("Directory name_: %s\n", power_supply_directory[i]->d_name); */ + /* printf("_!!\n"); */ + } + + /* printf("!!!\n"); */ + + /* struct dirent *potential_battery; */ + /* if ((potential_battery = readdir(power_supply_directory)) != NULL) */ + /* { */ + /* if (!directory_is_battery(potential_battery)) */ + /* { */ + /* free(potential_battery); */ + /* return get_next_battery(); */ + /* } */ + /* } */ + + + if (n < num_dirs) + return power_supply_directory[n]; + else + return NULL; +} + // returns "% []" // Credit: allisio - https://gist.github.com/allisio/1e850b93c81150124c2634716fbc4815 // returns percentage of the nth battery From f7cd366aaa7dc33aaa1cca8f5b3fcd70a0b51191 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Tue, 12 May 2020 20:28:45 -0400 Subject: [PATCH 04/18] Replace hardcoded battery path refs --- paleofetch.c | 29 +++++++++++++++++++++++++---- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 7ffbd19..a6c9e4a 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -301,11 +301,28 @@ static struct dirent *get_nth_battery(int n) // returns percentage of the nth battery // changes the contents of *label to the name of the battery static char *get_nth_battery_percentage(int n, char** label) { + + struct dirent *battery_directory = get_nth_battery(n); + /* if (battery_directory != NULL) */ + /* printf("Active battery: %s\n", battery_directory->d_name); */ + if (battery_directory == NULL) + return calloc(1, 1); + + /* DIR *power_supply_directory; */ + /* struct dirent *directory; */ + int battery_capacity; FILE *capacity_file, *status_file, *model_file; char battery_status[12] = "Unknown"; - - if ((capacity_file = fopen(BATTERY_DIRECTORY "/capacity", "r")) == NULL) { + + char file_path_buffer[BUF_SIZE]; + snprintf(file_path_buffer, sizeof(file_path_buffer), "/sys/class/power_supply/%s/", battery_directory->d_name); + + char *file_path_buffer_end = file_path_buffer + strlen(file_path_buffer); + + strcpy(file_path_buffer_end, "capacity"); + printf("%s\n", file_path_buffer); + if ((capacity_file = fopen(file_path_buffer, "r")) == NULL) { status = ENOENT; halt_and_catch_fire("Unable to get battery information"); } @@ -313,7 +330,9 @@ static char *get_nth_battery_percentage(int n, char** label) { fscanf(capacity_file, "%d", &battery_capacity); fclose(capacity_file); - if ((status_file = fopen(BATTERY_DIRECTORY "/status", "r")) != NULL) { + strcpy(file_path_buffer_end, "status"); + printf("%s\n", file_path_buffer); + if ((status_file = fopen(file_path_buffer, "r")) != NULL) { fscanf(status_file, "%s", battery_status); fclose(status_file); } @@ -329,7 +348,9 @@ static char *get_nth_battery_percentage(int n, char** label) { // get model name of battery char *model_name = malloc(30); - if ((model_file = fopen(BATTERY_DIRECTORY "/model_name", "r")) != NULL) { + strcpy(file_path_buffer_end, "model_name"); + printf("%s\n", file_path_buffer); + if ((model_file = fopen(file_path_buffer, "r")) != NULL) { fgets(model_name, 30, model_file); remove_newline(model_name); strcat(model_name, ": "); From 1caeb17ac39f2a9c8b8f1339dae966b71323a832 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Tue, 12 May 2020 20:33:33 -0400 Subject: [PATCH 05/18] Split get_battery_percentage function into 1 and 2 --- paleofetch.c | 7 ++++++- paleofetch.h | 3 ++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index a6c9e4a..dda44be 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -360,11 +360,16 @@ static char *get_nth_battery_percentage(int n, char** label) { return battery; } -static char *get_battery_percentage(char** label) +static char *get_battery_1_percentage(char** label) { return get_nth_battery_percentage(0, label); } +static char *get_battery_2_percentage(char** label) +{ + return get_nth_battery_percentage(1, label); +} + 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 index 1498545..7232957 100644 --- a/paleofetch.h +++ b/paleofetch.h @@ -7,7 +7,8 @@ static char *get_title(char** label), *get_kernel(char** label), *get_host(char** label), *get_uptime(char** label), - *get_battery_percentage(char** label), + *get_battery_1_percentage(char** label), + *get_battery_2_percentage(char** label), *get_packages_pacman(char** label), *get_shell(char** label), *get_resolution(char** label), From a8551e249351e4c886ad3ef8665b7c8419bd2cbc Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Tue, 12 May 2020 20:33:51 -0400 Subject: [PATCH 06/18] Reflect function change in config.h, fix spacing --- config.h | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/config.h b/config.h index 39c621c..a178e4b 100644 --- a/config.h +++ b/config.h @@ -3,26 +3,27 @@ #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 }, \ - { "Battery: ", get_battery_percentage, 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 1: ", get_battery_1_percentage, false }, \ + { "Battery 2: ", get_battery_2_percentage, false }, \ SPACER \ - { "Packages: ", get_packages_pacman, false }, \ - { "Shell: ", get_shell, false }, \ - { "Resolution: ", get_resolution, false }, \ - { "Terminal: ", get_terminal, false }, \ + { "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 \ From 6efdd48ec63f6e58d3907b6e35fdbcb045da2ba4 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Wed, 13 May 2020 00:23:22 -0400 Subject: [PATCH 07/18] Add support for batteries without percentages --- paleofetch.c | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index dda44be..0fb2e7a 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -311,7 +311,9 @@ static char *get_nth_battery_percentage(int n, char** label) { /* DIR *power_supply_directory; */ /* struct dirent *directory; */ - int battery_capacity; + // could not figure out possible values for this + char battery_capacity[12]; + FILE *capacity_file, *status_file, *model_file; char battery_status[12] = "Unknown"; @@ -320,14 +322,20 @@ static char *get_nth_battery_percentage(int n, char** label) { char *file_path_buffer_end = file_path_buffer + strlen(file_path_buffer); + bool capacity_is_percent = true; + strcpy(file_path_buffer_end, "capacity"); printf("%s\n", file_path_buffer); if ((capacity_file = fopen(file_path_buffer, "r")) == NULL) { - status = ENOENT; - halt_and_catch_fire("Unable to get battery information"); + strcpy(file_path_buffer_end, "capacity_level"); + if ((capacity_file = fopen(file_path_buffer, "r")) == NULL) { + status = ENOENT; + halt_and_catch_fire("Unable to get battery information"); + } + capacity_is_percent = false; } - fscanf(capacity_file, "%d", &battery_capacity); + fscanf(capacity_file, "%s", battery_capacity); fclose(capacity_file); strcpy(file_path_buffer_end, "status"); @@ -340,10 +348,18 @@ static char *get_nth_battery_percentage(int n, char** label) { // max length of resulting string is 19 // one byte for padding incase there is a newline // 100% [Discharging] + // High [Discharging] // 1234567890123456789 char *battery = malloc(20); - snprintf(battery, 20, "%d%% [%s]", battery_capacity, battery_status); + if (capacity_is_percent) + { + snprintf(battery, 20, "%s%% [%s]", battery_capacity, battery_status); + } + else + { + snprintf(battery, 20, "%s [%s]", battery_capacity, battery_status); + } // get model name of battery char *model_name = malloc(30); From 3157b5d334d949286dc6ad726d356e2828b0f566 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Wed, 13 May 2020 00:24:22 -0400 Subject: [PATCH 08/18] Comment out debug printf's --- paleofetch.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 0fb2e7a..f7af696 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -325,7 +325,7 @@ static char *get_nth_battery_percentage(int n, char** label) { bool capacity_is_percent = true; strcpy(file_path_buffer_end, "capacity"); - printf("%s\n", file_path_buffer); + /* printf("%s\n", file_path_buffer); */ if ((capacity_file = fopen(file_path_buffer, "r")) == NULL) { strcpy(file_path_buffer_end, "capacity_level"); if ((capacity_file = fopen(file_path_buffer, "r")) == NULL) { @@ -339,7 +339,7 @@ static char *get_nth_battery_percentage(int n, char** label) { fclose(capacity_file); strcpy(file_path_buffer_end, "status"); - printf("%s\n", file_path_buffer); + /* printf("%s\n", file_path_buffer); */ if ((status_file = fopen(file_path_buffer, "r")) != NULL) { fscanf(status_file, "%s", battery_status); fclose(status_file); @@ -365,7 +365,7 @@ static char *get_nth_battery_percentage(int n, char** label) { char *model_name = malloc(30); strcpy(file_path_buffer_end, "model_name"); - printf("%s\n", file_path_buffer); + /* printf("%s\n", file_path_buffer); */ if ((model_file = fopen(file_path_buffer, "r")) != NULL) { fgets(model_name, 30, model_file); remove_newline(model_name); From 841de22802cb4dfee3b9df6ad1732bd34a8355b2 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Wed, 13 May 2020 00:25:59 -0400 Subject: [PATCH 09/18] Delete commented out code --- paleofetch.c | 25 +------------------------ 1 file changed, 1 insertion(+), 24 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index f7af696..c5129a4 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -271,25 +271,8 @@ static struct dirent *get_nth_battery(int n) if (!power_supply_directory) { num_dirs = scandir("/sys/class/power_supply/", &power_supply_directory, directory_is_battery, alphasort); - - /* for (int i = 0; i < num_dirs; i++) */ - /* printf("Directory name_: %s\n", power_supply_directory[i]->d_name); */ - /* printf("_!!\n"); */ } - /* printf("!!!\n"); */ - - /* struct dirent *potential_battery; */ - /* if ((potential_battery = readdir(power_supply_directory)) != NULL) */ - /* { */ - /* if (!directory_is_battery(potential_battery)) */ - /* { */ - /* free(potential_battery); */ - /* return get_next_battery(); */ - /* } */ - /* } */ - - if (n < num_dirs) return power_supply_directory[n]; else @@ -303,14 +286,10 @@ static struct dirent *get_nth_battery(int n) static char *get_nth_battery_percentage(int n, char** label) { struct dirent *battery_directory = get_nth_battery(n); - /* if (battery_directory != NULL) */ - /* printf("Active battery: %s\n", battery_directory->d_name); */ + if (battery_directory == NULL) return calloc(1, 1); - /* DIR *power_supply_directory; */ - /* struct dirent *directory; */ - // could not figure out possible values for this char battery_capacity[12]; @@ -325,7 +304,6 @@ static char *get_nth_battery_percentage(int n, char** label) { bool capacity_is_percent = true; strcpy(file_path_buffer_end, "capacity"); - /* printf("%s\n", file_path_buffer); */ if ((capacity_file = fopen(file_path_buffer, "r")) == NULL) { strcpy(file_path_buffer_end, "capacity_level"); if ((capacity_file = fopen(file_path_buffer, "r")) == NULL) { @@ -339,7 +317,6 @@ static char *get_nth_battery_percentage(int n, char** label) { fclose(capacity_file); strcpy(file_path_buffer_end, "status"); - /* printf("%s\n", file_path_buffer); */ if ((status_file = fopen(file_path_buffer, "r")) != NULL) { fscanf(status_file, "%s", battery_status); fclose(status_file); From 0d45e49a5c08a9adab5cf811126464d94a5e091e Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Wed, 13 May 2020 00:30:12 -0400 Subject: [PATCH 10/18] Make curly brace style consistent --- paleofetch.c | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index c5129a4..ef4cdea 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -234,12 +234,10 @@ static char *get_uptime(char** label) { static int directory_is_battery(const struct dirent *potential_battery) { // filters out . and .. - if (potential_battery->d_name[0] == '.') - { + if (potential_battery->d_name[0] == '.') { return 0; } - else - { + else { // d_name can be up to 255 characters long // 285 bytes are necessary to hold all possible strings // in reality, the name of the battery folder probably wont be that long, @@ -249,23 +247,20 @@ static int directory_is_battery(const struct dirent *potential_battery) { FILE* battery_type_file; - if ((battery_type_file = fopen(directory_buffer, "r")) != NULL) - { + if ((battery_type_file = fopen(directory_buffer, "r")) != NULL) { // longest possible string is "Battery", which is 7 characters long // add 1 to buffer size for null terminating character char battery_type[8]; fscanf(battery_type_file, "%s", battery_type); return strcmp(battery_type, "Battery") == 0; } - else - { + else { return 0; } } } -static struct dirent *get_nth_battery(int n) -{ +static struct dirent *get_nth_battery(int n) { static struct dirent **power_supply_directory = NULL; static int num_dirs = -1; @@ -284,7 +279,6 @@ static struct dirent *get_nth_battery(int n) // returns percentage of the nth battery // changes the contents of *label to the name of the battery static char *get_nth_battery_percentage(int n, char** label) { - struct dirent *battery_directory = get_nth_battery(n); if (battery_directory == NULL) @@ -329,12 +323,10 @@ static char *get_nth_battery_percentage(int n, char** label) { // 1234567890123456789 char *battery = malloc(20); - if (capacity_is_percent) - { + if (capacity_is_percent) { snprintf(battery, 20, "%s%% [%s]", battery_capacity, battery_status); } - else - { + else { snprintf(battery, 20, "%s [%s]", battery_capacity, battery_status); } @@ -353,13 +345,11 @@ static char *get_nth_battery_percentage(int n, char** label) { return battery; } -static char *get_battery_1_percentage(char** label) -{ +static char *get_battery_1_percentage(char** label) { return get_nth_battery_percentage(0, label); } -static char *get_battery_2_percentage(char** label) -{ +static char *get_battery_2_percentage(char** label) { return get_nth_battery_percentage(1, label); } From ff155fe359e2c4c02225a1bc6d7e33b064da0438 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Wed, 13 May 2020 00:53:37 -0400 Subject: [PATCH 11/18] Delete battery labels Otherwise they are treated as spacers --- config.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.h b/config.h index a178e4b..08e1da6 100644 --- a/config.h +++ b/config.h @@ -10,8 +10,8 @@ { "Host: ", get_host, true }, \ { "Kernel: ", get_kernel, true }, \ { "Uptime: ", get_uptime, false }, \ - { "Battery 1: ", get_battery_1_percentage, false }, \ - { "Battery 2: ", get_battery_2_percentage, false }, \ + { "", get_battery_1_percentage, false }, \ + { "", get_battery_2_percentage, false }, \ SPACER \ { "Packages: ", get_packages_pacman, false }, \ { "Shell: ", get_shell, false }, \ From 9ca7a9c23d10e154746b6788e51593a509095cc8 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Wed, 13 May 2020 02:29:13 -0400 Subject: [PATCH 12/18] Give spacers "SPACER" label --- paleofetch.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paleofetch.h b/paleofetch.h index 7232957..1dc49cc 100644 --- a/paleofetch.h +++ b/paleofetch.h @@ -23,6 +23,6 @@ static char *get_title(char** label), *get_colors2(char** label), *spacer(char** label); -#define SPACER {"", spacer, false}, +#define SPACER {"SPACER", spacer, false}, #define REMOVE(A) { (A), NULL, sizeof(A) - 1 , 0 } #define REPLACE(A, B) { (A), (B), sizeof(A) - 1, sizeof(B) - 1 } From 7e80644eb32132921ff11c77d4e69914a02e2328 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Wed, 13 May 2020 02:30:55 -0400 Subject: [PATCH 13/18] Allow empty values to be skipped in output --- paleofetch.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index ef4cdea..b073d84 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -815,12 +815,14 @@ int main(int argc, char *argv[]) { if (strcmp(value, "") != 0) { // check if value is an empty string printf(COLOR"%s%s\e[0m%s\n", LOGO[i], c.label, value); // just print if not empty } else { - if (strcmp(c.label, "") != 0) { // check if label is empty, otherwise it's a spacer - ++offset; // print next line of information + if (strcmp(c.label, "SPACER") != 0) { // check if label is SPACER, otherwise not a spacer + ++offset; // print next line of informati free(value); // free memory allocated for empty value - value = get_value(&config[i+offset], read_cache, cache_data); + i--; + continue; } - printf(COLOR"%s%s\e[0m%s\n", LOGO[i], c.label, value); + + printf(COLOR"%s\e[0m\n", LOGO[i]); } free(value); From 00085fa3797410dda3f0566e6406d239a094ec60 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Mon, 18 May 2020 23:47:03 -0400 Subject: [PATCH 14/18] Fix typo --- paleofetch.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/paleofetch.c b/paleofetch.c index b073d84..371ab0e 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -816,7 +816,7 @@ int main(int argc, char *argv[]) { printf(COLOR"%s%s\e[0m%s\n", LOGO[i], c.label, value); // just print if not empty } else { if (strcmp(c.label, "SPACER") != 0) { // check if label is SPACER, otherwise not a spacer - ++offset; // print next line of informati + ++offset; // print next line of information free(value); // free memory allocated for empty value i--; continue; From b916a5ce2a6f25d2116eeff9bcf2e2b760dea48a Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Tue, 19 May 2020 15:53:00 -0400 Subject: [PATCH 15/18] Make battery labels user-configurable --- paleofetch.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/paleofetch.c b/paleofetch.c index 371ab0e..2936148 100644 --- a/paleofetch.c +++ b/paleofetch.c @@ -330,16 +330,25 @@ static char *get_nth_battery_percentage(int n, char** label) { snprintf(battery, 20, "%s [%s]", battery_capacity, battery_status); } - // get model name of battery - char *model_name = malloc(30); - - strcpy(file_path_buffer_end, "model_name"); - /* printf("%s\n", file_path_buffer); */ - if ((model_file = fopen(file_path_buffer, "r")) != NULL) { - fgets(model_name, 30, model_file); - remove_newline(model_name); - strcat(model_name, ": "); - *label = model_name; + // model name is needed if there is a "%s" in the format string + bool use_model_name = strstr(*label, "%s") != NULL; + if (use_model_name) { + // get model name of battery + char model_name[30]; + int final_label_buffer_size = sizeof(model_name) + strlen(*label); + char *final_label_buffer = malloc(final_label_buffer_size); + + strcpy(file_path_buffer_end, "model_name"); + /* printf("%s\n", file_path_buffer); */ + if ((model_file = fopen(file_path_buffer, "r")) != NULL) { + fgets(model_name, sizeof(model_name), model_file); + remove_newline(model_name); + /* strcat(model_name, ": "); */ + } + + snprintf(final_label_buffer, final_label_buffer_size, *label, model_name); + + *label = final_label_buffer; } return battery; From 544062a24e2c174ecb5959a12a8ce337129ebc64 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Tue, 19 May 2020 15:53:23 -0400 Subject: [PATCH 16/18] Update labels in config --- config.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/config.h b/config.h index 08e1da6..f563d68 100644 --- a/config.h +++ b/config.h @@ -10,8 +10,8 @@ { "Host: ", get_host, true }, \ { "Kernel: ", get_kernel, true }, \ { "Uptime: ", get_uptime, false }, \ - { "", get_battery_1_percentage, false }, \ - { "", get_battery_2_percentage, false }, \ + { "Battery: ", get_battery_1_percentage, false }, \ + { "%s: ", get_battery_2_percentage, false }, \ SPACER \ { "Packages: ", get_packages_pacman, false }, \ { "Shell: ", get_shell, false }, \ From f391fdce944a5f7e412292f6ae75d87df362b910 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Tue, 19 May 2020 16:08:34 -0400 Subject: [PATCH 17/18] Add battery percentage functions to README --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index c966979..3cafaf8 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ which function paleofetch will call display. Current available getter functions * `get_host`: Prints the model of computer * `get_kernel`: Prints the version of the linux kernel * `get_uptime`: Shows how long linux has been running +* `get_battery_1_percentage`, `get_battery_2_percentage`: Prints out the percentage of the nth battery and whether or not it is charging. The label is treated as a format string internally. If you want to use the model name of the battery inside of the label, use "%s" in its place. If you do not want to use the model name, you do not have to put "%s" in the label. * `get_packages`: Shows how many packages you have installed. Currently only works for pacman. * `get_shell`: Shows which shell you are using * `get_resolution`: Prints your screen resolution From 3dddd39fccdad3117c822bbb3b9f4c17dac9cfd0 Mon Sep 17 00:00:00 2001 From: Sridaran Thoniyil Date: Tue, 19 May 2020 16:10:41 -0400 Subject: [PATCH 18/18] Put "%s" references in code block --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 3cafaf8..ae49eec 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ which function paleofetch will call display. Current available getter functions * `get_host`: Prints the model of computer * `get_kernel`: Prints the version of the linux kernel * `get_uptime`: Shows how long linux has been running -* `get_battery_1_percentage`, `get_battery_2_percentage`: Prints out the percentage of the nth battery and whether or not it is charging. The label is treated as a format string internally. If you want to use the model name of the battery inside of the label, use "%s" in its place. If you do not want to use the model name, you do not have to put "%s" in the label. +* `get_battery_1_percentage`, `get_battery_2_percentage`: Prints out the percentage of the nth battery and whether or not it is charging. The label is treated as a format string internally. If you want to use the model name of the battery inside of the label, use `%s` in its place. If you do not want to use the model name, you do not have to put `%s` in the label. * `get_packages`: Shows how many packages you have installed. Currently only works for pacman. * `get_shell`: Shows which shell you are using * `get_resolution`: Prints your screen resolution