From ec1af43272e62983a51322c43ca2d5d70a0b6413 Mon Sep 17 00:00:00 2001 From: Pascal Puffke Date: Fri, 19 Mar 2021 19:21:35 +0100 Subject: [PATCH] Fix CPU and GPU length based on terminal width If the CPU or GPU name strings are longer than the terminal width minus 28, truncate them --- uwufetch.c | 96 +++++++++++++++++++++++++++++++++--------------------- 1 file changed, 59 insertions(+), 37 deletions(-) diff --git a/uwufetch.c b/uwufetch.c index fe978e2..c603744 100644 --- a/uwufetch.c +++ b/uwufetch.c @@ -20,6 +20,7 @@ #include #include #include +#include // COLORS #define NORMAL "\x1b[0m" @@ -37,9 +38,10 @@ struct utsname sys_var; struct sysinfo sys; +struct winsize win; // initialise the variables to store data, gpu array can hold up to 8 gpus -int ram_max = 0, ram_free = 0, pkgs, a_i_flag = 0; +int ram_max = 0, ram_free = 0, pkgs, a_i_flag = 0, target_width = 0; char user[32], host[256], shell[64], version_name[64], cpu_model[256], gpu_model[8][256] = {{'0'},{'0'},{'0'},{'0'},{'0'},{'0'},{'0'},{'0'}}, pkgman_name[64], image_name[32]; int pkgman(); @@ -50,6 +52,7 @@ void print_info(); void print_image(); void usage(char*); void uwu_name(); +void truncate_name(char*); int main(int argc, char *argv[]) { int opt = 0; @@ -86,41 +89,47 @@ int main(int argc, char *argv[]) { } int pkgman() { // this is just a function that returns the total of installed packages - int total = 0; + int apt, apk, dnf, emerge, flatpak, guix, nix, pacman, rpm, xbps, total = 0; - // TODO: should this be at the top of the program? maybe in a config.c file? - // TODO: do we need to `free()` this? I have no idea how to do memory management in C... - struct package_manager { - char command_string[128]; // command to get number of packages installed - char pkgman_name[16]; // name of the package manager - }; + FILE *file[10]; // when you add a new package manager support, make sure to update the array size. + file[0] = popen("dpkg-query -f '${binary:Package}\n' -W 2> /dev/null | wc -l", "r"); + file[1] = popen("apk info 2> /dev/null | wc -l", "r"); + file[2] = popen("dnf list installed 2> /dev/null | wc -l", "r"); + file[3] = popen("qlist -I 2> /dev/null | wc -l", "r"); + file[4] = popen("flatpak list 2> /dev/null | wc -l", "r"); + file[5] = popen("guix package --list-installed 2> /dev/null | wc -l", "r"); + file[6] = popen("nix-store -q --requisites /run/current-sys_vartem/sw 2> /dev/null | wc -l", "r"); + file[7] = popen("pacman -Qq 2> /dev/null | wc -l", "r"); + file[8] = popen("rpm -qa --last 2> /dev/null | wc -l", "r"); + file[9] = popen("xbps-query -l 2> /dev/null | wc -l", "r"); - struct package_manager pkgmans[] = { - { "apk info 2> /dev/null | wc -l", "(apk)" }, - { "dnf list installed 2> /dev/null | wc -l", "(dnf)" }, - { "qlist -I 2> /dev/null | wc -l", "(emerge)" }, - { "flatpak list 2> /dev/null | wc -l", "(flatpack)" }, - { "guix package --list-installed 2> /dev/null | wc -l", "(guix)" }, - { "nix-store -q --requisites /run/current-sys_vartem/sw 2> /dev/null | wc -l", "(nix)" }, - { "pacman -Qq 2> /dev/null | wc -l", "(pacman)" }, - { "rpm -qa --last 2> /dev/null | wc -l", "(rpm)" }, - { "xbps-query -l 2> /dev/null | wc -l", "(xbps)" } - }; + // the if statements are there for error handling and for preventing the #27 issue + if (fscanf(file[0], "%d", &apt) == 3) apt = 0; + if (fscanf(file[1], "%d", &apk) == 3) apk = 0; + if (fscanf(file[2], "%d", &dnf) == 3) dnf = 0; + if (fscanf(file[3], "%d", &emerge) == 3) emerge = 0; + if (fscanf(file[4], "%d", &flatpak) == 3) flatpak = 0; + if (fscanf(file[5], "%d", &guix) == 3) guix = 0; + if (fscanf(file[6], "%d", &nix) == 3) nix = 0; + if (fscanf(file[7], "%d", &pacman) == 3) pacman = 0; + if (fscanf(file[8], "%d", &rpm) == 3) rpm = 0; + if (fscanf(file[9], "%d", &xbps) == 3) xbps = 0; - const unsigned long pkgman_count = sizeof(pkgmans) / sizeof(pkgmans[0]); + for (int i = 0; i < 8; i++) fclose(file[i]); - for (long unsigned int i = 0; i < pkgman_count; i++) { // long unsigned int instead of int because of -Wsign-compare - struct package_manager *current = &pkgmans[i]; + #define ADD_PKGMAN_NAME(package_count, pkgman_to_add) if (package_count > 0) { total += package_count; strcat(pkgman_name, pkgman_to_add); } + ADD_PKGMAN_NAME(apt, "(apt)") + ADD_PKGMAN_NAME(apk, "(apk)") + ADD_PKGMAN_NAME(dnf, "(dnf)") + ADD_PKGMAN_NAME(emerge, "(emerge)") + ADD_PKGMAN_NAME(flatpak,"(flatpak)") + ADD_PKGMAN_NAME(guix ,"(guix)") + ADD_PKGMAN_NAME(nix, "(nix)") + ADD_PKGMAN_NAME(pacman, "(pacman)") + ADD_PKGMAN_NAME(rpm, "(rpm)") + ADD_PKGMAN_NAME(xbps, "(xbps)") + #undef ADD_PKGMAN_NAME - FILE *fp = popen(current->command_string, "r"); - unsigned int pkg_count; - - if (fscanf(fp, "%u", &pkg_count) == 3) continue; - fclose(fp); - - total += pkg_count; - if (pkg_count > 0) strcat(pkgman_name, current->pkgman_name); - } return total; } @@ -158,6 +167,12 @@ void print_info() { void get_info() { // get all necessary info char line[256]; // var to scan file lines + + // terminal width + // used for truncating long names + ioctl(STDOUT_FILENO, TIOCGWINSZ, &win); + target_width = win.ws_col - 28; + // os version FILE *os_release = fopen("/etc/os-release", "r"); FILE *cpuinfo = fopen("/proc/cpuinfo", "r"); @@ -185,6 +200,9 @@ void get_info() { // get all necessary info sscanf(getenv("SHELL"), "%s", shell); if (strlen(shell) > 16) memmove(&shell, &shell[27], strlen(shell)); // android shell was too long, this works only for termux + // truncate CPU name + truncate_name(cpu_model); + // system resources uname(&sys_var); sysinfo(&sys); @@ -215,12 +233,10 @@ void get_info() { // get all necessary info while (fgets(line, sizeof(line), gpu)) if (sscanf(line, "%[^\n]", gpu_model[0])) break; } fclose(gpu); - - // format the strings a bit - for(int i = 0; i < gpun; i++) { - for (int j = 42; j < 256; j++) { //max gpu_name length - gpu_model[i][j] = '\0'; - } + + // GPU name shortening + for (int i = 0; i < gpun; i++) { + truncate_name(gpu_model[i]); } pkgs = pkgman(); @@ -468,3 +484,9 @@ void uwu_name() { // changes distro name to uwufied(?) name } #undef STRING_TO_UWU } + +void truncate_name(char* name) { + for (int i = target_width; i < 256; i++) { + name[i] = '\0'; + } +}