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 {
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}
};

View File

@ -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

View File

@ -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;
}

View File

@ -4,18 +4,29 @@
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);
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++)
printf("%s\n", fetch.logo[i]);