2026-01-06 09:48:38 +08:00
|
|
|
#include "common/commandoption.h"
|
|
|
|
|
#include "common/color.h"
|
|
|
|
|
#include "common/printing.h"
|
|
|
|
|
#include "common/time.h"
|
|
|
|
|
#include "common/jsonconfig.h"
|
2026-05-28 15:58:08 +08:00
|
|
|
#include "common/strutil.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>
|
|
|
|
|
|
2026-03-29 09:28:49 +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) {
|
2025-09-03 09:36:16 +08:00
|
|
|
FFModuleBaseInfo* baseInfo = *modules;
|
|
|
|
|
const char* subKey = ffOptionTestPrefix(key, baseInfo->name);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (subKey != NULL) {
|
2025-09-03 09:36:16 +08:00
|
|
|
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;
|
2026-03-29 09:28:49 +08:00
|
|
|
for (const char* p = subKey; *p; ++p) {
|
|
|
|
|
if (*p == '-') {
|
|
|
|
|
if (flag) {
|
2025-09-03 09:36:16 +08:00
|
|
|
fprintf(stderr, "Error: invalid double `-` in module key %s\n", key);
|
|
|
|
|
exit(477);
|
|
|
|
|
}
|
|
|
|
|
flag = true;
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
|
|
|
|
if (!isalpha((unsigned char) *p) && !isdigit((unsigned char) *p)) {
|
2025-09-03 09:36:16 +08:00
|
|
|
fprintf(stderr, "Error: invalid character `%c` in module key %s\n", *p, key);
|
|
|
|
|
exit(477);
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (flag) {
|
2025-09-03 09:36:16 +08:00
|
|
|
flag = false;
|
|
|
|
|
ffStrbufAppendC(&jsonKey, (char) toupper((unsigned char) *p));
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2025-09-03 09:36:16 +08:00
|
|
|
ffStrbufAppendC(&jsonKey, *p);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2025-09-03 09:36:16 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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);
|
2026-01-22 09:06:23 +08:00
|
|
|
fprintf(stderr, " Example (demonstration only): `{ \"modules\": [ { \"type\": \"%s\", \"%s\": %s%s%s } ] }`\n", moduleName.chars, jsonKey.chars, value ? "\"" : "", value ?: "true", value ? "\"" : "");
|
2025-09-03 09:36:16 +08:00
|
|
|
fputs(" See <https://github.com/fastfetch-cli/fastfetch/wiki/Configuration> for more information.\n", stderr);
|
|
|
|
|
exit(477);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffPrepareCommandOption(FFdata* data) {
|
2026-01-15 10:35:01 +08:00
|
|
|
char* moduleType = NULL;
|
|
|
|
|
size_t moduleLen = 0;
|
2026-03-29 09:28:49 +08:00
|
|
|
while (ffStrbufGetdelim(&moduleType, &moduleLen, ':', &data->structure)) {
|
|
|
|
|
#define FF_IF_MODULE_MATCH(moduleNameConstant) if (moduleLen == strlen(moduleNameConstant) && ffStrEqualsIgnCase(moduleType, moduleNameConstant) && !ffStrbufSeparatedContainIgnCaseS(&data->structureDisabled, moduleNameConstant, ':'))
|
2023-10-08 16:30:09 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
switch (moduleType[0]) {
|
2026-05-28 23:14:49 +08:00
|
|
|
#if !FF_DISABLE_MODULE_CPUUSAGE
|
2026-03-29 09:28:49 +08:00
|
|
|
case 'C':
|
|
|
|
|
case 'c':
|
2026-01-15 10:35:01 +08:00
|
|
|
FF_IF_MODULE_MATCH(FF_CPUUSAGE_MODULE_NAME)
|
2026-03-29 09:28:49 +08:00
|
|
|
ffPrepareCPUUsage();
|
2026-01-15 10:35:01 +08:00
|
|
|
break;
|
2026-05-28 15:38:18 +08:00
|
|
|
#endif
|
2023-09-26 16:26:15 +08:00
|
|
|
|
2026-05-28 23:14:49 +08:00
|
|
|
#if !FF_DISABLE_MODULE_DISKIO
|
2026-03-29 09:28:49 +08:00
|
|
|
case 'D':
|
|
|
|
|
case 'd':
|
|
|
|
|
FF_IF_MODULE_MATCH(FF_DISKIO_MODULE_NAME) {
|
2026-04-09 16:20:59 +08:00
|
|
|
FF_A_CLEANUP(ffDestroyDiskIOOptions) FFDiskIOOptions options;
|
2026-01-15 10:35:01 +08:00
|
|
|
ffInitDiskIOOptions(&options);
|
|
|
|
|
ffPrepareDiskIO(&options);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2026-05-28 15:38:18 +08:00
|
|
|
#endif
|
2023-08-29 11:13:55 +08:00
|
|
|
|
2026-05-28 23:14:49 +08:00
|
|
|
#if !FF_DISABLE_MODULE_NETIO
|
2026-03-29 09:28:49 +08:00
|
|
|
case 'N':
|
|
|
|
|
case 'n':
|
|
|
|
|
FF_IF_MODULE_MATCH(FF_NETIO_MODULE_NAME) {
|
2026-04-09 16:20:59 +08:00
|
|
|
FF_A_CLEANUP(ffDestroyNetIOOptions) FFNetIOOptions options;
|
2026-01-15 10:35:01 +08:00
|
|
|
ffInitNetIOOptions(&options);
|
|
|
|
|
ffPrepareNetIO(&options);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2026-05-28 15:38:18 +08:00
|
|
|
#endif
|
2026-01-15 10:35:01 +08:00
|
|
|
|
2026-05-28 23:14:49 +08:00
|
|
|
#if !FF_DISABLE_MODULE_PUBLICIP
|
2026-03-29 09:28:49 +08:00
|
|
|
case 'P':
|
|
|
|
|
case 'p':
|
|
|
|
|
FF_IF_MODULE_MATCH(FF_PUBLICIP_MODULE_NAME) {
|
2026-04-09 16:20:59 +08:00
|
|
|
FF_A_CLEANUP(ffDestroyPublicIpOptions) FFPublicIPOptions options;
|
2026-01-15 10:35:01 +08:00
|
|
|
ffInitPublicIpOptions(&options);
|
|
|
|
|
ffPreparePublicIp(&options);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2026-05-28 15:38:18 +08:00
|
|
|
#endif
|
2026-01-15 10:35:01 +08:00
|
|
|
|
2026-05-28 23:14:49 +08:00
|
|
|
#if !FF_DISABLE_MODULE_WEATHER
|
2026-03-29 09:28:49 +08:00
|
|
|
case 'W':
|
|
|
|
|
case 'w':
|
|
|
|
|
FF_IF_MODULE_MATCH(FF_WEATHER_MODULE_NAME) {
|
2026-04-09 16:20:59 +08:00
|
|
|
FF_A_CLEANUP(ffDestroyWeatherOptions) FFWeatherOptions options;
|
2026-01-15 10:35:01 +08:00
|
|
|
ffInitWeatherOptions(&options);
|
|
|
|
|
ffPrepareWeather(&options);
|
|
|
|
|
}
|
|
|
|
|
break;
|
2026-05-28 15:38:18 +08:00
|
|
|
#endif
|
2026-01-15 10:35:01 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
#undef FF_IF_MODULE_MATCH
|
2023-08-29 11:13:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
static void genJsonConfig(FFdata* data, FFModuleBaseInfo* baseInfo, void* options) {
|
2026-01-15 15:03:38 +08:00
|
|
|
yyjson_mut_doc* doc = data->resultDoc;
|
|
|
|
|
|
2023-10-26 15:39:28 +08:00
|
|
|
yyjson_mut_val* modules = yyjson_mut_obj_get(doc->root, "modules");
|
2026-03-29 09:28:49 +08:00
|
|
|
if (!modules) {
|
2023-10-26 15:39:28 +08:00
|
|
|
modules = yyjson_mut_obj_add_arr(doc, doc->root, "modules");
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-10-26 15:39:28 +08:00
|
|
|
|
|
|
|
|
FF_STRBUF_AUTO_DESTROY type = ffStrbufCreateS(baseInfo->name);
|
|
|
|
|
ffStrbufLowerCase(&type);
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (data->docType == FF_RESULT_DOC_TYPE_CONFIG_FULL) {
|
2025-08-11 14:49:22 +08:00
|
|
|
yyjson_mut_val* module = yyjson_mut_obj(doc);
|
|
|
|
|
yyjson_mut_obj_add_strbuf(doc, module, "type", &type);
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (baseInfo->generateJsonConfig) {
|
2025-08-11 14:49:22 +08:00
|
|
|
baseInfo->generateJsonConfig(options, doc, module);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-10-26 15:39:28 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (yyjson_mut_obj_size(module) > 1) {
|
2025-08-11 14:49:22 +08:00
|
|
|
yyjson_mut_arr_add_val(modules, module);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2025-08-11 14:49:22 +08:00
|
|
|
yyjson_mut_arr_add_strbuf(doc, modules, &type);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
|
|
|
|
} else {
|
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
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
static void genJsonResult(FFdata* data, FFModuleBaseInfo* baseInfo, void* options) {
|
2026-01-15 15:03:38 +08:00
|
|
|
yyjson_mut_doc* doc = data->resultDoc;
|
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);
|
2026-03-29 09:28:49 +08:00
|
|
|
if (baseInfo->generateJsonResult) {
|
2025-08-04 14:25:14 +08:00
|
|
|
baseInfo->generateJsonResult(options, doc, module);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2023-10-26 15:39:28 +08:00
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "Unsupported for JSON format");
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-10-26 15:39:28 +08:00
|
|
|
}
|
|
|
|
|
|
2026-04-27 20:28:11 +08:00
|
|
|
static bool parseStructureCommand(
|
2026-01-15 15:03:38 +08:00
|
|
|
FFdata* data,
|
2023-10-26 15:39:28 +08:00
|
|
|
const char* line,
|
2026-03-29 09:28:49 +08:00
|
|
|
void (*fn)(FFdata*, FFModuleBaseInfo* baseInfo, void* options)) {
|
|
|
|
|
if (ffCharIsEnglishAlphabet(line[0])) {
|
|
|
|
|
for (FFModuleBaseInfo** modules = ffModuleInfos[toupper(line[0]) - 'A']; *modules; ++modules) {
|
2023-10-26 15:39:28 +08:00
|
|
|
FFModuleBaseInfo* baseInfo = *modules;
|
2026-03-29 09:28:49 +08:00
|
|
|
if (ffStrEqualsIgnCase(line, baseInfo->name)) {
|
2025-08-04 14:25:14 +08:00
|
|
|
uint8_t optionBuf[FF_OPTION_MAX_SIZE];
|
|
|
|
|
baseInfo->initOptions(optionBuf);
|
2026-04-27 20:28:11 +08:00
|
|
|
if (data->resultDoc != NULL) {
|
2026-01-15 15:03:38 +08:00
|
|
|
fn(data, baseInfo, optionBuf);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
2025-08-04 14:25:14 +08:00
|
|
|
baseInfo->printModule(optionBuf);
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2025-08-04 14:25:14 +08:00
|
|
|
baseInfo->destroyOptions(optionBuf);
|
2026-04-27 20:28:11 +08:00
|
|
|
return true;
|
2023-10-26 15:39:28 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-13 19:28:36 +08:00
|
|
|
if (data->resultDoc) {
|
2026-04-27 20:28:11 +08:00
|
|
|
yyjson_mut_doc* doc = data->resultDoc;
|
|
|
|
|
yyjson_mut_val* module = yyjson_mut_arr_add_obj(doc, doc->root);
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "type", line);
|
|
|
|
|
yyjson_mut_obj_add_str(doc, module, "error", "Unknown module type");
|
|
|
|
|
} else {
|
|
|
|
|
ffPrintError(line, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "<no implementation provided>");
|
|
|
|
|
}
|
|
|
|
|
return false;
|
2023-08-29 11:13:55 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffPrintCommandOption(FFdata* data) {
|
|
|
|
|
// Parse the structure and call the modules
|
2024-08-13 15:28:07 +08:00
|
|
|
int32_t thres = instance.config.display.stat;
|
2026-01-15 10:35:01 +08:00
|
|
|
|
|
|
|
|
char* moduleType = NULL;
|
|
|
|
|
size_t moduleLen = 0;
|
2026-03-29 09:28:49 +08:00
|
|
|
while (ffStrbufGetdelim(&moduleType, &moduleLen, ':', &data->structure)) {
|
|
|
|
|
if (ffStrbufSeparatedContainIgnCaseS(&data->structureDisabled, moduleType, ':')) {
|
2026-01-15 10:35:01 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-08-29 11:13:55 +08:00
|
|
|
|
2024-08-12 11:03:52 +08:00
|
|
|
double ms = 0;
|
2026-03-29 09:28:49 +08:00
|
|
|
if (thres >= 0) {
|
2023-08-29 11:13:55 +08:00
|
|
|
ms = ffTimeGetTick();
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-08-29 11:13:55 +08:00
|
|
|
|
2026-01-15 15:03:38 +08:00
|
|
|
parseStructureCommand(data, moduleType, genJsonResult);
|
2023-08-29 11:13:55 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (thres >= 0) {
|
2023-09-14 15:53:44 +08:00
|
|
|
ms = ffTimeGetTick() - ms;
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
if (data->resultDoc) {
|
2026-01-15 15:03:38 +08:00
|
|
|
yyjson_mut_val* moduleJson = yyjson_mut_arr_get_last(data->resultDoc->root);
|
|
|
|
|
yyjson_mut_obj_add_real(data->resultDoc, moduleJson, "stat", ms);
|
2026-03-29 09:28:49 +08:00
|
|
|
} else {
|
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);
|
2026-03-29 09:28:49 +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);
|
|
|
|
|
}
|
2026-05-23 09:50:49 +08:00
|
|
|
printf("\e7\e[1A\e[9999999C\e[%dD%s\e8", len - 1, str); // Save; Up 1; Right 9999999; Left <len - 1>; Print <str>; Load
|
2023-09-14 15:53:44 +08:00
|
|
|
}
|
2023-08-29 11:13:55 +08:00
|
|
|
}
|
|
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
#if defined(_WIN32)
|
|
|
|
|
if (!data->resultDoc && !instance.config.display.noBuffer) {
|
|
|
|
|
fflush(stdout);
|
|
|
|
|
}
|
|
|
|
|
#endif
|
2023-08-29 11:13:55 +08:00
|
|
|
}
|
|
|
|
|
}
|
2023-10-23 13:39:07 +08:00
|
|
|
|
2026-03-29 09:28:49 +08:00
|
|
|
void ffMigrateCommandOptionToJsonc(FFdata* data) {
|
|
|
|
|
// 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
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-10-26 15:39:28 +08:00
|
|
|
|
2026-01-15 10:35:01 +08:00
|
|
|
char* moduleType = NULL;
|
|
|
|
|
size_t moduleLen = 0;
|
2026-03-29 09:28:49 +08:00
|
|
|
while (ffStrbufGetdelim(&moduleType, &moduleLen, ':', &data->structure)) {
|
|
|
|
|
if (ffStrbufSeparatedContainIgnCaseS(&data->structureDisabled, moduleType, ':')) {
|
2026-01-15 10:35:01 +08:00
|
|
|
continue;
|
2026-03-29 09:28:49 +08:00
|
|
|
}
|
2023-10-26 15:39:28 +08:00
|
|
|
|
2026-01-15 15:03:38 +08:00
|
|
|
parseStructureCommand(data, moduleType, genJsonConfig);
|
2023-10-26 15:39:28 +08:00
|
|
|
}
|
2023-10-23 13:39:07 +08:00
|
|
|
}
|