OS: major code refactoring

Split `fastfetch.h` to make it possible to print OS module with different options
This commit is contained in:
李通洲
2023-03-07 14:56:13 +08:00
parent de2a346601
commit 6cb073e6a5
11 changed files with 223 additions and 29 deletions
+4 -5
View File
@@ -15,6 +15,8 @@
#include <signal.h>
#endif
#include "modules/os/os.h"
static void initState(FFstate* state)
{
state->logoWidth = 0;
@@ -69,7 +71,7 @@ static void defaultConfig(FFinstance* instance)
instance->config.multithreading = true;
instance->config.stat = false;
initModuleArg(&instance->config.os);
ffInitOSOptions(&instance->config.os);
initModuleArg(&instance->config.host);
initModuleArg(&instance->config.bios);
initModuleArg(&instance->config.board);
@@ -180,8 +182,6 @@ static void defaultConfig(FFinstance* instance)
instance->config.weatherTimeout = 0;
ffStrbufInitS(&instance->config.weatherOutputFormat, "%t+-+%C+(%l)");
ffStrbufInitA(&instance->config.osFile, 0);
ffStrbufInitA(&instance->config.playerName, 0);
instance->config.percentType = 1;
@@ -333,7 +333,7 @@ static void destroyConfig(FFinstance* instance)
ffStrbufDestroy(&instance->config.colorTitle);
ffStrbufDestroy(&instance->config.separator);
destroyModuleArg(&instance->config.os);
ffDestroyOSOptions(&instance->config.os);
destroyModuleArg(&instance->config.host);
destroyModuleArg(&instance->config.bios);
destroyModuleArg(&instance->config.board);
@@ -412,7 +412,6 @@ static void destroyConfig(FFinstance* instance)
ffStrbufDestroy(&instance->config.localIpNamePrefix);
ffStrbufDestroy(&instance->config.publicIpUrl);
ffStrbufDestroy(&instance->config.weatherOutputFormat);
ffStrbufDestroy(&instance->config.osFile);
ffStrbufDestroy(&instance->config.playerName);
ffStrbufDestroy(&instance->config.commandShell);
+120
View File
@@ -0,0 +1,120 @@
#include "common/option.h"
#include "util/stringUtils.h"
// Return start position of the inner key if the argument key belongs to the module specified, NULL otherwise
const char* ffOptionTestPrefix(const char* argumentKey, const char* moduleName)
{
const char* subKey = argumentKey;
if(!(subKey[0] == '-' && subKey[1] == '-'))
return NULL;
subKey += 2;
uint32_t moduleNameLen = (uint32_t)strlen(moduleName);
if(strncasecmp(subKey, moduleName, moduleNameLen) != 0)
return NULL;
subKey += moduleNameLen;
if(subKey[0] != '-')
return NULL;
subKey += 1;
return subKey;
}
bool ffOptionParseModuleArgs(const char* argumentKey, const char* subKey, const char* value, FFModuleArgs* result)
{
if(strcasecmp(subKey, "key") == 0)
{
ffOptionParseString(argumentKey, value, &result->key);
return true;
}
else if(strcasecmp(subKey, "format") == 0)
{
ffOptionParseString(argumentKey, value, &result->outputFormat);
return true;
}
else if(strcasecmp(subKey, "error") == 0)
{
ffOptionParseString(argumentKey, value, &result->errorFormat);
return true;
}
return false;
}
void ffOptionParseString(const char* argumentKey, const char* value, FFstrbuf* buffer)
{
if(value == NULL)
{
fprintf(stderr, "Error: usage: %s <str>\n", argumentKey);
exit(477);
}
ffStrbufSetS(buffer, value);
}
uint32_t ffOptionParseUInt32(const char* argumentKey, const char* value)
{
if(value == NULL)
{
fprintf(stderr, "Error: usage: %s <num>\n", argumentKey);
exit(480);
}
char* end;
uint32_t num = (uint32_t) strtoul(value, &end, 10);
if(*end != '\0')
{
fprintf(stderr, "Error: usage: %s <num>\n", argumentKey);
exit(479);
}
return num;
}
void ffOptionParseEnum(const char* argumentKey, const char* requestedKey, void* result, ...)
{
if(requestedKey == NULL)
{
fprintf(stderr, "Error: usage: %s <value>\n", argumentKey);
exit(476);
}
va_list args;
va_start(args, result);
while(true)
{
const char* key = va_arg(args, const char*);
if(key == NULL)
break;
int value = va_arg(args, int); //C standard guarantees that enumeration constants are presented as ints
if(strcasecmp(requestedKey, key) == 0)
{
*(int*)result = value;
va_end(args);
return;
}
}
va_end(args);
fprintf(stderr, "Error: unknown %s value: %s\n", argumentKey, requestedKey);
exit(478);
}
void ffOptionInitModuleArg(FFModuleArgs* args)
{
ffStrbufInit(&args->key);
ffStrbufInit(&args->outputFormat);
ffStrbufInit(&args->errorFormat);
}
void ffOptionDestroyModuleArg(FFModuleArgs* args)
{
ffStrbufDestroy(&args->key);
ffStrbufDestroy(&args->outputFormat);
ffStrbufDestroy(&args->errorFormat);
}
+18
View File
@@ -0,0 +1,18 @@
#pragma once
#include "util/FFstrbuf.h"
typedef struct FFModuleArgs
{
FFstrbuf key;
FFstrbuf outputFormat;
FFstrbuf errorFormat;
} FFModuleArgs;
const char* ffOptionTestPrefix(const char* argumentKey, const char* moduleName);
bool ffOptionParseModuleArgs(const char* argumentKey, const char* pkey, const char* value, FFModuleArgs* result);
void ffOptionParseString(const char* argumentKey, const char* value, FFstrbuf* buffer);
uint32_t ffOptionParseUInt32(const char* argumentKey, const char* value);
void ffOptionParseEnum(const char* argumentKey, const char* requestedKey, void* result, ...);
void ffOptionInitModuleArg(FFModuleArgs* args);
void ffOptionDestroyModuleArg(FFModuleArgs* args);