mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Memory / Swap: add JSON config support
This commit is contained in:
+2
-2
@@ -286,7 +286,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/kernel/kernel.c
|
||||
src/modules/locale/locale.c
|
||||
src/modules/localip/localip.c
|
||||
src/modules/memory.c
|
||||
src/modules/memory/memory.c
|
||||
src/modules/opencl.c
|
||||
src/modules/opengl.c
|
||||
src/modules/os/os.c
|
||||
@@ -299,7 +299,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/separator/separator.c
|
||||
src/modules/shell.c
|
||||
src/modules/sound/sound.c
|
||||
src/modules/swap.c
|
||||
src/modules/swap/swap.c
|
||||
src/modules/media.c
|
||||
src/modules/terminal.c
|
||||
src/modules/terminalfont.c
|
||||
|
||||
+4
-4
@@ -84,8 +84,8 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffInitCPUOptions(&instance->config.cpu);
|
||||
ffInitCPUUsageOptions(&instance->config.cpuUsage);
|
||||
ffInitGPUOptions(&instance->config.gpu);
|
||||
initModuleArg(&instance->config.memory);
|
||||
initModuleArg(&instance->config.swap);
|
||||
ffInitMemoryOptions(&instance->config.memory);
|
||||
ffInitSwapOptions(&instance->config.swap);
|
||||
ffInitDiskOptions(&instance->config.disk);
|
||||
ffInitBatteryOptions(&instance->config.battery);
|
||||
initModuleArg(&instance->config.powerAdapter);
|
||||
@@ -306,8 +306,8 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffDestroyCPUOptions(&instance->config.cpu);
|
||||
ffDestroyCPUUsageOptions(&instance->config.cpuUsage);
|
||||
ffDestroyGPUOptions(&instance->config.gpu);
|
||||
destroyModuleArg(&instance->config.memory);
|
||||
destroyModuleArg(&instance->config.swap);
|
||||
ffDestroyMemoryOptions(&instance->config.memory);
|
||||
ffDestroySwapOptions(&instance->config.swap);
|
||||
ffDestroyDiskOptions(&instance->config.disk);
|
||||
ffDestroyBatteryOptions(&instance->config.battery);
|
||||
destroyModuleArg(&instance->config.powerAdapter);
|
||||
|
||||
+6
-6
@@ -1002,8 +1002,8 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(ffParseCPUCommandOptions(&instance->config.cpu, key, value)) {}
|
||||
else if(ffParseCPUUsageCommandOptions(&instance->config.cpuUsage, key, value)) {}
|
||||
else if(ffParseGPUCommandOptions(&instance->config.gpu, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "memory", &instance->config.memory)) {}
|
||||
else if(optionParseModuleArgs(key, value, "swap", &instance->config.swap)) {}
|
||||
else if(ffParseMemoryCommandOptions(&instance->config.memory, key, value)) {}
|
||||
else if(ffParseSwapCommandOptions(&instance->config.swap, key, value)) {}
|
||||
else if(ffParseDiskCommandOptions(&instance->config.disk, key, value)) {}
|
||||
else if(ffParseBatteryCommandOptions(&instance->config.battery, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "poweradapter", &instance->config.powerAdapter)) {}
|
||||
@@ -1224,10 +1224,10 @@ static void parseStructureCommand(FFinstance* instance, const char* line)
|
||||
ffPrintCustom(instance, &instance->config.custom);
|
||||
else if(strcasecmp(line, FF_GPU_MODULE_NAME) == 0)
|
||||
ffPrintGPU(instance, &instance->config.gpu);
|
||||
else if(strcasecmp(line, "memory") == 0)
|
||||
ffPrintMemory(instance);
|
||||
else if(strcasecmp(line, "swap") == 0)
|
||||
ffPrintSwap(instance);
|
||||
else if(strcasecmp(line, FF_MEMORY_MODULE_NAME) == 0)
|
||||
ffPrintMemory(instance, &instance->config.memory);
|
||||
else if(strcasecmp(line, FF_SWAP_MODULE_NAME) == 0)
|
||||
ffPrintSwap(instance, &instance->config.swap);
|
||||
else if(strcasecmp(line, FF_DISK_MODULE_NAME) == 0)
|
||||
ffPrintDisk(instance, &instance->config.disk);
|
||||
else if(strcasecmp(line, FF_BATTERY_MODULE_NAME) == 0)
|
||||
|
||||
+2
-4
@@ -85,8 +85,8 @@ typedef struct FFconfig
|
||||
FFCPUUsageOptions cpuUsage;
|
||||
FFCustomOptions custom;
|
||||
FFGPUOptions gpu;
|
||||
FFModuleArgs memory;
|
||||
FFModuleArgs swap;
|
||||
FFMemoryOptions memory;
|
||||
FFSwapOptions swap;
|
||||
FFDiskOptions disk;
|
||||
FFBatteryOptions battery;
|
||||
FFModuleArgs powerAdapter;
|
||||
@@ -210,8 +210,6 @@ void ffPrintIcons(FFinstance* instance);
|
||||
void ffPrintWallpaper(FFinstance* instance);
|
||||
void ffPrintTerminal(FFinstance* instance);
|
||||
void ffPrintTerminalFont(FFinstance* instance);
|
||||
void ffPrintMemory(FFinstance* instance);
|
||||
void ffPrintSwap(FFinstance* instance);
|
||||
void ffPrintPowerAdapter(FFinstance* instance);
|
||||
void ffPrintPlayer(FFinstance* instance);
|
||||
void ffPrintMedia(FFinstance* instance);
|
||||
|
||||
+2
-2
@@ -45,8 +45,8 @@ int main(int argc, char** argv)
|
||||
ffPrintTerminalFont(&instance);
|
||||
ffPrintCPU(&instance, &instance.config.cpu);
|
||||
ffPrintGPU(&instance, &instance.config.gpu);
|
||||
ffPrintMemory(&instance);
|
||||
//ffPrintSwap(&instance);
|
||||
ffPrintMemory(&instance, &instance.config.memory);
|
||||
ffPrintSwap(&instance, &instance.config.swap);
|
||||
ffPrintDisk(&instance, &instance.config.disk);
|
||||
ffPrintBattery(&instance, &instance.config.battery);
|
||||
ffPrintPowerAdapter(&instance);
|
||||
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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;
|
||||
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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;
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in New Issue
Block a user