Files
fastfetch/src/common.c
T

268 lines
6.2 KiB
C
Raw Normal View History

2021-02-27 16:40:05 +01:00
#include "fastfetch.h"
2021-02-27 20:31:54 +01:00
#include <malloc.h>
2021-02-27 16:40:05 +01:00
#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);
2021-03-11 17:21:18 +01:00
state->terminal.value = NULL;
state->terminal.error = NULL;
2021-02-27 16:40:05 +01:00
}
2021-03-03 21:03:12 +01:00
void ffDefaultConfig(FFconfig* config)
{
config->color[0] = '\0';
2021-03-05 23:00:28 +01:00
config->logo_spacer = 4;
strcpy(config->seperator, ": ");
2021-03-03 21:03:12 +01:00
config->offsetx = 0;
config->titleLength = 20; // This is overwritten by ffPrintTitle
config->colorLogo = true;
config->showErrors = false;
config->recache = false;
2021-03-06 11:28:38 +01:00
config->cacheSave = true;
config->osShowArchitecture = true;
2021-03-03 21:03:12 +01:00
2021-03-07 11:58:24 +01:00
config->hostShowVersion = true;
2021-03-07 20:35:17 +01:00
config->kernelShowRelease = true;
config->kernelShowVersion = false;
2021-03-05 23:26:05 +01:00
config->shellShowPath = false;
2021-03-03 21:03:12 +01:00
config->batteryShowManufacturer = true;
config->batteryShowModel = true;
config->batteryShowTechnology = true;
config->batteryShowCapacity = true;
config->batteryShowStatus = true;
2021-03-04 20:49:12 +01:00
config->batteryFormat[0] = '\0';
2021-03-03 21:03:12 +01:00
}
2021-02-27 18:30:05 +01:00
void ffPrintKey(FFconfig* config, const char* key)
2021-02-27 16:40:05 +01:00
{
2021-03-05 23:00:28 +01:00
printf(FASTFETCH_TEXT_MODIFIER_BOLT"%s%s"FASTFETCH_TEXT_MODIFIER_RESET"%s", config->color, key, config->seperator);
2021-02-27 16:40:05 +01:00
}
2021-02-27 18:30:05 +01:00
void ffPrintLogoAndKey(FFinstance* instance, const char* key)
2021-02-27 16:40:05 +01:00
{
2021-02-27 18:30:05 +01:00
ffPrintLogoLine(instance);
ffPrintKey(&instance->config, key);
2021-02-27 16:40:05 +01:00
}
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);
}
2021-02-27 18:30:05 +01:00
void ffParsePropFileHome(FFinstance* instance, const char* relativeFile, const char* regex, char* buffer)
2021-02-27 16:40:05 +01:00
{
char absolutePath[512];
2021-02-27 18:30:05 +01:00
strcpy(absolutePath, instance->state.passwd->pw_dir);
2021-02-27 16:40:05 +01:00
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);
}
}
2021-03-11 17:21:18 +01:00
void ffParseFont(char* font, char* buffer)
{
char name[64];
char size[32];
int scanned = sscanf(font, "%[^,], %[^,]", name, size);
if(scanned == 0)
strcpy(buffer, font);
else if(scanned == 1)
strcpy(buffer, name);
else
sprintf(buffer, "%s (%spt)", name, size);
}
2021-02-27 18:30:05 +01:00
void ffPrintError(FFinstance* instance, const char* key, const char* message)
2021-02-27 16:40:05 +01:00
{
2021-02-27 18:30:05 +01:00
if(!instance->config.showErrors)
2021-02-27 16:40:05 +01:00
return;
2021-02-27 18:30:05 +01:00
ffPrintLogoAndKey(instance, key);
2021-02-27 20:31:54 +01:00
printf(FASTFETCH_TEXT_MODIFIER_ERROR"%s"FASTFETCH_TEXT_MODIFIER_RESET"\n", message);
2021-02-27 16:40:05 +01:00
}
2021-02-28 17:10:12 +01:00
void ffTrimTrailingWhitespace(char* buffer)
2021-02-27 16:40:05 +01:00
{
2021-02-28 17:10:12 +01:00
uint32_t end = 0;
for(uint32_t i = 0; buffer[i] != '\0'; i++)
{
if(buffer[i] != ' ')
end = i;
}
if(buffer[end + 1] == ' ')
buffer[end + 1] = '\0';
}
2021-03-01 19:53:20 +01:00
void ffGetFileContent(const char* fileName, char* buffer, uint32_t bufferSize)
2021-02-28 17:10:12 +01:00
{
buffer[0] = '\0';
int fd = open(fileName, O_RDONLY);
if(fd == -1)
return;
ssize_t readed = read(fd, buffer, bufferSize - 1);
if(readed < 1)
return;
close(fd);
if(buffer[readed - 1] == '\n')
buffer[readed - 1] = '\0';
else
buffer[readed] = '\0';
ffTrimTrailingWhitespace(buffer);
}
static const char* getCacheDir(FFinstance* instance)
{
static char cache[256];
static bool init = false;
if(init)
return cache;
2021-02-27 16:40:05 +01:00
const char* xdgCache = getenv("XDG_CACHE_HOME");
if(xdgCache == NULL)
{
2021-02-28 17:10:12 +01:00
strcpy(cache, instance->state.passwd->pw_dir);
strcat(cache, "/.cache");
2021-02-27 16:40:05 +01:00
}
else
{
2021-02-28 17:10:12 +01:00
strcpy(cache, xdgCache);
2021-02-27 16:40:05 +01:00
}
2021-02-28 17:10:12 +01:00
mkdir(cache, S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH); //I hope everybody has a cache folder but whow knews
2021-02-27 16:40:05 +01:00
2021-02-28 17:10:12 +01:00
strcat(cache, "/fastfetch/");
mkdir(cache, S_IRWXU | S_IRGRP | S_IROTH);
init = true;
return cache;
}
2021-02-27 18:30:05 +01:00
bool ffPrintCachedValue(FFinstance* instance, const char* key)
2021-02-27 16:40:05 +01:00
{
2021-02-27 18:30:05 +01:00
if(instance->config.recache)
2021-02-27 16:40:05 +01:00
return false;
char fileName[256];
2021-02-28 17:10:12 +01:00
strcpy(fileName, getCacheDir(instance));
strcat(fileName, key);
2021-02-27 16:40:05 +01:00
char value[1024];
2021-03-01 19:53:20 +01:00
ffGetFileContent(fileName, value, sizeof(value));
2021-02-27 16:40:05 +01:00
2021-02-28 17:10:12 +01:00
if(value[0] == '\0')
2021-02-27 16:40:05 +01:00
return false;
2021-02-27 18:30:05 +01:00
ffPrintLogoAndKey(instance, key);
2021-02-27 16:40:05 +01:00
puts(value);
return true;
}
2021-02-27 18:30:05 +01:00
void ffPrintAndSaveCachedValue(FFinstance* instance, const char* key, const char* value)
2021-02-27 16:40:05 +01:00
{
2021-02-27 18:30:05 +01:00
ffPrintLogoAndKey(instance, key);
2021-02-27 16:40:05 +01:00
puts(value);
2021-03-06 11:28:38 +01:00
if(!instance->config.cacheSave)
return;
2021-02-27 16:40:05 +01:00
char fileName[256];
2021-02-28 17:10:12 +01:00
strcpy(fileName, getCacheDir(instance));
strcat(fileName, key);
2021-02-27 16:40:05 +01:00
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);
}