Modules: refactor module option handling for static allocation

Moves module option structs out of global config and removes dynamic allocation, using static buffers and size assertions for safer, simpler management.

Refactors module initialization and destruction to work with isolated option structs, eliminating the need for the previous modules container and related code.

Unifies module option header usage and updates detection logic to leverage statically-sized option buffers, improving encapsulation and maintainability.

Simplifies module function signatures and reduces coupling between modules and config.

Enhances future extensibility and reliability by enforcing max size constraints and removing unnecessary indirections.
This commit is contained in:
李通洲
2025-08-04 14:25:14 +08:00
parent 559082806b
commit 75d2c668d0
289 changed files with 1449 additions and 1672 deletions
+14 -15
View File
@@ -120,23 +120,8 @@ void ffGenerateCommandJsonResult(FF_MAYBE_UNUSED FFCommandOptions* options, yyjs
yyjson_mut_obj_add_strbuf(doc, module, "result", &result);
}
FFModuleBaseInfo ffCommandModuleInfo = {
.name = FF_COMMAND_MODULE_NAME,
.description = "Run custom shell scripts",
.initOptions = (void*) ffInitCommandOptions,
.destroyOptions = (void*) ffDestroyCommandOptions,
.parseJsonObject = (void*) ffParseCommandJsonObject,
.printModule = (void*) ffPrintCommand,
.generateJsonResult = (void*) ffGenerateCommandJsonResult,
.generateJsonConfig = (void*) ffGenerateCommandJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Command result", "result"},
}))
};
void ffInitCommandOptions(FFCommandOptions* options)
{
options->moduleInfo = ffCommandModuleInfo;
ffOptionInitModuleArg(&options->moduleArgs, "");
ffStrbufInitStatic(&options->shell,
@@ -163,3 +148,17 @@ void ffDestroyCommandOptions(FFCommandOptions* options)
ffStrbufDestroy(&options->param);
ffStrbufDestroy(&options->text);
}
FFModuleBaseInfo ffCommandModuleInfo = {
.name = FF_COMMAND_MODULE_NAME,
.description = "Run custom shell scripts",
.initOptions = (void*) ffInitCommandOptions,
.destroyOptions = (void*) ffDestroyCommandOptions,
.parseJsonObject = (void*) ffParseCommandJsonObject,
.printModule = (void*) ffPrintCommand,
.generateJsonResult = (void*) ffGenerateCommandJsonResult,
.generateJsonConfig = (void*) ffGenerateCommandJsonConfig,
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
{"Command result", "result"},
}))
};
+1 -1
View File
@@ -1,6 +1,6 @@
#pragma once
#include "fastfetch.h"
#include "option.h"
#define FF_COMMAND_MODULE_NAME "Command"
+2 -3
View File
@@ -1,15 +1,14 @@
#pragma once
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
#include "common/option.h"
typedef struct FFCommandOptions
{
FFModuleBaseInfo moduleInfo;
FFModuleArgs moduleArgs;
FFstrbuf shell;
FFstrbuf param;
FFstrbuf text;
} FFCommandOptions;
static_assert(sizeof(FFCommandOptions) <= FF_OPTION_MAX_SIZE, "FFCommandOptions size exceeds maximum allowed size");