kfetch/src/main.c
2024-10-20 09:03:05 +00:00

103 lines
1.8 KiB
C

#define _MAIN_C
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "structs.h"
#include "config.h"
#ifdef ERROR_STR
char *error_str = ERROR_STR;
#else
char *error_str = "none";
#endif
#ifdef STRIP_RESET_ESCAPE
char *reset = "";
#else
char *reset = "\033[0m";
#endif
#ifdef DONT_PRINT_LOGO
char l_flag = 1;
#else
char l_flag = 0;
#endif
void print_unescape(char newline, char *str) {
#ifdef STRIP_LOGO_ESCAPES
size_t len = strlen(str);
char *str2 = malloc(len);
if (str2 == NULL) {
fprintf(stderr, "kfetch: malloc: %s\n", strerror(errno));
exit(1);
}
size_t j = 0;
for (size_t i = 0; i < len; i++) {
if (str[i] == '\033') {
while (str[i] != 'm') {
if (i > len) {
fputs("kfetch: wrong logo string\n", stderr);
exit(1);
}
i++;
}
continue;
}
str2[j] = str[i];
j++;
}
str2[j] = '\0';
printf("%s%c", str2, newline);
free(str2);
#else
printf("%s%c", str, newline);
#endif
}
int main(int argc, char **argv) {
FETCH fetch = Init();
size_t j = 0;
for (size_t i = 0; i < sizeof(CONFIG) / sizeof(PARAMETR); i++) {
if (CONFIG[i].func == NULL)
break;
/* Print ascii logo */
if (j < fetch.logo_size && !l_flag)
print_unescape('\0', fetch.logo[j]);
else if (j >= fetch.logo_size && !l_flag)
print_unescape('\0', fetch.logo[fetch.logo_size - 1]);
/* Function exec */
printf("%s%s", fetch.logo_color, CONFIG[i].par.str0);
if (CONFIG[i].func(CONFIG[i].par, fetch))
printf("%s%s", fetch.font_color, error_str);
else {
if (CONFIG[i].par.str1 != NULL)
printf(CONFIG[i].par.str1, fetch.font_color);
}
j++;
printf("%s\n", reset);
}
for (; j < fetch.logo_size - 1; j++)
if (!l_flag)
print_unescape('\n', fetch.logo[j]);
printf("%s", reset);
return 0;
}