This commit is contained in:
Your Name 2023-11-26 12:49:11 +03:00
parent 4b434d35ae
commit bbf0fd0aba
4 changed files with 81 additions and 36 deletions

View File

@ -5,7 +5,7 @@
typedef struct { typedef struct {
char *title; char *title;
void (*func)(const char *title, const FETCH fetch); int (*func)(const char *title, const FETCH fetch);
} PARAMETR; } PARAMETR;
/* Config, EDIT THERE */ /* Config, EDIT THERE */
@ -13,12 +13,13 @@ typedef struct {
#define PC_CHAR "%" #define PC_CHAR "%"
static PARAMETR CONFIG[] = { static PARAMETR CONFIG[] = {
{"Os > ", PrintOs}, {"Os ", PrintOs},
{"Krnl > ", GetKernel}, {"Krnl ", GetKernel},
{"User > ", GetUser}, {"User ", GetUser},
{"Uptm > ", GetUptime}, {"Uptm ", GetUptime},
{"Arch > ", GetArch}, {"Arch ", GetArch},
{"Shll > ", GetShell}, {"Shll ", GetShell},
{"Pkgs ", GetPkg},
{" ", Blank}, {" ", Blank},
{" ", PrintColors} {" ", PrintColors}
}; };

View File

@ -26,13 +26,14 @@ typedef struct {
FETCH Init(void); FETCH Init(void);
int GetOs(char *os_name, size_t len); int GetOs(char *os_name, size_t len);
void GetArt(FETCH *fetch); void GetArt(FETCH *fetch);
void GetKernel(const char *title, const FETCH fetch); int GetKernel(const char *title, const FETCH fetch);
void PrintOs(const char *title, const FETCH fetch); int PrintOs(const char *title, const FETCH fetch);
void PrintColors(const char *title, const FETCH fetch); int PrintColors(const char *title, const FETCH fetch);
void Blank(const char *title, const FETCH fetch); int Blank(const char *title, const FETCH fetch);
void GetUser(const char *title, const FETCH fetch); int GetUser(const char *title, const FETCH fetch);
void GetUptime(const char *title, const FETCH fetch); int GetUptime(const char *title, const FETCH fetch);
void GetArch(const char *title, const FETCH fetch); int GetArch(const char *title, const FETCH fetch);
void GetShell(const char *title, const FETCH fetch); int GetShell(const char *title, const FETCH fetch);
int GetPkg(const char *title, const FETCH fetch);
#endif #endif

View File

@ -81,37 +81,44 @@ void GetArt(FETCH *fetch) {
SetArt(fetch, sizeof(Unknow) / sizeof(char *), NULL, Unknow, "\033[1;36m"); 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); 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); 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(title);
UNUSED(fetch); UNUSED(fetch);
for (int i = 1; i < 7; i++) 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); 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(title);
UNUSED(fetch); UNUSED(fetch);
return 1;
printf(" ");
} }
void GetUser(const char *title, const FETCH fetch) { int GetUser(const char *title, const FETCH fetch) {
UNUSED(fetch); UNUSED(fetch);
struct passwd *pw = getpwuid(geteuid()); 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 hours = 0;
int mins = 0; int mins = 0;
@ -121,25 +128,50 @@ void GetUptime(const char *title, const FETCH fetch) {
hours = uptime.tv_sec / 3600; hours = uptime.tv_sec / 3600;
mins = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60); 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); 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); 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"); char *shell = getenv("SHELL");
if (shell == NULL) if (shell == NULL)
printf("%s%s%s%s", fetch.color, title, fetch.font_color, "none"); return 1;
char *splt = strrchr(shell, '/'); char *splt = strrchr(shell, '/');
if (splt == NULL) { if (splt == NULL)
printf("%s%s%s%s", fetch.color, title, fetch.font_color, "none"); return 1;
return;
}
printf("%s%s%s%s", fetch.color, title, fetch.font_color, splt + 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;
} }

View File

@ -4,16 +4,27 @@
int main(void) { int main(void) {
FETCH fetch = Init(); FETCH fetch = Init();
/* Function failed */
int flag = 0;
size_t i; size_t i;
for (i = 0; i < sizeof(CONFIG) / sizeof(PARAMETR); 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]); printf("%s", fetch.logo[i]);
if (i >= fetch.logo_size) if (i >= fetch.logo_size && !flag)
printf("%s", fetch.logo[fetch.logo_size - 1]); printf("%s", fetch.logo[fetch.logo_size - 1]);
CONFIG[i].func(CONFIG[i].title, fetch); if (!CONFIG[i].func(CONFIG[i].title, fetch)) {
printf("\033[0m\n"); flag = 0;
printf("\033[0m\n");
}
else {
flag = 1;
printf("\033[0m");
}
} }
for (; i < fetch.logo_size; i++) for (; i < fetch.logo_size; i++)