mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-12 09:58:03 +02:00
Separator: init support of JSON config
This commit is contained in:
+1
-1
@@ -295,7 +295,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/processes.c
|
||||
src/modules/publicip.c
|
||||
src/modules/display.c
|
||||
src/modules/separator.c
|
||||
src/modules/separator/separator.c
|
||||
src/modules/shell.c
|
||||
src/modules/sound.c
|
||||
src/modules/swap.c
|
||||
|
||||
+11
-3
@@ -8,6 +8,16 @@
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
static inline bool parseModuleJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module)
|
||||
{
|
||||
return
|
||||
ffParseBatteryJsonObject(instance, type, data, module) ||
|
||||
ffParseCommandJsonObject(instance, type, data, module) ||
|
||||
ffParseOSJsonObject(instance, type, data, module) ||
|
||||
ffParseSeparatorJsonObject(instance, type, data, module) ||
|
||||
false;
|
||||
}
|
||||
|
||||
static inline void wrapJsoncFree(JSONCData* data)
|
||||
{
|
||||
assert(data);
|
||||
@@ -37,9 +47,7 @@ static const char* parseModules(FFinstance* instance, JSONCData* data, json_obje
|
||||
else
|
||||
return "modules must be an array of strings or objects";
|
||||
|
||||
if(!ffParseBatteryJsonObject(instance, type, data, module))
|
||||
if(!ffParseCommandJsonObject(instance, type, data, module))
|
||||
if(!ffParseOSJsonObject(instance, type, data, module))
|
||||
if(!parseModuleJsonObject(instance, type, data, module))
|
||||
return "Unknown module type";
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -118,6 +118,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
initModuleArg(&instance->config.users);
|
||||
initModuleArg(&instance->config.bluetooth);
|
||||
initModuleArg(&instance->config.sound);
|
||||
ffInitSeparatorOptions(&instance->config.separator);
|
||||
initModuleArg(&instance->config.gamepad);
|
||||
|
||||
ffStrbufInitA(&instance->config.libPCI, 0);
|
||||
@@ -169,8 +170,6 @@ static void defaultConfig(FFinstance* instance)
|
||||
|
||||
instance->config.soundType = FF_SOUND_TYPE_MAIN;
|
||||
|
||||
ffStrbufInitA(&instance->config.separatorString, 0);
|
||||
|
||||
instance->config.localIpShowType = FF_LOCALIP_TYPE_IPV4_BIT;
|
||||
ffStrbufInit(&instance->config.localIpNamePrefix);
|
||||
|
||||
@@ -364,6 +363,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
destroyModuleArg(&instance->config.openCL);
|
||||
destroyModuleArg(&instance->config.users);
|
||||
destroyModuleArg(&instance->config.bluetooth);
|
||||
ffDestroySeparatorOptions(&instance->config.separator);
|
||||
destroyModuleArg(&instance->config.sound);
|
||||
destroyModuleArg(&instance->config.gamepad);
|
||||
|
||||
@@ -394,7 +394,6 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffStrbufDestroy(&instance->config.libnm);
|
||||
|
||||
ffStrbufDestroy(&instance->config.diskFolders);
|
||||
ffStrbufDestroy(&instance->config.separatorString);
|
||||
ffStrbufDestroy(&instance->config.localIpNamePrefix);
|
||||
ffStrbufDestroy(&instance->config.publicIpUrl);
|
||||
ffStrbufDestroy(&instance->config.weatherOutputFormat);
|
||||
|
||||
+2
-3
@@ -1179,6 +1179,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(optionParseModuleArgs(key, value, "opencl", &instance->config.openCL)) {}
|
||||
else if(optionParseModuleArgs(key, value, "users", &instance->config.users)) {}
|
||||
else if(optionParseModuleArgs(key, value, "bluetooth", &instance->config.bluetooth)) {}
|
||||
else if(ffParseSeparatorCommandOptions(&instance->config.separator, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "sound", &instance->config.sound)) {}
|
||||
else if(optionParseModuleArgs(key, value, "gamepad", &instance->config.gamepad)) {}
|
||||
|
||||
@@ -1298,8 +1299,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
NULL
|
||||
);
|
||||
}
|
||||
else if(strcasecmp(key, "--separator-string") == 0)
|
||||
optionParseString(key, value, &instance->config.separatorString);
|
||||
else if(strcasecmp(key, "--localip-show-ipv4") == 0)
|
||||
optionParseBoolean(value) ? (instance->config.localIpShowType |= FF_LOCALIP_TYPE_IPV4_BIT) : (instance->config.localIpShowType &= ~FF_LOCALIP_TYPE_IPV4_BIT);
|
||||
else if(strcasecmp(key, "--localip-show-ipv6") == 0)
|
||||
@@ -1398,7 +1397,7 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
||||
else if(strcasecmp(line, "title") == 0)
|
||||
ffPrintTitle(instance);
|
||||
else if(strcasecmp(line, "separator") == 0)
|
||||
ffPrintSeparator(instance);
|
||||
ffPrintSeparator(instance, &instance->config.separator);
|
||||
else if(strcasecmp(line, "os") == 0)
|
||||
ffPrintOS(instance, &instance->config.os);
|
||||
else if(strcasecmp(line, "host") == 0)
|
||||
|
||||
+1
-3
@@ -179,6 +179,7 @@ typedef struct FFconfig
|
||||
FFModuleArgs openCL;
|
||||
FFModuleArgs users;
|
||||
FFModuleArgs bluetooth;
|
||||
FFSeparatorOptions separator;
|
||||
FFModuleArgs sound;
|
||||
FFModuleArgs gamepad;
|
||||
|
||||
@@ -229,8 +230,6 @@ typedef struct FFconfig
|
||||
|
||||
bool bluetoothShowDisconnected;
|
||||
|
||||
FFstrbuf separatorString;
|
||||
|
||||
FFstrbuf localIpNamePrefix;
|
||||
FFLocalIpType localIpShowType;
|
||||
|
||||
@@ -302,7 +301,6 @@ void ffPrepareWeather(FFinstance* instance);
|
||||
void ffPrintCustom(FFinstance* instance, const char* key, const char* value);
|
||||
void ffPrintBreak(FFinstance* instance);
|
||||
void ffPrintTitle(FFinstance* instance);
|
||||
void ffPrintSeparator(FFinstance* instance);
|
||||
void ffPrintHost(FFinstance* instance);
|
||||
void ffPrintBios(FFinstance* instance);
|
||||
void ffPrintBoard(FFinstance* instance);
|
||||
|
||||
+1
-1
@@ -21,7 +21,7 @@ int main(int argc, char** argv)
|
||||
|
||||
//Printing
|
||||
ffPrintTitle(&instance);
|
||||
ffPrintSeparator(&instance);
|
||||
ffPrintSeparator(&instance, &instance.config.separator);
|
||||
ffPrintOS(&instance, &instance.config.os);
|
||||
ffPrintHost(&instance);
|
||||
//ffPrintBios(&instance);
|
||||
|
||||
@@ -5,3 +5,4 @@
|
||||
#include "modules/os/os.h"
|
||||
#include "modules/battery/battery.h"
|
||||
#include "modules/command/command.h"
|
||||
#include "modules/separator/separator.h"
|
||||
|
||||
@@ -5,3 +5,4 @@
|
||||
#include "modules/battery/option.h"
|
||||
#include "modules/os/option.h"
|
||||
#include "modules/command/option.h"
|
||||
#include "modules/separator/option.h"
|
||||
|
||||
@@ -1,29 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
|
||||
void ffPrintSeparator(FFinstance* instance)
|
||||
{
|
||||
uint32_t titleLength = instance->state.platform.userName.length + 1 + (instance->config.titleFQDN ?
|
||||
instance->state.platform.domainName.length :
|
||||
instance->state.platform.hostName.length
|
||||
);
|
||||
|
||||
ffLogoPrintLine(instance);
|
||||
|
||||
if(instance->config.separatorString.length == 0)
|
||||
{
|
||||
for(uint32_t i = 0; i < titleLength; i++)
|
||||
putchar('-');
|
||||
}
|
||||
else
|
||||
{
|
||||
//Write the whole separator as often as it fits fully into titleLength
|
||||
for(uint32_t i = 0; i < titleLength / instance->config.separatorString.length; i++)
|
||||
ffStrbufWriteTo(&instance->config.separatorString, stdout);
|
||||
|
||||
//Write as much of the separator as needed to fill titleLength
|
||||
for(uint32_t i = 0; i < titleLength % instance->config.separatorString.length; i++)
|
||||
putchar(instance->config.separatorString.chars[i]);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFSeparatorOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
|
||||
FFstrbuf string;
|
||||
} FFSeparatorOptions;
|
||||
@@ -0,0 +1,90 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "modules/separator/separator.h"
|
||||
|
||||
#define FF_SEPARATOR_MODULE_NAME "Separator"
|
||||
|
||||
void ffPrintSeparator(FFinstance* instance, FFSeparatorOptions* options)
|
||||
{
|
||||
uint32_t titleLength = instance->state.platform.userName.length + 1 + (instance->config.titleFQDN ?
|
||||
instance->state.platform.domainName.length :
|
||||
instance->state.platform.hostName.length
|
||||
);
|
||||
|
||||
ffLogoPrintLine(instance);
|
||||
|
||||
if(options->string.length == 0)
|
||||
{
|
||||
ffPrintCharTimes('-', titleLength);
|
||||
}
|
||||
else
|
||||
{
|
||||
//Write the whole separator as often as it fits fully into titleLength
|
||||
for(uint32_t i = 0; i < titleLength / options->string.length; i++)
|
||||
ffStrbufWriteTo(&options->string, stdout);
|
||||
|
||||
//Write as much of the separator as needed to fill titleLength
|
||||
for(uint32_t i = 0; i < titleLength % options->string.length; i++)
|
||||
putchar(options->string.chars[i]);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void ffInitSeparatorOptions(FFSeparatorOptions* options)
|
||||
{
|
||||
options->moduleName = FF_SEPARATOR_MODULE_NAME;
|
||||
ffStrbufInit(&options->string);
|
||||
}
|
||||
|
||||
bool ffParseSeparatorCommandOptions(FFSeparatorOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_SEPARATOR_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
|
||||
if (strcasecmp(subKey, "string") == 0)
|
||||
{
|
||||
ffOptionParseString(key, value, &options->string);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroySeparatorOptions(FFSeparatorOptions* options)
|
||||
{
|
||||
ffStrbufDestroy(&options->string);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
bool ffParseSeparatorJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module)
|
||||
{
|
||||
if (strcasecmp(type, FF_SEPARATOR_MODULE_NAME) != 0)
|
||||
return false;
|
||||
|
||||
FFSeparatorOptions __attribute__((__cleanup__(ffDestroySeparatorOptions))) options;
|
||||
ffInitSeparatorOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
struct lh_entry* entry;
|
||||
lh_foreach(data->ffjson_object_get_object(module), entry)
|
||||
{
|
||||
const char* key = (const char *)lh_entry_k(entry);
|
||||
if (strcasecmp(key, "type") == 0)
|
||||
continue;
|
||||
json_object* val = (struct json_object *)lh_entry_v(entry);
|
||||
|
||||
if (strcasecmp(key, "string") == 0)
|
||||
{
|
||||
ffStrbufSetS(&options.string, data->ffjson_object_get_string(val));
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPrintErrorString(instance, FF_SEPARATOR_MODULE_NAME, 0, NULL, NULL, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintSeparator(instance, &options);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
#include "modules/separator/option.h"
|
||||
|
||||
void ffPrintSeparator(FFinstance* instance, FFSeparatorOptions* options);
|
||||
void ffInitSeparatorOptions(FFSeparatorOptions* options);
|
||||
bool ffParseSeparatorCommandOptions(FFSeparatorOptions* options, const char* key, const char* value);
|
||||
void ffDestroySeparatorOptions(FFSeparatorOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/config.h"
|
||||
bool ffParseSeparatorJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module);
|
||||
#endif
|
||||
Reference in New Issue
Block a user