From bbf0fd0aba51b4d29354feab1d7e5e730fcd4d6f Mon Sep 17 00:00:00 2001 From: Your Name Date: Sun, 26 Nov 2023 12:49:11 +0300 Subject: [PATCH] fix --- config.h | 15 +++++------ include/fetch.h | 17 +++++++------ src/fetch.c | 66 ++++++++++++++++++++++++++++++++++++------------- src/main.c | 19 +++++++++++--- 4 files changed, 81 insertions(+), 36 deletions(-) diff --git a/config.h b/config.h index 84f490a..2d57acf 100644 --- a/config.h +++ b/config.h @@ -5,7 +5,7 @@ typedef struct { char *title; - void (*func)(const char *title, const FETCH fetch); + int (*func)(const char *title, const FETCH fetch); } PARAMETR; /* Config, EDIT THERE */ @@ -13,12 +13,13 @@ typedef struct { #define PC_CHAR "%" static PARAMETR CONFIG[] = { - {"Os > ", PrintOs}, - {"Krnl > ", GetKernel}, - {"User > ", GetUser}, - {"Uptm > ", GetUptime}, - {"Arch > ", GetArch}, - {"Shll > ", GetShell}, + {"Os ", PrintOs}, + {"Krnl ", GetKernel}, + {"User ", GetUser}, + {"Uptm ", GetUptime}, + {"Arch ", GetArch}, + {"Shll ", GetShell}, + {"Pkgs ", GetPkg}, {" ", Blank}, {" ", PrintColors} }; diff --git a/include/fetch.h b/include/fetch.h index fb18d77..1a5eea6 100644 --- a/include/fetch.h +++ b/include/fetch.h @@ -26,13 +26,14 @@ typedef struct { FETCH Init(void); int GetOs(char *os_name, size_t len); void GetArt(FETCH *fetch); -void GetKernel(const char *title, const FETCH fetch); -void PrintOs(const char *title, const FETCH fetch); -void PrintColors(const char *title, const FETCH fetch); -void Blank(const char *title, const FETCH fetch); -void GetUser(const char *title, const FETCH fetch); -void GetUptime(const char *title, const FETCH fetch); -void GetArch(const char *title, const FETCH fetch); -void GetShell(const char *title, const FETCH fetch); +int GetKernel(const char *title, const FETCH fetch); +int PrintOs(const char *title, const FETCH fetch); +int PrintColors(const char *title, const FETCH fetch); +int Blank(const char *title, const FETCH fetch); +int GetUser(const char *title, const FETCH fetch); +int GetUptime(const char *title, const FETCH fetch); +int GetArch(const char *title, const FETCH fetch); +int GetShell(const char *title, const FETCH fetch); +int GetPkg(const char *title, const FETCH fetch); #endif diff --git a/src/fetch.c b/src/fetch.c index 13388c1..b5cf437 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -81,37 +81,44 @@ void GetArt(FETCH *fetch) { SetArt(fetch, sizeof(Unknow) / sizeof(char *), NULL, Unknow, "\033[1;36m"); } -void GetKernel(const char *title, const FETCH fetch) { +int GetKernel(const char *title, const FETCH fetch) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.release); + return 0; } -void PrintOs(const char *title, const FETCH fetch) { +int PrintOs(const char *title, const FETCH fetch) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.os_name); + return 0; } -void PrintColors(const char *title, const FETCH fetch) { +int PrintColors(const char *title, const FETCH fetch) { UNUSED(title); UNUSED(fetch); for (int i = 1; i < 7; i++) printf("\033[1;3%dm%s\033[0m \033[0;3%dm%s\033[0m ", i, PC_CHAR, i, PC_CHAR); + + return 0; } -void Blank(const char *title, const FETCH fetch) { +int Blank(const char *title, const FETCH fetch) { UNUSED(title); UNUSED(fetch); - - printf(" "); + return 1; } -void GetUser(const char *title, const FETCH fetch) { +int GetUser(const char *title, const FETCH fetch) { UNUSED(fetch); struct passwd *pw = getpwuid(geteuid()); - printf("%s%s%s%s", fetch.color, title, fetch.font_color, (pw != 0) ? pw->pw_name : "none"); + if (pw == 0) + return 1; + + printf("%s%s%s%s", fetch.color, title, fetch.font_color, pw->pw_name); + return 0; } -void GetUptime(const char *title, const FETCH fetch) { +int GetUptime(const char *title, const FETCH fetch) { int hours = 0; int mins = 0; @@ -121,25 +128,50 @@ void GetUptime(const char *title, const FETCH fetch) { hours = uptime.tv_sec / 3600; mins = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60); -#endif printf("%s%s%s%dh %dm", fetch.color, title, fetch.font_color, hours, mins); + return 0; +#endif + + return 1; } -void GetArch(const char *title, const FETCH fetch) { +int GetArch(const char *title, const FETCH fetch) { printf("%s%s%s%s", fetch.color, title, fetch.font_color, fetch.uts.machine); + return 0; } -void GetShell(const char *title, const FETCH fetch) { +int GetShell(const char *title, const FETCH fetch) { char *shell = getenv("SHELL"); if (shell == NULL) - printf("%s%s%s%s", fetch.color, title, fetch.font_color, "none"); + return 1; char *splt = strrchr(shell, '/'); - if (splt == NULL) { - printf("%s%s%s%s", fetch.color, title, fetch.font_color, "none"); - return; - } + if (splt == NULL) + return 1; printf("%s%s%s%s", fetch.color, title, fetch.font_color, splt + 1); + return 0; +} + +int GetPkg(const char *title, const FETCH fetch) { + if (fetch.pkg_cmd == NULL) + return 1; + + FILE *fp = popen(fetch.pkg_cmd, "r"); + if (fp == NULL) + return 1; + + printf("%s%s%s", fetch.color, title, fetch.font_color); + + int ch; + while ((ch = getc(fp))) { + if (ch == EOF || ch == '\n') + break; + + putchar(ch); + } + + pclose(fp); + return 0; } diff --git a/src/main.c b/src/main.c index a65f880..3b373e4 100644 --- a/src/main.c +++ b/src/main.c @@ -4,16 +4,27 @@ int main(void) { FETCH fetch = Init(); + + /* Function failed */ + int flag = 0; + size_t i; for (i = 0; i < sizeof(CONFIG) / sizeof(PARAMETR); i++) { - if (i < fetch.logo_size) + if (i < fetch.logo_size && !flag) printf("%s", fetch.logo[i]); - if (i >= fetch.logo_size) + if (i >= fetch.logo_size && !flag) printf("%s", fetch.logo[fetch.logo_size - 1]); - CONFIG[i].func(CONFIG[i].title, fetch); - printf("\033[0m\n"); + if (!CONFIG[i].func(CONFIG[i].title, fetch)) { + flag = 0; + printf("\033[0m\n"); + } + + else { + flag = 1; + printf("\033[0m"); + } } for (; i < fetch.logo_size; i++)