mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:52:08 +02:00
Custom: support JSON module
Note: old command options `--set` and `--set-keyless` were removed. To reuse custom module, JSON config must be used.
This commit is contained in:
@@ -65,6 +65,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffInitBrightnessOptions(&instance->config.brightness);
|
||||
initModuleArg(&instance->config.chassis);
|
||||
ffInitCommandOptions(&instance->config.command);
|
||||
ffInitCustomOptions(&instance->config.custom);
|
||||
ffInitKernelOptions(&instance->config.kernel);
|
||||
initModuleArg(&instance->config.uptime);
|
||||
initModuleArg(&instance->config.processes);
|
||||
@@ -300,6 +301,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffDestroyBrightnessOptions(&instance->config.brightness);
|
||||
destroyModuleArg(&instance->config.chassis);
|
||||
ffDestroyCommandOptions(&instance->config.command);
|
||||
ffDestroyCustomOptions(&instance->config.custom);
|
||||
ffDestroyKernelOptions(&instance->config.kernel);
|
||||
destroyModuleArg(&instance->config.uptime);
|
||||
destroyModuleArg(&instance->config.processes);
|
||||
|
||||
+7
-45
@@ -3,7 +3,6 @@
|
||||
#include "common/parsing.h"
|
||||
#include "common/io/io.h"
|
||||
#include "common/time.h"
|
||||
#include "util/FFvaluestore.h"
|
||||
#include "util/stringUtils.h"
|
||||
#include "logo/logo.h"
|
||||
|
||||
@@ -30,7 +29,6 @@ typedef struct CustomValue
|
||||
// Things only needed by fastfetch
|
||||
typedef struct FFdata
|
||||
{
|
||||
FFvaluestore customValues;
|
||||
FFstrbuf structure;
|
||||
bool loadUserConfig;
|
||||
} FFdata;
|
||||
@@ -718,32 +716,6 @@ static uint32_t optionParseUInt32(const char* key, const char* value)
|
||||
return num;
|
||||
}
|
||||
|
||||
static void optionParseCustomValue(FFdata* data, const char* key, const char* value, bool printKey)
|
||||
{
|
||||
if(value == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error: usage: %s <key=value>\n", key);
|
||||
exit(411);
|
||||
}
|
||||
|
||||
char* separator = strchr(value, '=');
|
||||
|
||||
if(separator == NULL)
|
||||
{
|
||||
fprintf(stderr, "Error: usage: %s <key=value>, '=' missing\n", key);
|
||||
exit(412);
|
||||
}
|
||||
|
||||
*separator = '\0';
|
||||
|
||||
bool created;
|
||||
CustomValue* customValue = ffValuestoreSet(&data->customValues, value, &created);
|
||||
if(created)
|
||||
ffStrbufInit(&customValue->value);
|
||||
ffStrbufSetS(&customValue->value, separator + 1);
|
||||
customValue->printKey = printKey;
|
||||
}
|
||||
|
||||
static void optionParseEnum(const char* argumentKey, const char* requestedKey, void* result, ...)
|
||||
{
|
||||
if(requestedKey == NULL)
|
||||
@@ -990,10 +962,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
optionParseColor(key, value, &instance->config.colorKeys);
|
||||
ffStrbufSet(&instance->config.colorTitle, &instance->config.colorKeys);
|
||||
}
|
||||
else if(strcasecmp(key, "--set") == 0)
|
||||
optionParseCustomValue(data, key, value, true);
|
||||
else if(strcasecmp(key, "--set-keyless") == 0)
|
||||
optionParseCustomValue(data, key, value, false);
|
||||
else if(strcasecmp(key, "--binary-prefix") == 0)
|
||||
{
|
||||
optionParseEnum(key, value, &instance->config.binaryPrefixType,
|
||||
@@ -1015,6 +983,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(ffParseBoardCommandOptions(&instance->config.board, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "chassis", &instance->config.chassis)) {}
|
||||
else if(ffParseCommandCommandOptions(&instance->config.command, key, value)) {}
|
||||
else if(ffParseCustomCommandOptions(&instance->config.custom, key, value)) {}
|
||||
else if(ffParseKernelCommandOptions(&instance->config.kernel, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "uptime", &instance->config.uptime)) {}
|
||||
else if(optionParseModuleArgs(key, value, "processes", &instance->config.processes)) {}
|
||||
@@ -1239,16 +1208,9 @@ static void parseArguments(FFinstance* instance, FFdata* data, int argc, const c
|
||||
}
|
||||
}
|
||||
|
||||
static void parseStructureCommand(FFinstance* instance, FFdata* data, const char* line)
|
||||
static void parseStructureCommand(FFinstance* instance, const char* line)
|
||||
{
|
||||
CustomValue* customValue = ffValuestoreGet(&data->customValues, line);
|
||||
if(customValue != NULL)
|
||||
{
|
||||
ffPrintCustom(instance, customValue->printKey ? line : NULL, customValue->value.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
if(strcasecmp(line, "break") == 0)
|
||||
if(strcasecmp(line, FF_BREAK_MODULE_NAME) == 0)
|
||||
ffPrintBreak(instance);
|
||||
else if(strcasecmp(line, FF_TITLE_MODULE_NAME) == 0)
|
||||
ffPrintTitle(instance, &instance->config.title);
|
||||
@@ -1302,6 +1264,8 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
||||
ffPrintCPU(instance, &instance->config.cpu);
|
||||
else if(strcasecmp(line, FF_CPUUSAGE_MODULE_NAME) == 0)
|
||||
ffPrintCPUUsage(instance, &instance->config.cpuUsage);
|
||||
else if(strcasecmp(line, FF_CUSTOM_MODULE_NAME) == 0)
|
||||
ffPrintCustom(instance, &instance->config.custom);
|
||||
else if(strcasecmp(line, "gpu") == 0)
|
||||
ffPrintGPU(instance);
|
||||
else if(strcasecmp(line, "memory") == 0)
|
||||
@@ -1310,7 +1274,7 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
||||
ffPrintSwap(instance);
|
||||
else if(strcasecmp(line, "disk") == 0)
|
||||
ffPrintDisk(instance);
|
||||
else if(strcasecmp(line, "battery") == 0)
|
||||
else if(strcasecmp(line, FF_BATTERY_MODULE_NAME) == 0)
|
||||
ffPrintBattery(instance, &instance->config.battery);
|
||||
else if(strcasecmp(line, "poweradapter") == 0)
|
||||
ffPrintPowerAdapter(instance);
|
||||
@@ -1361,7 +1325,6 @@ int main(int argc, const char** argv)
|
||||
|
||||
//Data stores things only needed for the configuration of fastfetch
|
||||
FFdata data;
|
||||
ffValuestoreInit(&data.customValues, sizeof(CustomValue));
|
||||
ffStrbufInitA(&data.structure, 256);
|
||||
data.loadUserConfig = true;
|
||||
|
||||
@@ -1402,7 +1365,7 @@ int main(int argc, const char** argv)
|
||||
if(__builtin_expect(instance.config.stat, false))
|
||||
ms = ffTimeGetTick();
|
||||
|
||||
parseStructureCommand(&instance, &data, data.structure.chars + startIndex);
|
||||
parseStructureCommand(&instance, data.structure.chars + startIndex);
|
||||
|
||||
if(__builtin_expect(instance.config.stat, false))
|
||||
{
|
||||
@@ -1424,7 +1387,6 @@ int main(int argc, const char** argv)
|
||||
ffFinish(&instance);
|
||||
|
||||
ffStrbufDestroy(&data.structure);
|
||||
ffValuestoreDestroy(&data.customValues);
|
||||
|
||||
ffDestroyInstance(&instance);
|
||||
}
|
||||
|
||||
+1
-1
@@ -118,6 +118,7 @@ typedef struct FFconfig
|
||||
FFModuleArgs terminalFont;
|
||||
FFCPUOptions cpu;
|
||||
FFCPUUsageOptions cpuUsage;
|
||||
FFCustomOptions custom;
|
||||
FFModuleArgs gpu;
|
||||
FFModuleArgs memory;
|
||||
FFModuleArgs swap;
|
||||
@@ -247,7 +248,6 @@ void ffPrepareWeather(FFinstance* instance);
|
||||
|
||||
//Printing
|
||||
|
||||
void ffPrintCustom(FFinstance* instance, const char* key, const char* value);
|
||||
void ffPrintChassis(FFinstance* instance);
|
||||
void ffPrintUptime(FFinstance* instance);
|
||||
void ffPrintProcesses(FFinstance* instance);
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "util/textModifier.h"
|
||||
|
||||
void ffPrintCustom(FFinstance* instance, const char* key, const char* value)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key, 0, NULL);
|
||||
ffPrintUserString(value);
|
||||
puts(FASTFETCH_TEXT_MODIFIER_RESET);
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "modules/custom/custom.h"
|
||||
#include "util/textModifier.h"
|
||||
|
||||
void ffPrintCustom(FFinstance* instance, FFCustomOptions* options)
|
||||
{
|
||||
if (options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_CUSTOM_MODULE_NAME, 0, &options->moduleArgs, "output format must be set for custom module");
|
||||
return;
|
||||
}
|
||||
|
||||
ffPrintLogoAndKey(instance, options->moduleArgs.key.length == 0 ? NULL : FF_CUSTOM_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
ffPrintUserString(options->moduleArgs.outputFormat.chars);
|
||||
puts(FASTFETCH_TEXT_MODIFIER_RESET);
|
||||
}
|
||||
|
||||
void ffInitCustomOptions(FFCustomOptions* options)
|
||||
{
|
||||
options->moduleName = FF_CUSTOM_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParseCustomCommandOptions(FFCustomOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_CUSTOM_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyCustomOptions(FFCustomOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseCustomJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFCustomOptions __attribute__((__cleanup__(ffDestroyCustomOptions))) options;
|
||||
ffInitCustomOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(instance, FF_CUSTOM_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintCustom(instance, &options);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_CUSTOM_MODULE_NAME "Custom"
|
||||
|
||||
void ffPrintCustom(FFinstance* instance, FFCustomOptions* options);
|
||||
void ffInitCustomOptions(FFCustomOptions* options);
|
||||
bool ffParseCustomCommandOptions(FFCustomOptions* options, const char* key, const char* value);
|
||||
void ffDestroyCustomOptions(FFCustomOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseCustomJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFCustomOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFCustomOptions;
|
||||
@@ -44,6 +44,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
tryModule(instance, type, module, FF_COMMAND_MODULE_NAME, ffParseCommandJsonObject) ||
|
||||
tryModule(instance, type, module, FF_COLORS_MODULE_NAME, ffParseColorsJsonObject) ||
|
||||
tryModule(instance, type, module, FF_CURSOR_MODULE_NAME, ffParseCursorJsonObject) ||
|
||||
tryModule(instance, type, module, FF_CUSTOM_MODULE_NAME, ffParseCustomJsonObject) ||
|
||||
false;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include "modules/command/command.h"
|
||||
#include "modules/colors/colors.h"
|
||||
#include "modules/cursor/cursor.h"
|
||||
#include "modules/custom/custom.h"
|
||||
#include "modules/datetime/datetime.h"
|
||||
#include "modules/display/display.h"
|
||||
#include "modules/host/host.h"
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "modules/cpu/option.h"
|
||||
#include "modules/cpuusage/option.h"
|
||||
#include "modules/cursor/option.h"
|
||||
#include "modules/custom/option.h"
|
||||
#include "modules/command/option.h"
|
||||
#include "modules/datetime/option.h"
|
||||
#include "modules/display/option.h"
|
||||
|
||||
@@ -1,46 +0,0 @@
|
||||
#include "FFvaluestore.h"
|
||||
|
||||
#include <string.h>
|
||||
|
||||
typedef char KeyType[32]; //31 byte key + \0
|
||||
|
||||
void ffValuestoreInit(FFvaluestore* vs, uint32_t valueSize)
|
||||
{
|
||||
ffListInit(vs, (uint32_t) (sizeof(KeyType) + valueSize));
|
||||
}
|
||||
|
||||
void* ffValuestoreGet(FFvaluestore* vs, const char* key)
|
||||
{
|
||||
for(uint32_t i = 0; i < vs->length; i++)
|
||||
{
|
||||
char* element = ffListGet(vs, i);
|
||||
if(strcmp(element, key) == 0)
|
||||
return element + sizeof(KeyType);
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void* ffValuestoreSet(FFvaluestore* vs, const char* key, bool* created)
|
||||
{
|
||||
char* element = ffValuestoreGet(vs, key);
|
||||
if(element != NULL)
|
||||
{
|
||||
if(created != NULL)
|
||||
*created = false;
|
||||
return element;
|
||||
}
|
||||
|
||||
element = ffListAdd(vs);
|
||||
strncpy(element, key, sizeof(KeyType) - 1);
|
||||
element[sizeof(KeyType) - 1] = '\0';
|
||||
|
||||
if(created != NULL)
|
||||
*created = true;
|
||||
return element + sizeof(KeyType);
|
||||
}
|
||||
|
||||
void ffValuestoreDestroy(FFvaluestore* vs)
|
||||
{
|
||||
ffListDestroy(vs);
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef FASTFETCH_INCLUDED_FFVALUESTORE
|
||||
#define FASTFETCH_INCLUDED_FFVALUESTORE
|
||||
|
||||
#include "FFlist.h"
|
||||
#include "FFcheckmacros.h"
|
||||
|
||||
typedef FFlist FFvaluestore;
|
||||
|
||||
void ffValuestoreInit(FFvaluestore* vs, uint32_t valueSize);
|
||||
FF_C_NODISCARD void* ffValuestoreGet(FFvaluestore* vs, const char* key);
|
||||
FF_C_NODISCARD void* ffValuestoreSet(FFvaluestore* vs, const char* key, bool* created); //created may be NULL
|
||||
void ffValuestoreDestroy(FFvaluestore* vs);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user