Reworked some stuff

This commit is contained in:
Јован Ђокић-Шумарац 2021-05-13 18:43:02 +02:00
parent 17cd2020c4
commit b8599b6023
3 changed files with 145 additions and 74 deletions

42
ascii.h
View file

@ -1 +1,41 @@
TEST char artix [14][100] = {
" .cc' ",
" .cccc. ",
" cccccc. ",
" :ccccccc. ",
" ;ccccccccc ",
" .;cccccc: ",
" .. .;cccc; ",
" 'cccc;.. .,cc; ",
" 'ccccccccc:'. '' ",
" .cccccccccccccc:;. ",
" .cccccccccccccc:,. . ",
" ccccccccccc;'. .;cc. ",
" :ccc:'. .';ccccc ",
" ;;.. .';: "
};
char arch [19][100] = {
"",
" ▟█▙ ",
" ▟███▙ ",
" ▟█████▙ ",
" ▟███████▙ ",
" ▂▔▀▜██████▙ ",
" ▟██▅▂▝▜█████▙ ",
" ▟█████████████▙ ",
" ▟███████████████▙ ",
" ▟█████████████████▙ ",
" ▟███████████████████▙ ",
" ▟█████████▛▀▀▜████████▙ ",
" ▟████████▛ ▜███████▙ ",
" ▟█████████ ████████▙ ",
" ▟██████████ █████▆▅▄▃▂ ",
" ▟██████████▛ ▜█████████▙ ",
" ▟██████▀▀▀ ▀▀██████▙ ",
" ▟███▀▘ ▝▀███▙ ",
" ▟▛▀ ▀▜▙ "
};

BIN
fetchy

Binary file not shown.

177
fetchy.c
View file

