From 79a11c4728e1531273140addd54dfe65944de643 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 15 Jan 2026 10:35:01 +0800 Subject: [PATCH] CommandOption: adds a new option `--structure-disabled` (WIP) To disable modules temporarily --- src/common/commandoption.h | 1 + src/common/impl/commandoption.c | 102 +++++++++++++++++++------------- src/data/help.json | 8 +++ src/fastfetch.c | 3 + 4 files changed, 74 insertions(+), 40 deletions(-) diff --git a/src/common/commandoption.h b/src/common/commandoption.h index 696bdb861..c8f7aae3b 100644 --- a/src/common/commandoption.h +++ b/src/common/commandoption.h @@ -6,6 +6,7 @@ typedef struct FFdata { FFstrbuf structure; + FFstrbuf structureDisabled; bool configLoaded; } FFdata; diff --git a/src/common/impl/commandoption.c b/src/common/impl/commandoption.c index e19be4edc..56493fa02 100644 --- a/src/common/impl/commandoption.c +++ b/src/common/impl/commandoption.c @@ -71,35 +71,59 @@ bool ffParseModuleOptions(const char* key, const char* value) void ffPrepareCommandOption(FFdata* data) { - if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_CPUUSAGE_MODULE_NAME, ':')) - ffPrepareCPUUsage(); - - if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_DISKIO_MODULE_NAME, ':')) + char* moduleType = NULL; + size_t moduleLen = 0; + while (ffStrbufGetdelim(&moduleType, &moduleLen, ':', &data->structure)) { - __attribute__((__cleanup__(ffDestroyDiskIOOptions))) FFDiskIOOptions options; - ffInitDiskIOOptions(&options); - ffPrepareDiskIO(&options); - } + #define FF_IF_MODULE_MATCH(moduleNameConstant) if (moduleLen == strlen(moduleNameConstant) \ + && ffStrEqualsIgnCase(moduleType, moduleNameConstant) \ + && !ffStrbufSeparatedContainIgnCaseS(&data->structureDisabled, moduleNameConstant, ':')) - if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_NETIO_MODULE_NAME, ':')) - { - __attribute__((__cleanup__(ffDestroyNetIOOptions))) FFNetIOOptions options; - ffInitNetIOOptions(&options); - ffPrepareNetIO(&options); - } + switch (moduleType[0]) + { + case 'C': case 'c': + FF_IF_MODULE_MATCH(FF_CPUUSAGE_MODULE_NAME) + ffPrepareCPUUsage(); + break; - if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_PUBLICIP_MODULE_NAME, ':')) - { - __attribute__((__cleanup__(ffDestroyPublicIpOptions))) FFPublicIPOptions options; - ffInitPublicIpOptions(&options); - ffPreparePublicIp(&options); - } + case 'D': case 'd': + FF_IF_MODULE_MATCH(FF_DISKIO_MODULE_NAME) + { + __attribute__((__cleanup__(ffDestroyDiskIOOptions))) FFDiskIOOptions options; + ffInitDiskIOOptions(&options); + ffPrepareDiskIO(&options); + } + break; - if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_WEATHER_MODULE_NAME, ':')) - { - __attribute__((__cleanup__(ffDestroyWeatherOptions))) FFWeatherOptions options; - ffInitWeatherOptions(&options); - ffPrepareWeather(&options); + case 'N': case 'n': + FF_IF_MODULE_MATCH(FF_NETIO_MODULE_NAME) + { + __attribute__((__cleanup__(ffDestroyNetIOOptions))) FFNetIOOptions options; + ffInitNetIOOptions(&options); + ffPrepareNetIO(&options); + } + break; + + case 'P': case 'p': + FF_IF_MODULE_MATCH(FF_PUBLICIP_MODULE_NAME) + { + __attribute__((__cleanup__(ffDestroyPublicIpOptions))) FFPublicIPOptions options; + ffInitPublicIpOptions(&options); + ffPreparePublicIp(&options); + } + break; + + case 'W': case 'w': + FF_IF_MODULE_MATCH(FF_WEATHER_MODULE_NAME) + { + __attribute__((__cleanup__(ffDestroyWeatherOptions))) FFWeatherOptions options; + ffInitWeatherOptions(&options); + ffPrepareWeather(&options); + } + break; + } + + #undef FF_IF_MODULE_MATCH } } @@ -173,17 +197,19 @@ void ffPrintCommandOption(FFdata* data, yyjson_mut_doc* jsonDoc) { //Parse the structure and call the modules int32_t thres = instance.config.display.stat; - uint32_t startIndex = 0; - while (startIndex < data->structure.length) + + char* moduleType = NULL; + size_t moduleLen = 0; + while (ffStrbufGetdelim(&moduleType, &moduleLen, ':', &data->structure)) { - uint32_t colonIndex = ffStrbufNextIndexC(&data->structure, startIndex, ':'); - data->structure.chars[colonIndex] = '\0'; + if (ffStrbufSeparatedContainIgnCaseS(&data->structureDisabled, moduleType, ':')) + continue; double ms = 0; if(thres >= 0) ms = ffTimeGetTick(); - parseStructureCommand(data->structure.chars + startIndex, genJsonResult, jsonDoc); + parseStructureCommand(moduleType, genJsonResult, jsonDoc); if(thres >= 0) { @@ -207,8 +233,6 @@ void ffPrintCommandOption(FFdata* data, yyjson_mut_doc* jsonDoc) #if defined(_WIN32) if (!jsonDoc && !instance.config.display.noBuffer) fflush(stdout); #endif - - startIndex = colonIndex + 1; } } @@ -218,15 +242,13 @@ void ffMigrateCommandOptionToJsonc(FFdata* data, yyjson_mut_doc* jsonDoc) if(data->structure.length == 0) ffStrbufAppendS(&data->structure, FASTFETCH_DATATEXT_STRUCTURE); // Cannot use `ffStrbufSetStatic` here because we will modify the string - //Parse the structure and call the modules - uint32_t startIndex = 0; - while (startIndex < data->structure.length) + char* moduleType = NULL; + size_t moduleLen = 0; + while (ffStrbufGetdelim(&moduleType, &moduleLen, ':', &data->structure)) { - uint32_t colonIndex = ffStrbufNextIndexC(&data->structure, startIndex, ':'); - data->structure.chars[colonIndex] = '\0'; + if (ffStrbufSeparatedContainIgnCaseS(&data->structureDisabled, moduleType, ':')) + continue; - parseStructureCommand(data->structure.chars + startIndex, genJsonConfig, jsonDoc); - - startIndex = colonIndex + 1; + parseStructureCommand(moduleType, genJsonConfig, jsonDoc); } } diff --git a/src/data/help.json b/src/data/help.json index b784ec650..0be2b8c8e 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -468,6 +468,14 @@ "default": "\"fastfetch --print-structure\"" } }, + { + "long": "structure-disabled", + "desc": "Disable specific modules in the structure", + "remark": "Must be a colon-separated list of keys", + "arg": { + "type": "structure" + } + }, { "long": "stat", "desc": "Show time usage (in ms) for individual modules", diff --git a/src/fastfetch.c b/src/fastfetch.c index 840790bef..b57420473 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -710,6 +710,9 @@ static void parseOption(FFdata* data, const char* key, const char* value) ffParseModuleOptions(key, value) ) {} + else if(ffStrEqualsIgnCase(key, "--structure-disabled")) + ffOptionParseString(key, value, &data->structureDisabled); + else { fprintf(stderr, "Error: unknown option: %s\n", key);