refactored project structure

This commit is contained in:
Linus Dierheimer
2021-02-27 16:40:05 +01:00
parent 558639086a
commit b44ab16dd1
26 changed files with 219 additions and 211 deletions
+27 -23
View File
@@ -20,33 +20,37 @@ set(PROJECT_VERSION "r${GIT_REV_LIST}.${GIT_REV_PARSE}")
# set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wall -march=native -O3 -pipe -fno-plt") # for testing
configure_file(src/fastfetch_config.h.in fastfetch_config.h)
include_directories(${PROJECT_BINARY_DIR})
include_directories(
${PROJECT_BINARY_DIR}
${PROJECT_SOURCE_DIR}/src
)
add_executable(${PROJECT_NAME}
src/fastfetch.c
src/break.c
src/util.c
src/logo.c
src/title.c
src/seperator.c
src/os.c
src/host.c
src/kernel.c
src/uptime.c
src/packages.c
src/shell.c
src/resolution.c
src/desktopenvironment.c
src/theme.c
src/icons.c
src/font.c
src/terminal.c
src/cpu.c
src/gpu.c
src/memory.c
src/disk.c
src/battery.c
src/locale.c
src/colors.c
src/modules/break.c
src/modules/title.c
src/modules/seperator.c
src/modules/os.c
src/modules/host.c
src/modules/kernel.c
src/modules/uptime.c
src/modules/packages.c
src/modules/shell.c
src/modules/resolution.c
src/modules/desktopenvironment.c
src/modules/theme.c
src/modules/icons.c
src/modules/font.c
src/modules/terminal.c
src/modules/cpu.c
src/modules/gpu.c
src/modules/memory.c
src/modules/disk.c
src/modules/battery.c
src/modules/locale.c
src/modules/colors.c
)
target_link_libraries(${PROJECT_NAME}
+1 -187
View File
@@ -2,178 +2,6 @@
#include "fastfetch_config.h"
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
void ffPrintKey(FFstate* state, const char* key)
{
printf(FASTFETCH_TEXT_MODIFIER_BOLT"%s%s"FASTFETCH_TEXT_MODIFIER_RESET": ", state->color, key);
}
void ffPrintLogoAndKey(FFstate* state, const char* key)
{
ffPrintLogoLine(state);
ffPrintKey(state, key);
}
void ffParsePropFile(const char* fileName, const char* regex, char* buffer)
{
buffer[0] = '\0'; //If an error occures, this is the indicator
char* line = NULL;
size_t len;
FILE* file = fopen(fileName, "r");
if(file == NULL)
return; // handle errors in higher functions
while (getline(&line, &len, file) != -1)
{
if (sscanf(line, regex, buffer) > 0)
break;
}
fclose(file);
free(line);
}
void ffParsePropFileHome(FFstate* state, const char* relativeFile, const char* regex, char* buffer)
{
char absolutePath[512];
strcpy(absolutePath, state->passwd->pw_dir);
strcat(absolutePath, "/");
strcat(absolutePath, relativeFile);
ffParsePropFile(absolutePath, regex, buffer);
}
static inline bool strSet(const char* str)
{
return str != NULL && str[0] != '\0';
}
void ffPrintGtkPretty(const char* gtk2, const char* gtk3, const char* gtk4)
{
if(strSet(gtk2) && strSet(gtk3) && strSet(gtk4))
{
if((strcmp(gtk2, gtk3) == 0) && (strcmp(gtk2, gtk4) == 0))
printf("%s [GTK2/3/4]", gtk2);
else if(strcmp(gtk2, gtk3) == 0)
printf("%s [GTK2/3], %s [GTK4]", gtk2, gtk4);
else if(strcmp(gtk3, gtk4) == 0)
printf("%s [GTK2], %s [GTK3/4]", gtk2, gtk3);
else
printf("%s [GTK2], %s [GTK3], %s [GTK4]", gtk2, gtk3, gtk4);
}
else if(strSet(gtk2) && strSet(gtk3))
{
if(strcmp(gtk2, gtk3) == 0)
printf("%s [GTK2/3]", gtk2);
else
printf("%s [GTK2], %s [GTK3]", gtk2, gtk3);
}
else if(strSet(gtk3) && strSet(gtk4))
{
if(strcmp(gtk3, gtk4) == 0)
printf("%s [GTK3/4]", gtk3);
else
printf("%s [GTK3], %s [GTK4]", gtk3, gtk4);
}
else if(strSet(gtk2))
{
printf("%s [GTK2]", gtk2);
}
else if(strSet(gtk3))
{
printf("%s [GTK3]", gtk3);
}
else if(strSet(gtk4))
{
printf("%s [GTK4]", gtk4);
}
}
void ffPrintError(FFstate* state, const char* key, const char* message)
{
if(!state->showErrors)
return;
ffPrintLogoAndKey(state, key);
printf(FASTFETCH_TEXT_MODIFIER_ERROR"%s\n"FASTFETCH_TEXT_MODIFIER_RESET, message);
}
static void getCacheFileName(FFstate* state, const char* key, char* buffer)
{
const char* xdgCache = getenv("XDG_CACHE_HOME");
if(xdgCache == NULL)
{
strcpy(buffer, state->passwd->pw_dir);
strcat(buffer, "/.cache");
}
else
{
strcpy(buffer, xdgCache);
}
mkdir(buffer, S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH); //I hope everybody has a cache folder but whow knews
strcat(buffer, "/fastfetch/");
mkdir(buffer, S_IRWXU | S_IRGRP | S_IROTH);
strcat(buffer, key);
}
bool ffPrintCachedValue(FFstate* state, const char* key)
{
if(state->recache)
return false;
char fileName[256];
getCacheFileName(state, key, fileName);
int fd = open(fileName, O_RDONLY);
if(fd == -1)
return false;
char value[1024];
ssize_t readed = read(fd, value, sizeof(value) - 1);
if(readed < 1)
return false;
close(fd);
value[readed] = '\0'; //Somehow read doesn't alway do this
ffPrintLogoAndKey(state, key);
puts(value);
return true;
}
void ffPrintAndSaveCachedValue(FFstate* state, const char* key, const char* value)
{
ffPrintLogoAndKey(state, key);
puts(value);
char fileName[256];
getCacheFileName(state, key, fileName);
int fd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(fd == -1)
return;
size_t len = strlen(value);
bool failed = write(fd, value, len) != len;
close(fd);
if(failed)
unlink(fileName);
}
static void printHelp()
{
@@ -219,20 +47,6 @@ static void printCommandHelp(const char* command)
printf("No specific help for command %s provided\n", command);
}
static void initState(FFstate* state)
{
state->current_row = 0;
state->passwd = getpwuid(getuid());
uname(&state->utsname);
sysinfo(&state->sysinfo);
state->logo_seperator = 4;
state->offsetx = 0;
state->titleLength = 20; // This is overwritten by ffPrintTitle
state->colorLogo = true;
state->showErrors = false;
state->recache = false;
}
static void parseArguments(int argc, char** argv, FFstate* state)
{
bool colorText = true;
@@ -352,7 +166,7 @@ static void parseArguments(int argc, char** argv, FFstate* state)
int main(int argc, char** argv)
{
FFstate state;
initState(&state);
ffInitState(&state);
parseArguments(argc, argv, &state);
//Start the printing
+2 -1
View File
@@ -49,7 +49,8 @@ typedef struct FFstate
struct sysinfo sysinfo;
} FFstate;
//Helper functions
//Util functions
void ffInitState(FFstate* state);
void ffPrintLogoLine(FFstate* state);
void ffPrintKey(FFstate* state, const char* key);
void ffPrintLogoAndKey(FFstate* state, const char* key);
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
View File
+189
View File
@@ -0,0 +1,189 @@
#include "fastfetch.h"
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
void ffInitState(FFstate* state)
{
state->current_row = 0;
state->passwd = getpwuid(getuid());
uname(&state->utsname);
sysinfo(&state->sysinfo);
state->logo_seperator = 4;
state->offsetx = 0;
state->titleLength = 20; // This is overwritten by ffPrintTitle
state->colorLogo = true;
state->showErrors = false;
state->recache = false;
}
void ffPrintKey(FFstate* state, const char* key)
{
printf(FASTFETCH_TEXT_MODIFIER_BOLT"%s%s"FASTFETCH_TEXT_MODIFIER_RESET": ", state->color, key);
}
void ffPrintLogoAndKey(FFstate* state, const char* key)
{
ffPrintLogoLine(state);
ffPrintKey(state, key);
}
void ffParsePropFile(const char* fileName, const char* regex, char* buffer)
{
buffer[0] = '\0'; //If an error occures, this is the indicator
char* line = NULL;
size_t len;
FILE* file = fopen(fileName, "r");
if(file == NULL)
return; // handle errors in higher functions
while (getline(&line, &len, file) != -1)
{
if (sscanf(line, regex, buffer) > 0)
break;
}
fclose(file);
free(line);
}
void ffParsePropFileHome(FFstate* state, const char* relativeFile, const char* regex, char* buffer)
{
char absolutePath[512];
strcpy(absolutePath, state->passwd->pw_dir);
strcat(absolutePath, "/");
strcat(absolutePath, relativeFile);
ffParsePropFile(absolutePath, regex, buffer);
}
static inline bool strSet(const char* str)
{
return str != NULL && str[0] != '\0';
}
void ffPrintGtkPretty(const char* gtk2, const char* gtk3, const char* gtk4)
{
if(strSet(gtk2) && strSet(gtk3) && strSet(gtk4))
{
if((strcmp(gtk2, gtk3) == 0) && (strcmp(gtk2, gtk4) == 0))
printf("%s [GTK2/3/4]", gtk2);
else if(strcmp(gtk2, gtk3) == 0)
printf("%s [GTK2/3], %s [GTK4]", gtk2, gtk4);
else if(strcmp(gtk3, gtk4) == 0)
printf("%s [GTK2], %s [GTK3/4]", gtk2, gtk3);
else
printf("%s [GTK2], %s [GTK3], %s [GTK4]", gtk2, gtk3, gtk4);
}
else if(strSet(gtk2) && strSet(gtk3))
{
if(strcmp(gtk2, gtk3) == 0)
printf("%s [GTK2/3]", gtk2);
else
printf("%s [GTK2], %s [GTK3]", gtk2, gtk3);
}
else if(strSet(gtk3) && strSet(gtk4))
{
if(strcmp(gtk3, gtk4) == 0)
printf("%s [GTK3/4]", gtk3);
else
printf("%s [GTK3], %s [GTK4]", gtk3, gtk4);
}
else if(strSet(gtk2))
{
printf("%s [GTK2]", gtk2);
}
else if(strSet(gtk3))
{
printf("%s [GTK3]", gtk3);
}
else if(strSet(gtk4))
{
printf("%s [GTK4]", gtk4);
}
}
void ffPrintError(FFstate* state, const char* key, const char* message)
{
if(!state->showErrors)
return;
ffPrintLogoAndKey(state, key);
printf(FASTFETCH_TEXT_MODIFIER_ERROR"%s\n"FASTFETCH_TEXT_MODIFIER_RESET, message);
}
static void getCacheFileName(FFstate* state, const char* key, char* buffer)
{
const char* xdgCache = getenv("XDG_CACHE_HOME");
if(xdgCache == NULL)
{
strcpy(buffer, state->passwd->pw_dir);
strcat(buffer, "/.cache");
}
else
{
strcpy(buffer, xdgCache);
}
mkdir(buffer, S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH); //I hope everybody has a cache folder but whow knews
strcat(buffer, "/fastfetch/");
mkdir(buffer, S_IRWXU | S_IRGRP | S_IROTH);
strcat(buffer, key);
}
bool ffPrintCachedValue(FFstate* state, const char* key)
{
if(state->recache)
return false;
char fileName[256];
getCacheFileName(state, key, fileName);
int fd = open(fileName, O_RDONLY);
if(fd == -1)
return false;
char value[1024];
ssize_t readed = read(fd, value, sizeof(value) - 1);
if(readed < 1)
return false;
close(fd);
value[readed] = '\0'; //Somehow read doesn't alway do this
ffPrintLogoAndKey(state, key);
puts(value);
return true;
}
void ffPrintAndSaveCachedValue(FFstate* state, const char* key, const char* value)
{
ffPrintLogoAndKey(state, key);
puts(value);
char fileName[256];
getCacheFileName(state, key, fileName);
int fd = open(fileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if(fd == -1)
return;
size_t len = strlen(value);
bool failed = write(fd, value, len) != len;
close(fd);
if(failed)
unlink(fileName);
}