mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 16:26:39 +01:00
using strbuf for loading/saving cached values
This commit is contained in:
+30
-30
@@ -251,31 +251,28 @@ void ffGetFileContent(const char* fileName, FFstrbuf* buffer)
|
||||
close(fd);
|
||||
}
|
||||
|
||||
static const char* getCacheDir(FFinstance* instance)
|
||||
static const FFstrbuf* getCacheDir(FFinstance* instance)
|
||||
{
|
||||
static char cache[256];
|
||||
static FFstrbuf cacheDir;
|
||||
static bool init = false;
|
||||
if(init)
|
||||
return cache;
|
||||
return &cacheDir;
|
||||
|
||||
const char* xdgCache = getenv("XDG_CACHE_HOME");
|
||||
if(xdgCache == NULL)
|
||||
ffStrbufInitS(&cacheDir, getenv("XDG_CACHE_HOME"));
|
||||
|
||||
if(ffStrbufIsEmpty(&cacheDir))
|
||||
{
|
||||
strcpy(cache, instance->state.passwd->pw_dir);
|
||||
strcat(cache, "/.cache");
|
||||
}
|
||||
else
|
||||
{
|
||||
strcpy(cache, xdgCache);
|
||||
ffStrbufSetS(&cacheDir, instance->state.passwd->pw_dir);
|
||||
ffStrbufAppendS(&cacheDir, "/.cache/");
|
||||
}
|
||||
|
||||
mkdir(cache, S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH); //I hope everybody has a cache folder but whow knews
|
||||
|
||||
strcat(cache, "/fastfetch/");
|
||||
mkdir(cache, S_IRWXU | S_IRGRP | S_IROTH);
|
||||
mkdir(cacheDir.chars, S_IRWXU | S_IXGRP | S_IRGRP | S_IXOTH | S_IROTH); //I hope everybody has a cache folder but whow knews
|
||||
|
||||
ffStrbufAppendS(&cacheDir, "fastfetch/");
|
||||
mkdir(cacheDir.chars, S_IRWXU | S_IRGRP | S_IROTH);
|
||||
|
||||
init = true;
|
||||
return cache;
|
||||
return &cacheDir;
|
||||
}
|
||||
|
||||
bool ffPrintCachedValue(FFinstance* instance, const char* key)
|
||||
@@ -283,13 +280,15 @@ bool ffPrintCachedValue(FFinstance* instance, const char* key)
|
||||
if(instance->config.recache)
|
||||
return false;
|
||||
|
||||
FFstrbuf filename;
|
||||
ffStrbufInitF(&filename, "%s%s", getCacheDir(instance), key);
|
||||
FFstrbuf cacheFile;
|
||||
ffStrbufInit(&cacheFile);
|
||||
ffStrbufAppend(&cacheFile, getCacheDir(instance));
|
||||
ffStrbufAppendS(&cacheFile, key);
|
||||
|
||||
FF_STRBUF_CREATE(value);
|
||||
ffGetFileContent(filename.chars, &value);
|
||||
ffGetFileContent(cacheFile.chars, &value);
|
||||
|
||||
ffStrbufDestroy(&filename);
|
||||
ffStrbufDestroy(&cacheFile);
|
||||
|
||||
if(ffStrbufIsEmpty(&value))
|
||||
{
|
||||
@@ -304,30 +303,31 @@ bool ffPrintCachedValue(FFinstance* instance, const char* key)
|
||||
return true;
|
||||
}
|
||||
|
||||
void ffPrintAndSaveCachedValue(FFinstance* instance, const char* key, const char* value)
|
||||
void ffPrintAndSaveCachedValue(FFinstance* instance, const char* key, FFstrbuf* value)
|
||||
{
|
||||
puts(value);
|
||||
ffStrbufWriteTo(value, stdout);
|
||||
putchar('\n');
|
||||
|
||||
if(!instance->config.cacheSave)
|
||||
return;
|
||||
|
||||
FFstrbuf filename;
|
||||
ffStrbufInitF(&filename, "%s%s", getCacheDir(instance), key);
|
||||
FFstrbuf cacheFile;
|
||||
ffStrbufInit(&cacheFile);
|
||||
ffStrbufAppend(&cacheFile, getCacheDir(instance));
|
||||
ffStrbufAppendS(&cacheFile, key);
|
||||
|
||||
int fd = open(filename.chars, O_WRONLY | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
|
||||
int fd = open(cacheFile.chars, 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;
|
||||
bool failed = write(fd, value->chars, value->length) != value->length;
|
||||
|
||||
close(fd);
|
||||
|
||||
if(failed)
|
||||
unlink(filename.chars);
|
||||
unlink(cacheFile.chars);
|
||||
|
||||
ffStrbufDestroy(&filename);
|
||||
ffStrbufDestroy(&cacheFile);
|
||||
}
|
||||
|
||||
void ffParseFormatString(FFstrbuf* buffer, FFstrbuf* formatstr, uint32_t numArgs, ...)
|
||||
|
||||
+1
-1
@@ -124,7 +124,7 @@ void ffFormatGtkPretty(FFstrbuf* buffer, const char* gtk2, const char* gtk3, con
|
||||
void ffPrintError(FFinstance* instance, const char* key, const char* message, ...);
|
||||
void ffTrimTrailingWhitespace(char* buffer);
|
||||
bool ffPrintCachedValue(FFinstance* instance, const char* key);
|
||||
void ffPrintAndSaveCachedValue(FFinstance* instance, const char* key, const char* value);
|
||||
void ffPrintAndSaveCachedValue(FFinstance* instance, const char* key, FFstrbuf* value);
|
||||
void ffParseFormatString(FFstrbuf* buffer, FFstrbuf* formatstr, uint32_t numArgs, ...);
|
||||
void ffFinish(FFinstance* instance);
|
||||
|
||||
|
||||
+1
-1
@@ -50,6 +50,6 @@ void ffPrintCPU(FFinstance* instance)
|
||||
);
|
||||
}
|
||||
|
||||
ffPrintAndSaveCachedValue(instance, "CPU", cpu.chars);
|
||||
ffPrintAndSaveCachedValue(instance, "CPU", &cpu);
|
||||
ffStrbufDestroy(&cpu);
|
||||
}
|
||||
+1
-1
@@ -39,7 +39,7 @@ static void handleGPU(FFinstance* instance, struct pci_access* pacc, struct pci_
|
||||
);
|
||||
}
|
||||
|
||||
ffPrintAndSaveCachedValue(instance, key, gpu.chars);
|
||||
ffPrintAndSaveCachedValue(instance, key, &gpu);
|
||||
ffStrbufDestroy(&gpu);
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -45,7 +45,7 @@ void ffPrintHost(FFinstance* instance)
|
||||
ffStrbufSetF(&host, "%s %s", name.chars, version.chars);
|
||||
}
|
||||
|
||||
ffPrintAndSaveCachedValue(instance, "Host", host.chars);
|
||||
ffPrintAndSaveCachedValue(instance, "Host", &host);
|
||||
ffStrbufDestroy(&host);
|
||||
|
||||
ffStrbufDestroy(&family);
|
||||
|
||||
@@ -55,6 +55,6 @@ void ffPrintLocale(FFinstance* instance)
|
||||
}
|
||||
|
||||
|
||||
ffPrintAndSaveCachedValue(instance, "Locale", locale.chars);
|
||||
ffPrintAndSaveCachedValue(instance, "Locale", &locale);
|
||||
ffStrbufDestroy(&locale);
|
||||
}
|
||||
|
||||
+1
-1
@@ -29,6 +29,6 @@ void ffPrintOS(FFinstance* instance)
|
||||
ffStrbufSetF(&os, "%s %s", name, instance->state.utsname.machine);
|
||||
}
|
||||
|
||||
ffPrintAndSaveCachedValue(instance, "OS", os.chars);
|
||||
ffPrintAndSaveCachedValue(instance, "OS", &os);
|
||||
ffStrbufDestroy(&os);
|
||||
}
|
||||
@@ -95,6 +95,6 @@ void ffPrintResolution(FFinstance* instance)
|
||||
ffStrbufAppendF(&resolution, " @ %iHz", currentRate);
|
||||
}
|
||||
|
||||
ffPrintAndSaveCachedValue(instance, "Resolution", resolution.chars);
|
||||
ffPrintAndSaveCachedValue(instance, "Resolution", &resolution);
|
||||
ffStrbufDestroy(&resolution);
|
||||
}
|
||||
+1
-1
@@ -43,6 +43,6 @@ void ffPrintShell(FFinstance* instance)
|
||||
ffStrbufSetS(&shell, shellName);
|
||||
}
|
||||
|
||||
ffPrintAndSaveCachedValue(instance, "Shell", shell.chars);
|
||||
ffPrintAndSaveCachedValue(instance, "Shell", &shell);
|
||||
ffStrbufDestroy(&shell);
|
||||
}
|
||||
+10
-4
@@ -9,6 +9,12 @@ void ffStrbufInit(FFstrbuf* strbuf)
|
||||
ffStrbufInitA(strbuf, FASTFETCH_STRBUF_DEFAULT_ALLOC);
|
||||
}
|
||||
|
||||
void ffStrbufInitCopy(FFstrbuf* strbuf, const FFstrbuf* src)
|
||||
{
|
||||
ffStrbufInitA(strbuf, src->allocated);
|
||||
ffStrbufAppend(strbuf, src);
|
||||
}
|
||||
|
||||
void ffStrbufInitS(FFstrbuf* strbuf, const char* value)
|
||||
{
|
||||
ffStrbufInitAS(strbuf, FASTFETCH_STRBUF_DEFAULT_ALLOC, value);
|
||||
@@ -98,7 +104,7 @@ bool ffStrbufIsEmpty(FFstrbuf* strbuf)
|
||||
return strbuf->length == 0;
|
||||
}
|
||||
|
||||
void ffStrbufSet(FFstrbuf* strbuf, FFstrbuf* value)
|
||||
void ffStrbufSet(FFstrbuf* strbuf, const FFstrbuf* value)
|
||||
{
|
||||
ffStrbufClear(strbuf);
|
||||
ffStrbufAppendNS(strbuf, value->length, value->chars);
|
||||
@@ -137,7 +143,7 @@ void ffStrbufSetVF(FFstrbuf* strbuf, const char* format, va_list arguments)
|
||||
ffStrbufAppendVF(strbuf, format, arguments);
|
||||
}
|
||||
|
||||
void ffStrbufAppend(FFstrbuf* strbuf, FFstrbuf* value)
|
||||
void ffStrbufAppend(FFstrbuf* strbuf, const FFstrbuf* value)
|
||||
{
|
||||
ffStrbufAppendNS(strbuf, value->length, value->chars);
|
||||
}
|
||||
@@ -232,7 +238,7 @@ void ffStrbufWriteTo(FFstrbuf* strbuf, FILE* file)
|
||||
fwrite(strbuf->chars, sizeof(char), strbuf->length, file);
|
||||
}
|
||||
|
||||
int ffStrbufComp(FFstrbuf* strbuf, FFstrbuf* comp)
|
||||
int ffStrbufComp(FFstrbuf* strbuf, const FFstrbuf* comp)
|
||||
{
|
||||
return ffStrbufCompNS(strbuf, comp->length, comp->chars);
|
||||
}
|
||||
@@ -247,7 +253,7 @@ int ffStrbufCompNS(FFstrbuf* strbuf, uint32_t length, const char* comp)
|
||||
return strncasecmp(strbuf->chars, comp, length);
|
||||
}
|
||||
|
||||
int ffStrbufIgnCaseComp(FFstrbuf* strbuf, FFstrbuf* comp)
|
||||
int ffStrbufIgnCaseComp(FFstrbuf* strbuf, const FFstrbuf* comp)
|
||||
{
|
||||
return ffStrbufIgnCaseCompNS(strbuf, comp->length, comp->chars);
|
||||
}
|
||||
|
||||
+5
-4
@@ -20,6 +20,7 @@ typedef struct FFstrbuf
|
||||
#define FF_STRBUF_CREATE(name) FFstrbuf name; ffStrbufInit(&name);
|
||||
|
||||
void ffStrbufInit(FFstrbuf* strbuf);
|
||||
void ffStrbufInitCopy(FFstrbuf* strbuf, const FFstrbuf* src);
|
||||
void ffStrbufInitS(FFstrbuf* strbuf, const char* value);
|
||||
void ffStrbufInitNS(FFstrbuf* strbuf, uint32_t length, const char* value);
|
||||
void ffStrbufInitC(FFstrbuf* strbuf, const char c);
|
||||
@@ -36,14 +37,14 @@ void ffStrbufEnsureFree(FFstrbuf* strbuf, uint32_t free);
|
||||
void ffStrbufClear(FFstrbuf* strbuf);
|
||||
bool ffStrbufIsEmpty(FFstrbuf* strbuf);
|
||||
|
||||
void ffStrbufSet(FFstrbuf* strbuf, FFstrbuf* value);
|
||||
void ffStrbufSet(FFstrbuf* strbuf, const FFstrbuf* value);
|
||||
void ffStrbufSetS(FFstrbuf* strbuf, const char* value);
|
||||
void ffStrbufSetNS(FFstrbuf* strbuf, uint32_t length, const char* value);
|
||||
void ffStrbufSetC(FFstrbuf* strbuf, const char c);
|
||||
void ffStrbufSetF(FFstrbuf* strbuf, const char* format, ...);
|
||||
void ffStrbufSetVF(FFstrbuf* strbuf, const char* format, va_list arguments);
|
||||
|
||||
void ffStrbufAppend(FFstrbuf* strbuf, FFstrbuf* value);
|
||||
void ffStrbufAppend(FFstrbuf* strbuf, const FFstrbuf* value);
|
||||
void ffStrbufAppendS(FFstrbuf* strbuf, const char* value);
|
||||
void ffStrbufAppendNS(FFstrbuf* strbuf, uint32_t length, const char* value);
|
||||
void ffStrbufAppendC(FFstrbuf* strbuf, const char c);
|
||||
@@ -54,10 +55,10 @@ char ffStrbufGetC(FFstrbuf* strbuf, uint32_t index);
|
||||
|
||||
void ffStrbufWriteTo(FFstrbuf* strbuf, FILE* file);
|
||||
|
||||
int ffStrbufComp(FFstrbuf* strbuf, FFstrbuf* comp);
|
||||
int ffStrbufComp(FFstrbuf* strbuf, const FFstrbuf* comp);
|
||||
int ffStrbufCompS(FFstrbuf* strbuf, const char* comp);
|
||||
int ffStrbufCompNS(FFstrbuf* strbuf, uint32_t length, const char* comp);
|
||||
int ffStrbufIgnCaseComp(FFstrbuf* strbuf, FFstrbuf* comp);
|
||||
int ffStrbufIgnCaseComp(FFstrbuf* strbuf, const FFstrbuf* comp);
|
||||
int ffStrbufIgnCaseCompS(FFstrbuf* strbuf, const char* comp);
|
||||
int ffStrbufIgnCaseCompNS(FFstrbuf* strbuf, uint32_t length, const char* comp);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user