mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
OS: major code refactoring
Split `fastfetch.h` to make it possible to print OS module with different options
This commit is contained in:
+2
-1
@@ -232,6 +232,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/common/format.c
|
||||
src/common/init.c
|
||||
src/common/library.c
|
||||
src/common/option.c
|
||||
src/common/parsing.c
|
||||
src/common/printing.c
|
||||
src/common/properties.c
|
||||
@@ -286,7 +287,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/memory.c
|
||||
src/modules/opencl.c
|
||||
src/modules/opengl.c
|
||||
src/modules/os.c
|
||||
src/modules/os/os.c
|
||||
src/modules/packages.c
|
||||
src/modules/player.c
|
||||
src/modules/poweradapter.c
|
||||
|
||||
+4
-5
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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);
|
||||
@@ -96,9 +96,9 @@ static void getUbuntuFlavour(FFOSResult* result)
|
||||
|
||||
static void detectOS(FFOSResult* os, const FFinstance* instance)
|
||||
{
|
||||
if(instance->config.osFile.length > 0)
|
||||
if(instance->config.os.file.length > 0)
|
||||
{
|
||||
parseFile(instance->config.osFile.chars, os);
|
||||
parseFile(instance->config.os.file.chars, os);
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
+4
-4
@@ -17,6 +17,8 @@
|
||||
|
||||
#pragma GCC diagnostic ignored "-Wsign-conversion"
|
||||
|
||||
#include "modules/os/os.h"
|
||||
|
||||
typedef struct CustomValue
|
||||
{
|
||||
bool printKey;
|
||||
@@ -1129,7 +1131,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
//Module args options//
|
||||
///////////////////////
|
||||
|
||||
else if(optionParseModuleArgs(key, value, "os", &instance->config.os)) {}
|
||||
else if(ffParseOSCommandOptions(&instance->config.os, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "host", &instance->config.host)) {}
|
||||
else if(optionParseModuleArgs(key, value, "bios", &instance->config.bios)) {}
|
||||
else if(optionParseModuleArgs(key, value, "board", &instance->config.board)) {}
|
||||
@@ -1311,8 +1313,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_COMPACT_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_COMPACT_BIT);
|
||||
else if(strcasecmp(key, "--localip-name-prefix") == 0)
|
||||
optionParseString(key, value, &instance->config.localIpNamePrefix);
|
||||
else if(strcasecmp(key, "--os-file") == 0)
|
||||
optionParseString(key, value, &instance->config.osFile);
|
||||
else if(strcasecmp(key, "--player-name") == 0)
|
||||
optionParseString(key, value, &instance->config.playerName);
|
||||
else if(strcasecmp(key, "--publicip-url") == 0)
|
||||
@@ -1415,7 +1415,7 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
||||
else if(strcasecmp(line, "separator") == 0)
|
||||
ffPrintSeparator(instance);
|
||||
else if(strcasecmp(line, "os") == 0)
|
||||
ffPrintOS(instance);
|
||||
ffPrintOS(instance, &instance->config.os);
|
||||
else if(strcasecmp(line, "host") == 0)
|
||||
ffPrintHost(instance);
|
||||
else if(strcasecmp(line, "bios") == 0)
|
||||
|
||||
+3
-11
@@ -18,6 +18,8 @@ static inline void ffUnused(int dummy, ...) { (void) dummy; }
|
||||
|
||||
#define FASTFETCH_LOGO_MAX_COLORS 9 //two digits would make parsing much more complicated (index 1 - 9)
|
||||
|
||||
#include "modules/os/option.h"
|
||||
|
||||
typedef enum FFLogoType
|
||||
{
|
||||
FF_LOGO_TYPE_AUTO, //if something is given, first try builtin, then file. Otherwise detect logo
|
||||
@@ -80,13 +82,6 @@ typedef enum FFGLType
|
||||
FF_GL_TYPE_OSMESA
|
||||
} FFGLType;
|
||||
|
||||
typedef struct FFModuleArgs
|
||||
{
|
||||
FFstrbuf key;
|
||||
FFstrbuf outputFormat;
|
||||
FFstrbuf errorFormat;
|
||||
} FFModuleArgs;
|
||||
|
||||
typedef enum FFLocalIpType
|
||||
{
|
||||
FF_LOCALIP_TYPE_NONE,
|
||||
@@ -138,7 +133,7 @@ typedef struct FFconfig
|
||||
bool multithreading;
|
||||
bool stat;
|
||||
|
||||
FFModuleArgs os;
|
||||
FFOSOptions os;
|
||||
FFModuleArgs host;
|
||||
FFModuleArgs bios;
|
||||
FFModuleArgs board;
|
||||
@@ -249,8 +244,6 @@ typedef struct FFconfig
|
||||
|
||||
FFSoundType soundType;
|
||||
|
||||
FFstrbuf osFile;
|
||||
|
||||
FFstrbuf playerName;
|
||||
|
||||
uint32_t percentType;
|
||||
@@ -316,7 +309,6 @@ void ffPrintCustom(FFinstance* instance, const char* key, const char* value);
|
||||
void ffPrintBreak(FFinstance* instance);
|
||||
void ffPrintTitle(FFinstance* instance);
|
||||
void ffPrintSeparator(FFinstance* instance);
|
||||
void ffPrintOS(FFinstance* instance);
|
||||
void ffPrintHost(FFinstance* instance);
|
||||
void ffPrintBios(FFinstance* instance);
|
||||
void ffPrintBoard(FFinstance* instance);
|
||||
|
||||
+3
-1
@@ -1,5 +1,7 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include "modules/os/os.h"
|
||||
|
||||
int main(int argc, char** argv)
|
||||
{
|
||||
//Disable compiler warnings
|
||||
@@ -20,7 +22,7 @@ int main(int argc, char** argv)
|
||||
//Printing
|
||||
ffPrintTitle(&instance);
|
||||
ffPrintSeparator(&instance);
|
||||
ffPrintOS(&instance);
|
||||
ffPrintOS(&instance, &instance.config.os);
|
||||
ffPrintHost(&instance);
|
||||
//ffPrintBios(&instance);
|
||||
//ffPrintBoard(&instance);
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFOSOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
|
||||
#if defined(__linux__) || defined(__FreeBSD__)
|
||||
FFstrbuf file;
|
||||
#endif
|
||||
} FFOSOptions;
|
||||
@@ -1,6 +1,8 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "common/option.h"
|
||||
#include "detection/os/os.h"
|
||||
#include "modules/os/os.h"
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
@@ -94,17 +96,17 @@ static void buildOutputNixOS(const FFinstance* instance, const FFOSResult* os, F
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintOS(FFinstance* instance)
|
||||
void ffPrintOS(FFinstance* instance, FFOSOptions* options)
|
||||
{
|
||||
const FFOSResult* os = ffDetectOS(instance);
|
||||
|
||||
if(os->name.length == 0 && os->prettyName.length == 0 && os->id.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_OS_MODULE_NAME, 0, &instance->config.os, "Could not detect OS");
|
||||
ffPrintError(instance, FF_OS_MODULE_NAME, 0, &options->moduleArgs, "Could not detect OS");
|
||||
return;
|
||||
}
|
||||
|
||||
if(instance->config.os.outputFormat.length == 0)
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
FFstrbuf result;
|
||||
ffStrbufInit(&result);
|
||||
@@ -114,13 +116,13 @@ void ffPrintOS(FFinstance* instance)
|
||||
else
|
||||
buildOutputDefault(instance, os, &result);
|
||||
|
||||
ffPrintLogoAndKey(instance, FF_OS_MODULE_NAME, 0, &instance->config.os.key);
|
||||
ffPrintLogoAndKey(instance, FF_OS_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
ffStrbufPutTo(&result, stdout);
|
||||
ffStrbufDestroy(&result);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_OS_MODULE_NAME, 0, &instance->config.os, FF_OS_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
ffPrintFormat(instance, FF_OS_MODULE_NAME, 0, &options->moduleArgs, FF_OS_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &instance->state.platform.systemName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &os->name},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &os->prettyName},
|
||||
@@ -136,3 +138,39 @@ void ffPrintOS(FFinstance* instance)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitOSOptions(FFOSOptions* options)
|
||||
{
|
||||
options->moduleName = FF_OS_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
|
||||
#if defined(__linux__) || defined(__FreeBSD__)
|
||||
ffStrbufInit(&options->file);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool ffParseOSCommandOptions(FFOSOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_OS_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
#if defined(__linux__) || defined(__FreeBSD__)
|
||||
if (strcasecmp(subKey, "file") == 0)
|
||||
{
|
||||
ffOptionParseString(key, value, &options->file);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyOSOptions(FFOSOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
#if defined(__linux__) || defined(__FreeBSD__)
|
||||
ffStrbufDestroy(&options->file);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
#include "modules/os/option.h"
|
||||
|
||||
void ffPrintOS(FFinstance* instance, FFOSOptions* options);
|
||||
void ffInitOSOptions(FFOSOptions* options);
|
||||
bool ffParseOSCommandOptions(FFOSOptions* options, const char* key, const char* value);
|
||||
void ffDestroyOSOptions(FFOSOptions* options);
|
||||
Reference in New Issue
Block a user