2026-01-06 09:42:54 +08:00
|
|
|
#include "util/commandoption.h"
|
2026-01-05 16:19:47 +08:00
|
|
|
#include "util/color.h"
|
|
|
|
|
#include "util/printing.h"
|
|
|
|
|
#include "util/time.h"
|
|
|
|
|
#include "util/jsonconfig.h"
|
|
|
|
|
#include "util/stringUtils.h"
|
2023-08-29 11:13:55 +08:00
|
|
|
#include "fastfetch_datatext.h"
|
|
|
|
|
#include "modules/modules.h"
|
2023-08-17 16:00:24 +08:00
|
|
|
|
|
|
|
|
#include <ctype.h>
|
2023-08-29 11:13:55 +08:00
|
|
|
#include <inttypes.h>
|
|
|
|
|
|
2025-09-03 09:36:16 +08:00
|
|
|
bool ffParseModuleOptions(const char* key, const char* value)
|
|
|
|
|
{
|
|
|
|
|
if (!ffStrStartsWith(key, "--") || !ffCharIsEnglishAlphabet(key[2])) return false;
|
|
|
|
|
if (value && !*value) value = NULL;
|
|
|
|
|
for (FFModuleBaseInfo** modules = ffModuleInfos[toupper(key[2]) - 'A']; *modules; ++modules)
|
|
|
|
|
{
|
|
|
|
|
FFModuleBaseInfo* baseInfo = *modules;
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, baseInfo->name);
|
|
|
|
|
if (subKey != NULL)
|
|
|
|
|
{
|
|
|
|
|
if (subKey[0] == '\0' || subKey[0] == '-') // Key is exactly the module name or has a leading '-'
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Error: unknown module key %s\n", key);
|
|
|
|
|
exit(477);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY moduleName = ffStrbufCreateS(baseInfo->name);
|
|
|
|
|
ffStrbufLowerCase(&moduleName);
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY jsonKey = ffStrbufCreate();
|
|
|
|
|
bool flag = false;
|
|
|
|
|
for (const char* p = subKey; *p; ++p)
|
|
|
|
|
{
|
|
|
|
|
if (*p == '-')
|
|
|
|
|
{
|
|
|
|
|
if (flag)
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Error: invalid double `-` in module key %s\n", key);
|
|
|
|
|
exit(477);
|
|
|
|
|
}
|
|
|
|
|
flag = true;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
if (!isalpha((unsigned char)*p) && !isdigit((unsigned char)*p))
|
|
|
|
|
{
|
|
|
|
|
fprintf(stderr, "Error: invalid character `%c` in module key %s\n", *p, key);
|
|
|
|
|
exit(477);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (flag)
|
|
|
|
|
{
|
|
|
|
|
flag = false;
|
|
|
|
|
ffStrbufAppendC(&jsonKey, (char) toupper((unsigned char) *p));
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
ffStrbufAppendC(&jsonKey, *p);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fprintf(stderr, "Error: Unsupported module option: %s\n", key);
|
|
|
|
|
fputs(" Support of module options has been removed. Please add the flag to the JSON config instead.\n", stderr);
|
|
|
|
|
fprintf(stderr, " Example (demonstration only): `{ \"modules\": [ { \"type\": \"%s\", \"%s\": %s%s%s } ] }`\n", moduleName.chars, jsonKey.chars, value ? "\"" : "", value ? value : "true", value ? "\"" : "");
|
|
|
|
|
fputs(" See <https://github.com/fastfetch-cli/fastfetch/wiki/Configuration> for more information.\n", stderr);
|
|
|
|
|
exit(477);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-29 11:13:55 +08:00
|
|
|
void ffPrepareCommandOption(FFdata* data)
|
|
|
|
|
{
|
2025-09-19 14:51:31 +08:00
|
|
|
if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_CPUUSAGE_MODULE_NAME, ':'))
|
2023-08-29 11:13:55 +08:00
|
|
|
ffPrepareCPUUsage();
|
|
|
|
|
|
2025-09-19 14:51:31 +08:00
|
|
|
if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_DISKIO_MODULE_NAME, ':'))
|
2025-08-04 14:25:14 +08:00
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyDiskIOOptions))) FFDiskIOOptions options;
|
|
|
|
|
ffInitDiskIOOptions(&options);
|
|
|
|
|
ffPrepareDiskIO(&options);
|
|
|
|
|
}
|
2023-10-08 16:30:09 +08:00
|
|
|
|
2025-09-19 14:51:31 +08:00
|
|
|
if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_NETIO_MODULE_NAME, ':'))
|
2025-08-04 14:25:14 +08:00
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyNetIOOptions))) FFNetIOOptions options;
|
|
|
|
|
ffInitNetIOOptions(&options);
|
|
|
|
|
ffPrepareNetIO(&options);
|
|
|
|
|
}
|
2023-09-26 16:26:15 +08:00
|
|
|
|
2025-09-19 14:51:31 +08:00
|
|
|
if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_PUBLICIP_MODULE_NAME, ':'))
|
2023-08-29 11:13:55 +08:00
|
|
|
{
|
2025-09-19 14:51:31 +08:00
|
|
|
__attribute__((__cleanup__(ffDestroyPublicIpOptions))) FFPublicIPOptions options;
|
|
|
|
|
ffInitPublicIpOptions(&options);
|
|
|
|
|
ffPreparePublicIp(&options);
|
|
|
|
|
}
|
2023-08-29 11:13:55 +08:00
|
|
|
|
2025-09-19 14:51:31 +08:00
|
|
|
if(ffStrbufSeparatedContainIgnCaseS(&data->structure, FF_WEATHER_MODULE_NAME, ':'))
|
|
|
|
|
{
|
|
|
|
|
__attribute__((__cleanup__(ffDestroyWeatherOptions))) FFWeatherOptions options;
|
|
|
|
|
ffInitWeatherOptions(&options);
|
|
|
|
|
ffPrepareWeather(&options);
|
2023-08-29 11:13:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-04 14:25:14 +08:00
|
|
|
static void genJsonConfig(FFModuleBaseInfo* baseInfo, void* options, yyjson_mut_doc* doc)
|
2023-10-26 15:39:28 +08:00
|
|
|
{
|
|
|
|
|
yyjson_mut_val* modules = yyjson_mut_obj_get(doc->root, "modules");
|
|
|
|
|
if (!modules)
|
|
|
|
|
modules = yyjson_mut_obj_add_arr(doc, doc->root, "modules");
|
|
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY type = ffStrbufCreateS(baseInfo->name);
|
|
|
|
|
ffStrbufLowerCase(&type);
|
|
|
|
|
|
2025-08-11 14:49:22 +08:00
|
|
|
if (instance.state.fullConfig)
|
|
|
|
|
{
|
|
|
|
|
yyjson_mut_val* module = yyjson_mut_obj(doc);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "type", &type);
|
|
|
|
|
|
|
|
|
|
if (baseInfo->generateJsonConfig)
|
|
|
|
|
baseInfo->generateJsonConfig(options, doc, module);
|
2023-10-26 15:39:28 +08:00
|
|
|
|
2025-08-11 14:49:22 +08:00
|
|
|
if (yyjson_mut_obj_size(module) > 1)
|
|
|
|
|
yyjson_mut_arr_add_val(modules, module);
|
|
|
|
|
else
|
|
|
|
|
yyjson_mut_arr_add_strbuf(doc, modules, &type);
|
|
|
|
|
}
|
2023-10-26 15:39:28 +08:00
|
|
|
else
|
2025-08-11 14:49:22 +08:00
|
|
|
{
|
2023-10-26 15:39:28 +08:00
|
|
|
yyjson_mut_arr_add_strbuf(doc, modules, &type);
|
2025-08-11 14:49:22 +08:00
|
|
|
}
|
2023-10-26 15:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
2025-08-04 14:25:14 +08:00
|
|
|
static void genJsonResult(FFModuleBaseInfo* baseInfo, void* options, yyjson_mut_doc* doc)
|
2023-10-26 15:39:28 +08:00
|
|
|
{
|
|
|
|
|
yyjson_mut_val* module = yyjson_mut_arr_add_obj(doc, doc->root);
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "type", baseInfo->name);
|
|
|
|
|
if (baseInfo->generateJsonResult)
|
2025-08-04 14:25:14 +08:00
|
|
|
baseInfo->generateJsonResult(options, doc, module);
|
2023-10-26 15:39:28 +08:00
|
|
|
else
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "Unsupported for JSON format");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void parseStructureCommand(
|
|
|
|
|
const char* line,
|
2025-08-04 14:25:14 +08:00
|
|
|
void (*fn)(FFModuleBaseInfo* baseInfo, void* options, yyjson_mut_doc* jsonDoc),
|
2023-10-26 15:39:28 +08:00
|
|
|
yyjson_mut_doc* jsonDoc
|
|
|
|
|
)
|
2023-08-29 11:13:55 +08:00
|
|
|
{
|
2024-05-05 07:10:51 +08:00
|
|
|
if(ffCharIsEnglishAlphabet(line[0]))
|
2023-10-26 15:39:28 +08:00
|
|
|
{
|
|
|
|
|
for (FFModuleBaseInfo** modules = ffModuleInfos[toupper(line[0]) - 'A']; *modules; ++modules)
|
|
|
|
|
{
|
|
|
|
|
FFModuleBaseInfo* baseInfo = *modules;
|
|
|
|
|
if (ffStrEqualsIgnCase(line, baseInfo->name))
|
|
|
|
|
{
|
2025-08-04 14:25:14 +08:00
|
|
|
uint8_t optionBuf[FF_OPTION_MAX_SIZE];
|
|
|
|
|
baseInfo->initOptions(optionBuf);
|
2023-10-26 15:39:28 +08:00
|
|
|
if (__builtin_expect(jsonDoc != NULL, false))
|
2025-08-04 14:25:14 +08:00
|
|
|
fn(baseInfo, optionBuf, jsonDoc);
|
2023-10-26 15:39:28 +08:00
|
|
|
else
|
2025-08-04 14:25:14 +08:00
|
|
|
baseInfo->printModule(optionBuf);
|
|
|
|
|
baseInfo->destroyOptions(optionBuf);
|
2023-10-26 15:39:28 +08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-03-07 11:10:18 +08:00
|
|
|
ffPrintError(line, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "<no implementation provided>");
|
2023-08-29 11:13:55 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-26 15:39:28 +08:00
|
|
|
void ffPrintCommandOption(FFdata* data, yyjson_mut_doc* jsonDoc)
|
2023-08-29 11:13:55 +08:00
|
|
|
{
|
|
|
|
|
//Parse the structure and call the modules
|
2024-08-13 15:28:07 +08:00
|
|
|
int32_t thres = instance.config.display.stat;
|
2023-08-29 11:13:55 +08:00
|
|
|
uint32_t startIndex = 0;
|
|
|
|
|
while (startIndex < data->structure.length)
|
|
|
|
|
{
|
|
|
|
|
uint32_t colonIndex = ffStrbufNextIndexC(&data->structure, startIndex, ':');
|
|
|
|
|
data->structure.chars[colonIndex] = '\0';
|
|
|
|
|
|
2024-08-12 11:03:52 +08:00
|
|
|
double ms = 0;
|
2024-08-12 16:16:47 +08:00
|
|
|
if(thres >= 0)
|
2023-08-29 11:13:55 +08:00
|
|
|
ms = ffTimeGetTick();
|
|
|
|
|
|
2024-05-08 11:05:00 +08:00
|
|
|
parseStructureCommand(data->structure.chars + startIndex, genJsonResult, jsonDoc);
|
2023-08-29 11:13:55 +08:00
|
|
|
|
2024-08-12 16:16:47 +08:00
|
|
|
if(thres >= 0)
|
2023-08-29 11:13:55 +08:00
|
|
|
{
|
2023-09-14 15:53:44 +08:00
|
|
|
ms = ffTimeGetTick() - ms;
|
|
|
|
|
|
2023-10-26 15:39:28 +08:00
|
|
|
if (jsonDoc)
|
2023-09-14 15:53:44 +08:00
|
|
|
{
|
2023-10-26 15:39:28 +08:00
|
|
|
yyjson_mut_val* moduleJson = yyjson_mut_arr_get_last(jsonDoc->root);
|
2024-08-12 11:03:52 +08:00
|
|
|
yyjson_mut_obj_add_real(jsonDoc, moduleJson, "stat", ms);
|
2023-09-14 15:53:44 +08:00
|
|
|
}
|
2023-08-29 11:13:55 +08:00
|
|
|
else
|
2023-09-14 15:53:44 +08:00
|
|
|
{
|
2024-08-12 16:16:47 +08:00
|
|
|
char str[64];
|
2024-08-12 11:03:52 +08:00
|
|
|
int len = snprintf(str, sizeof str, "%.3fms", ms);
|
2024-08-12 16:16:47 +08:00
|
|
|
if (thres > 0)
|
|
|
|
|
snprintf(str, sizeof str, "\e[%sm%.3fms\e[m", (ms <= thres ? FF_COLOR_FG_GREEN : ms <= 2 * thres ? FF_COLOR_FG_YELLOW : FF_COLOR_FG_RED), ms);
|
2025-05-16 10:33:33 +08:00
|
|
|
printf("\e7\e[1A\e[9999999C\e[%dD%s\e8", len, str); // Save; Up 1; Right 9999999; Left <len>; Print <str>; Load
|
2023-09-14 15:53:44 +08:00
|
|
|
}
|
2023-08-29 11:13:55 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if defined(_WIN32)
|
2023-10-26 15:39:28 +08:00
|
|
|
if (!jsonDoc && !instance.config.display.noBuffer) fflush(stdout);
|
2023-08-29 11:13:55 +08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
startIndex = colonIndex + 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-23 13:39:07 +08:00
|
|
|
|
2023-10-26 15:39:28 +08:00
|
|
|
void ffMigrateCommandOptionToJsonc(FFdata* data, yyjson_mut_doc* jsonDoc)
|
2023-10-23 13:39:07 +08:00
|
|
|
{
|
|
|
|
|
//If we don't have a custom structure, use the default one
|
|
|
|
|
if(data->structure.length == 0)
|
2024-03-04 13:45:00 +08:00
|
|
|
ffStrbufAppendS(&data->structure, FASTFETCH_DATATEXT_STRUCTURE); // Cannot use `ffStrbufSetStatic` here because we will modify the string
|
2023-10-26 15:39:28 +08:00
|
|
|
|
|
|
|
|
//Parse the structure and call the modules
|
|
|
|
|
uint32_t startIndex = 0;
|
|
|
|
|
while (startIndex < data->structure.length)
|
|
|
|
|
{
|
|
|
|
|
uint32_t colonIndex = ffStrbufNextIndexC(&data->structure, startIndex, ':');
|
|
|
|
|
data->structure.chars[colonIndex] = '\0';
|
|
|
|
|
|
2024-05-08 11:05:00 +08:00
|
|
|
parseStructureCommand(data->structure.chars + startIndex, genJsonConfig, jsonDoc);
|
2023-10-26 15:39:28 +08:00
|
|
|
|
|
|
|
|
startIndex = colonIndex + 1;
|
|
|
|
|
}
|
2023-10-23 13:39:07 +08:00
|
|
|
}
|