@ -4,13 +4,15 @@
#include <stdlib.h> #include <stdlib.h>
#include <math.h> #include <math.h>
#include <dirent.h> #include <dirent.h>
#include "ascii.h"
#define BUF_SIZE 20 #define BUF_SIZE 50
void truncate_spaces_leading(char *str); void truncate_spaces_leading(char *str);
void truncate_spaces_trailing(char *str); void truncate_spaces_trailing(char *str);
void read_line(char x); void read_line(char x);
void cache_info(char *cache_path, char **cpu_name, char **gpu);
static int count_files(DIR *package_dir); static int count_files(DIR *package_dir);
@ -20,89 +22,67 @@ static char *get_uptime(char *uptime);
static char *get_RAM(char *ram_info); static char *get_RAM(char *ram_info);
static char *get_cpu(char *cpuname); static char *get_cpu(char *cpuname);
static char *get_gpu(char *gpu); static char *get_gpu(char *gpu);
static int get_packages(char sys); static char *get_packages(char *package_count, char sys);
void concatenate_and_print(char *ascii, int line_n);
int main(int argc, char *argv[]){ int main(int argc, char *argv[]){
char sys; char sys;
char *os_name = NULL, *uptime = NULL, *kern_name = NULL, *ram_info = NULL, *cpu_name = NULL, *gpu = NULL; char *os_name = NULL, *uptime = NULL, *kern_name = NULL, *ram_info = NULL, *cpu_name = NULL, *gpu = NULL, *package_count = NULL;
char *os = malloc(BUF_SIZE), *cache_path = malloc(BUF_SIZE * 2); char *os = malloc(BUF_SIZE), *cache_path = malloc(BUF_SIZE * 2);
snprintf(cache_path, BUF_SIZE * 2, "%s/.local/share/fetchy.cache", getenv("HOME"));
FILE *FCache = NULL;
if( (FCache = fopen(cache_path, "r")) ){
cpu_name = malloc(BUF_SIZE * 4);
gpu = malloc(BUF_SIZE * 4);
char *line = malloc(BUF_SIZE * 4);
fgets(line, BUF_SIZE * 4, FCache);
snprintf(cpu_name, BUF_SIZE * 4, "%s", strchr(line, ':') +2);
truncate_spaces_trailing(cpu_name);
fgets(line, BUF_SIZE * 4, FCache);
snprintf(gpu, BUF_SIZE * 4, "%s", strchr(line, ':') +2);
truncate_spaces_trailing(gpu);
fclose(FCache);
free(line);
}
else {
FCache = fopen(cache_path, "w");
fprintf(FCache, "CPU : %s\n", get_cpu(cpu_name));
fprintf(FCache, "GPU : %s\n", get_gpu(gpu));
fclose(FCache);
printf("\n\aFILE CACHING DONE!! \n\nValues returned : \nCPU -> %s\nGPU -> %s\n\n Caching occurs only once, every subsequent execution will run program normally. Enjoy!\n\n", get_cpu(cpu_name), get_gpu(gpu));
exit(0);
}
cache_info(cache_path, &cpu_name, &gpu);
os = get_sys(&sys, os_name); os = get_sys(&sys, os_name);
switch(sys){ switch(sys){
case 'x':
case 'a': case 'a':
printf("\n"); printf("\n");
printf("\x1b[1m . \x1b[36mOS\x1b[0m -> %s\n", os); printf("\x1b[1m . %s\n", os);
printf("\x1b[1m / \\ \x1b[36mKRNL\x1b[0m -> %s\n", get_kernel(kern_name)); printf("\x1b[1m / \\ %s\n", get_kernel(kern_name));
printf("\x1b[1m / \\ \x1b[36mUPT\x1b[0m -> %s\n", get_uptime(uptime)); printf("\x1b[1m / \\ %s\n", get_uptime(uptime));
printf("\x1b[1m /^. \\ \x1b[36mPKGS\x1b[0m -> %d\n", get_packages(sys)); printf("\x1b[1m /^. \\ %s\n", get_packages(package_count, sys));
printf("\x1b[1m / .-. \\ \x1b[36m\x1b[0m \n"); printf("\x1b[1m / .-. \\ \n");
printf("\x1b[1m / ( ) _\\ \x1b[36mCPU\x1b[0m -> %s\n", cpu_name); printf("\x1b[1m / ( ) _\\ %s\n", cpu_name);
printf("\x1b[1m / _.~ ~._^\\ \x1b[36mGPU\x1b[0m -> %s\n", gpu); printf("\x1b[1m / _.~ ~._^\\ %s\n", gpu);
printf("\x1b[1m /.^ ^.\\ \x1b[36mRAM\x1b[0m -> %s\n", get_RAM(ram_info)); printf("\x1b[1m /.^ ^.\\ %s\n", get_RAM(ram_info));
printf("\n\n"); printf("\n\n");
break; break;
case 'u': case 'u':
printf("\n"); printf("\n");
printf("\x1b[1m .-. \x1b[36mOS\x1b[0m -> %s\n", os); printf("\x1b[1m .-. %s\n", os);
printf("\x1b[1m .-'``(|||) \x1b[36mKERNEL\x1b[0m -> %s\n", get_kernel(kern_name)); printf("\x1b[1m .-'``(|||) %s\n", get_kernel(kern_name));
printf("\x1b[1m ,`\\ \\ `-`. \x1b[36mUPTIME\x1b[0m -> %s\n", get_uptime(uptime)); printf("\x1b[1m ,`\\ \\ `-`. %s\n", get_uptime(uptime));
printf("\x1b[1m / \\ '``-. ` \x1b[36mPACKAGES\x1b[0m -> %s\n", get_packages(sys)); //%d\n", get_packages(sys)); printf("\x1b[1m / \\ '``-. ` %s\n", get_packages(package_count, sys)); //%d\n", get_packages(sys));
printf("\x1b[1m .-. , `___: \x1b[36m\x1b[0m \n"); printf("\x1b[1m .-. , `___: \n");
printf("\x1b[1m (:::) : ___ \x1b[36mCPU\x1b[0m -> %s\n", cpu_name); printf("\x1b[1m (:::) : ___ %s\n", cpu_name);
printf("\x1b[1m `-` ` , : \x1b[36mGPU\x1b[0m -> %s\n", gpu); printf("\x1b[1m `-` ` , : %s\n", gpu);
printf("\x1b[1m \\ / ,..-` , \x1b[36mRAM\x1b[0m -> %s\n", get_RAM(ram_info)); printf("\x1b[1m \\ / ,..-` , %s\n", get_RAM(ram_info));
printf("\x1b[1m `./ / .-.` \x1b[36m\x1b[0m \n"); printf("\x1b[1m `./ / .-.` \n");
printf("\x1b[1m `-..-( ) \x1b[36m\x1b[0m \n"); printf("\x1b[1m `-..-( ) \n");
printf("\x1b[1m `-` \x1b[36m\x1b[0m \n"); printf("\x1b[1m `-` \n");
printf("\n\n"); printf("\n\n");
break; break;
/* case 'x':
strcat(artix[3], os);
for (int i = 0; i < 14; i++)
printf("%s\n", artix[i]);
*/
default: default:
printf("\n\n ERROR : Unsupported system\n\n"); printf("\n\n ERROR : Unsupported system\n\n");
exit(-1); exit(-1);
@ -110,13 +90,7 @@ int main(int argc, char *argv[]){
free(os_name); free(os_name); free(uptime); free(ram_info); free(kern_name); free(cpu_name); free(gpu); free(cache_path); free(package_count);
free(uptime);
free(ram_info);
free(kern_name);
free(cpu_name);
free(gpu);
free(cache_path);
return 0; return 0;
} }
@ -164,6 +138,43 @@ void truncate_spaces_trailing(char *str){
} }
void cache_info(char *cache_path, char **cpu_name, char **gpu){
snprintf(cache_path, BUF_SIZE * 2, "%s/.local/share/fetchy.cache", getenv("HOME"));
FILE *FCache = NULL;
if( (FCache = fopen(cache_path, "r")) ){
*cpu_name = malloc(BUF_SIZE * 4);
*gpu = malloc(BUF_SIZE * 4);
char *line = malloc(BUF_SIZE * 4);
fgets(line, BUF_SIZE * 4, FCache);
snprintf(*cpu_name, BUF_SIZE * 4, "\x1b[36mCPU\x1b[0m -> %s", strchr(line, ':') +2);
truncate_spaces_trailing(*cpu_name);
fgets(line, BUF_SIZE * 4, FCache);
snprintf(*gpu, BUF_SIZE * 4, "\x1b[36mGPU\x1b[0m -> %s", strchr(line, ':') +2);
truncate_spaces_trailing(*gpu);
fclose(FCache);
free(line);
}
else {
FCache = fopen(cache_path, "w");
fprintf(FCache, "CPU : %s\n", get_cpu(*cpu_name));
fprintf(FCache, "GPU : %s\n", get_gpu(*gpu));
fclose(FCache);
printf("\n\aFILE CACHING DONE!! \n\nValues returned : \nCPU -> %s\nGPU -> %s\n\n Caching occurs only once, every subsequent execution will run program normally. Enjoy!\n\n", get_cpu(*cpu_name), get_gpu(*gpu));
free(*cpu_name); free(*gpu);
exit(0);
}
}
static int count_files(DIR *package_dir){ static int count_files(DIR *package_dir){
struct dirent * entry; struct dirent * entry;
@ -190,9 +201,10 @@ static char *get_sys(char *sys, char *os_name){
fclose(sysName); fclose(sysName);
truncate_spaces_leading(name); truncate_spaces_leading(name);
snprintf(os_name, BUF_SIZE, "%s Linux", name); snprintf(os_name, BUF_SIZE, "\x1b[36mOS\x1b[0m -> %s Linux", name);
if ( !strcmp( name, "Arch")) *sys = 'a'; if ( !strcmp( name, "Arch")) *sys = 'a';
if ( !strcmp( name, "Artix")) *sys = 'x';
if ( !strcmp( name, "Ubuntu")) *sys = 'u'; if ( !strcmp( name, "Ubuntu")) *sys = 'u';
free(name); free(name);
@ -247,7 +259,7 @@ static char *get_kernel(char *kern_name){
fclose(kInfo); fclose(kInfo);
truncate_spaces_leading(kernel); truncate_spaces_leading(kernel);
snprintf(kern_name, BUF_SIZE + 5, "Linux %s", kernel); snprintf(kern_name, BUF_SIZE + 5, "\x1b[36mKRNL\x1b[0m -> Linux %s", kernel);
free(kernel); free(kernel);
return kern_name; return kern_name;
@ -265,16 +277,16 @@ static char *get_uptime(char *uptime){
hr = (sec/60/60%24); hr = (sec/60/60%24);
min = (sec/60%60); min = (sec/60%60);
snprintf(uptime, BUF_SIZE, "%dh, %dmin", hr, min); snprintf(uptime, BUF_SIZE, "\x1b[36mUPT\x1b[0m -> %dh, %dmin", hr, min);
return uptime; return uptime;
} }
static int get_packages(char sys){ static char *get_packages(char *package_count, char sys){
//DIR *package_dir; //DIR *package_dir;
int pkg_count = 0; int pkg_count = 0;
package_count = malloc(BUF_SIZE);
switch (sys){ switch (sys){
case 'a': case 'a':
@ -287,7 +299,10 @@ static int get_packages(char sys){
printf("\n\n\aUnable to find package list\n\n"); printf("\n\n\aUnable to find package list\n\n");
} }
return pkg_count; snprintf(package_count, BUF_SIZE, "\x1b[36mPKGS\x1b[0m -> %d", pkg_count);
return package_count;
} }
@ -323,10 +338,26 @@ static char *get_RAM(char *ram_info){
bar[i] = '*'; bar[i] = '*';
} }
snprintf(ram_info, BUF_SIZE * 3, "%.2f GB of %.2f GB, %s -> %.2f %%", used / 1000000, total / 1000000, bar, real_percent); snprintf(ram_info, BUF_SIZE * 3, "\x1b[36mRAM\x1b[0m -> %.2f GB of %.2f GB, %s -> %.2f %%", used / 1000000, total / 1000000, bar, real_percent);
free(line); free(line);
return ram_info; return ram_info;
} }
/*void concatenate_and_print(char *ascii, int line_n){
strcat(ascii[0], os);
strcat(ascii[1], os);
strcat(ascii[2], os);
strcat(ascii[3], os);
strcat(ascii[4], os);
strcat(ascii[5], os);
strcat(ascii[6], os);
strcat(ascii[7], os);
for (int i = 0; i < line_n ; i++)
printf("%s\n", artix[i]);
}*/