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
This commit is contained in:
parent
3ada75bbaa
commit
ec1af43272
1 changed files with 59 additions and 37 deletions
92
uwufetch.c
92
uwufetch.c
|
@ -20,6 +20,7 @@
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sys/sysinfo.h>
|
#include <sys/sysinfo.h>
|
||||||
#include <sys/utsname.h>
|
#include <sys/utsname.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
// COLORS
|
// COLORS
|
||||||
#define NORMAL "\x1b[0m"
|
#define NORMAL "\x1b[0m"
|
||||||
|
@ -37,9 +38,10 @@
|
||||||
|
|
||||||
struct utsname sys_var;
|
struct utsname sys_var;
|
||||||
struct sysinfo sys;
|
struct sysinfo sys;
|
||||||
|
struct winsize win;
|
||||||
|
|
||||||
// initialise the variables to store data, gpu array can hold up to 8 gpus
|
// 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];
|
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();
|
int pkgman();
|
||||||
|
@ -50,6 +52,7 @@ void print_info();
|
||||||
void print_image();
|
void print_image();
|
||||||
void usage(char*);
|
void usage(char*);
|
||||||
void uwu_name();
|
void uwu_name();
|
||||||
|
void truncate_name(char*);
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
int opt = 0;
|
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 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?
|
FILE *file[10]; // when you add a new package manager support, make sure to update the array size.
|
||||||
// TODO: do we need to `free()` this? I have no idea how to do memory management in C...
|
file[0] = popen("dpkg-query -f '${binary:Package}\n' -W 2> /dev/null | wc -l", "r");
|
||||||
struct package_manager {
|
file[1] = popen("apk info 2> /dev/null | wc -l", "r");
|
||||||
char command_string[128]; // command to get number of packages installed
|
file[2] = popen("dnf list installed 2> /dev/null | wc -l", "r");
|
||||||
char pkgman_name[16]; // name of the package manager
|
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[] = {
|
// the if statements are there for error handling and for preventing the #27 issue
|
||||||
{ "apk info 2> /dev/null | wc -l", "(apk)" },
|
if (fscanf(file[0], "%d", &apt) == 3) apt = 0;
|
||||||
{ "dnf list installed 2> /dev/null | wc -l", "(dnf)" },
|
if (fscanf(file[1], "%d", &apk) == 3) apk = 0;
|
||||||
{ "qlist -I 2> /dev/null | wc -l", "(emerge)" },
|
if (fscanf(file[2], "%d", &dnf) == 3) dnf = 0;
|
||||||
{ "flatpak list 2> /dev/null | wc -l", "(flatpack)" },
|
if (fscanf(file[3], "%d", &emerge) == 3) emerge = 0;
|
||||||
{ "guix package --list-installed 2> /dev/null | wc -l", "(guix)" },
|
if (fscanf(file[4], "%d", &flatpak) == 3) flatpak = 0;
|
||||||
{ "nix-store -q --requisites /run/current-sys_vartem/sw 2> /dev/null | wc -l", "(nix)" },
|
if (fscanf(file[5], "%d", &guix) == 3) guix = 0;
|
||||||
{ "pacman -Qq 2> /dev/null | wc -l", "(pacman)" },
|
if (fscanf(file[6], "%d", &nix) == 3) nix = 0;
|
||||||
{ "rpm -qa --last 2> /dev/null | wc -l", "(rpm)" },
|
if (fscanf(file[7], "%d", &pacman) == 3) pacman = 0;
|
||||||
{ "xbps-query -l 2> /dev/null | wc -l", "(xbps)" }
|
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
|
#define ADD_PKGMAN_NAME(package_count, pkgman_to_add) if (package_count > 0) { total += package_count; strcat(pkgman_name, pkgman_to_add); }
|
||||||
struct package_manager *current = &pkgmans[i];
|
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;
|
return total;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -158,6 +167,12 @@ void print_info() {
|
||||||
|
|
||||||
void get_info() { // get all necessary info
|
void get_info() { // get all necessary info
|
||||||
char line[256]; // var to scan file lines
|
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
|
// os version
|
||||||
FILE *os_release = fopen("/etc/os-release", "r");
|
FILE *os_release = fopen("/etc/os-release", "r");
|
||||||
FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
|
FILE *cpuinfo = fopen("/proc/cpuinfo", "r");
|
||||||
|
@ -185,6 +200,9 @@ void get_info() { // get all necessary info
|
||||||
sscanf(getenv("SHELL"), "%s", shell);
|
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
|
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
|
// system resources
|
||||||
uname(&sys_var);
|
uname(&sys_var);
|
||||||
sysinfo(&sys);
|
sysinfo(&sys);
|
||||||
|
@ -216,11 +234,9 @@ void get_info() { // get all necessary info
|
||||||
}
|
}
|
||||||
fclose(gpu);
|
fclose(gpu);
|
||||||
|
|
||||||
// format the strings a bit
|
// GPU name shortening
|
||||||
for (int i = 0; i < gpun; i++) {
|
for (int i = 0; i < gpun; i++) {
|
||||||
for (int j = 42; j < 256; j++) { //max gpu_name length
|
truncate_name(gpu_model[i]);
|
||||||
gpu_model[i][j] = '\0';
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pkgs = pkgman();
|
pkgs = pkgman();
|
||||||
|
@ -468,3 +484,9 @@ void uwu_name() { // changes distro name to uwufied(?) name
|
||||||
}
|
}
|
||||||
#undef STRING_TO_UWU
|
#undef STRING_TO_UWU
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void truncate_name(char* name) {
|
||||||
|
for (int i = target_width; i < 256; i++) {
|
||||||
|
name[i] = '\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue