Memory / Swap: add JSON config support

This commit is contained in:
李通洲
2023-05-11 21:07:04 +08:00
parent 31241b64d8
commit f3190b8979
14 changed files with 175 additions and 30 deletions
+7
View File
@@ -88,6 +88,12 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
false;
}
case 'M': {
return
tryModule(instance, type, module, FF_MEMORY_MODULE_NAME, ffParseMemoryJsonObject) ||
false;
}
case 'O': {
return
tryModule(instance, type, module, FF_OS_MODULE_NAME, ffParseOSJsonObject) ||
@@ -98,6 +104,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
return
tryModule(instance, type, module, FF_SEPARATOR_MODULE_NAME, ffParseSeparatorJsonObject) ||
tryModule(instance, type, module, FF_SOUND_MODULE_NAME, ffParseSoundJsonObject) ||
tryModule(instance, type, module, FF_SWAP_MODULE_NAME, ffParseSwapJsonObject) ||
false;
}
@@ -3,18 +3,18 @@
#include "common/parsing.h"
#include "common/bar.h"
#include "detection/memory/memory.h"
#include "modules/memory/memory.h"
#define FF_MEMORY_MODULE_NAME "Memory"
#define FF_MEMORY_NUM_FORMAT_ARGS 3
void ffPrintMemory(FFinstance* instance)
void ffPrintMemory(FFinstance* instance, FFMemoryOptions* options)
{
FFMemoryResult storage;
const char* error = ffDetectMemory(&storage);
if(error)
{
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "%s", error);
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
return;
}
@@ -28,9 +28,9 @@ void ffPrintMemory(FFinstance* instance)
? 0
: (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0);
if(instance->config.memory.outputFormat.length == 0)
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory.key);
ffPrintLogoAndKey(instance, FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs.key);
if (storage.bytesTotal == 0)
puts("Disabled");
else
@@ -55,10 +55,52 @@ void ffPrintMemory(FFinstance* instance)
}
else
{
ffPrintFormat(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){
ffPrintFormat(instance, FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
{FF_FORMAT_ARG_TYPE_UINT8, &percentage},
});
}
}
void ffInitMemoryOptions(FFMemoryOptions* options)
{
options->moduleName = FF_MEMORY_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseMemoryCommandOptions(FFMemoryOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_MEMORY_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyMemoryOptions(FFMemoryOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
#ifdef FF_HAVE_JSONC
void ffParseMemoryJsonObject(FFinstance* instance, json_object* module)
{
FFMemoryOptions __attribute__((__cleanup__(ffDestroyMemoryOptions))) options;
ffInitMemoryOptions(&options);
if (module)
{
json_object_object_foreach(module, key, val)
{
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintMemory(instance, &options);
}
#endif
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "fastfetch.h"
#define FF_MEMORY_MODULE_NAME "Memory"
void ffPrintMemory(FFinstance* instance, FFMemoryOptions* options);
void ffInitMemoryOptions(FFMemoryOptions* options);
bool ffParseMemoryCommandOptions(FFMemoryOptions* options, const char* key, const char* value);
void ffDestroyMemoryOptions(FFMemoryOptions* options);
#ifdef FF_HAVE_JSONC
#include "common/jsonconfig.h"
void ffParseMemoryJsonObject(FFinstance* instance, json_object* module);
#endif
+11
View File
@@ -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 FFMemoryOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
} FFMemoryOptions;
+2
View File
@@ -25,8 +25,10 @@
#include "modules/kernel/kernel.h"
#include "modules/locale/locale.h"
#include "modules/localip/localip.h"
#include "modules/memory/memory.h"
#include "modules/separator/separator.h"
#include "modules/sound/sound.h"
#include "modules/swap/swap.h"
#include "modules/title/title.h"
#include "modules/jsonconfig/jsonconfig.h"
#include "modules/uptime/uptime.h"
+2
View File
@@ -22,9 +22,11 @@
#include "modules/kernel/option.h"
#include "modules/locale/option.h"
#include "modules/localip/option.h"
#include "modules/memory/option.h"
#include "modules/os/option.h"
#include "modules/separator/option.h"
#include "modules/sound/option.h"
#include "modules/swap/option.h"
#include "modules/title/option.h"
#include "modules/uptime/option.h"
#include "modules/wmtheme/option.h"
+11
View File
@@ -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 FFSwapOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
} FFSwapOptions;
+48 -6
View File
@@ -3,18 +3,18 @@
#include "common/parsing.h"
#include "common/bar.h"
#include "detection/swap/swap.h"
#include "modules/swap/swap.h"
#define FF_SWAP_MODULE_NAME "Swap"
#define FF_SWAP_NUM_FORMAT_ARGS 3
void ffPrintSwap(FFinstance* instance)
void ffPrintSwap(FFinstance* instance, FFSwapOptions* options)
{
FFSwapResult storage;
const char* error = ffDetectSwap(&storage);
if(error)
{
ffPrintError(instance, FF_SWAP_MODULE_NAME, 0, &instance->config.swap, "%s", error);
ffPrintError(instance, FF_SWAP_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
return;
}
@@ -28,9 +28,9 @@ void ffPrintSwap(FFinstance* instance)
? 0
: (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0);
if(instance->config.swap.outputFormat.length == 0)
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_SWAP_MODULE_NAME, 0, &instance->config.swap.key);
ffPrintLogoAndKey(instance, FF_SWAP_MODULE_NAME, 0, &options->moduleArgs.key);
if (storage.bytesTotal == 0)
puts("Disabled");
else
@@ -55,10 +55,52 @@ void ffPrintSwap(FFinstance* instance)
}
else
{
ffPrintFormat(instance, FF_SWAP_MODULE_NAME, 0, &instance->config.swap, FF_SWAP_NUM_FORMAT_ARGS, (FFformatarg[]){
ffPrintFormat(instance, FF_SWAP_MODULE_NAME, 0, &options->moduleArgs, FF_SWAP_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
{FF_FORMAT_ARG_TYPE_UINT8, &percentage},
});
}
}
void ffInitSwapOptions(FFSwapOptions* options)
{
options->moduleName = FF_SWAP_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseSwapCommandOptions(FFSwapOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_SWAP_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroySwapOptions(FFSwapOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
#ifdef FF_HAVE_JSONC
void ffParseSwapJsonObject(FFinstance* instance, json_object* module)
{
FFSwapOptions __attribute__((__cleanup__(ffDestroySwapOptions))) options;
ffInitSwapOptions(&options);
if (module)
{
json_object_object_foreach(module, key, val)
{
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
ffPrintError(instance, FF_SWAP_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintSwap(instance, &options);
}
#endif
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "fastfetch.h"
#define FF_SWAP_MODULE_NAME "Swap"
void ffPrintSwap(FFinstance* instance, FFSwapOptions* options);
void ffInitSwapOptions(FFSwapOptions* options);
bool ffParseSwapCommandOptions(FFSwapOptions* options, const char* key, const char* value);
void ffDestroySwapOptions(FFSwapOptions* options);
#ifdef FF_HAVE_JSONC
#include "common/jsonconfig.h"
void ffParseSwapJsonObject(FFinstance* instance, json_object* module);
#endif