CPU: support JSON config

This commit is contained in:
李通洲
2023-03-15 18:17:24 +08:00
parent 9209ac5773
commit f205982ae5
17 changed files with 157 additions and 75 deletions
+111
View File
@@ -0,0 +1,111 @@
#include "fastfetch.h"
#include "common/printing.h"
#include "detection/cpu/cpu.h"
#include "modules/cpu/cpu.h"
#define FF_CPU_NUM_FORMAT_ARGS 8
void ffPrintCPU(FFinstance* instance, FFCPUOptions* options)
{
const FFCPUResult* cpu = ffDetectCPU(instance);
if(cpu->vendor.length == 0 && cpu->name.length == 0 && cpu->coresOnline <= 1)
{
ffPrintError(instance, FF_CPU_MODULE_NAME, 0, &options->moduleArgs, "No CPU detected");
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(instance, FF_CPU_MODULE_NAME, 0, &options->moduleArgs.key);
if(cpu->name.length > 0)
ffStrbufWriteTo(&cpu->name, stdout);
else if(cpu->vendor.length > 0)
{
ffStrbufWriteTo(&cpu->vendor, stdout);
fputs(" CPU", stdout);
}
else
fputs("CPU", stdout);
if(cpu->coresOnline > 1)
printf(" (%u)", cpu->coresOnline);
if(cpu->frequencyMax > 0.0)
printf(" @ %.9g GHz", cpu->frequencyMax);
if(cpu->temperature == cpu->temperature) //FF_CPU_TEMP_UNSET
printf(" - %.1f°C", cpu->temperature);
putchar('\n');
}
else
{
ffPrintFormat(instance, FF_CPU_MODULE_NAME, 0, &options->moduleArgs, FF_CPU_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &cpu->name},
{FF_FORMAT_ARG_TYPE_STRBUF, &cpu->vendor},
{FF_FORMAT_ARG_TYPE_UINT16, &cpu->coresPhysical},
{FF_FORMAT_ARG_TYPE_UINT16, &cpu->coresLogical},
{FF_FORMAT_ARG_TYPE_UINT16, &cpu->coresOnline},
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpu->frequencyMin},
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpu->frequencyMax},
{FF_FORMAT_ARG_TYPE_DOUBLE, &cpu->temperature}
});
}
}
void ffInitCPUOptions(FFCPUOptions* options)
{
options->moduleName = FF_CPU_MODULE_NAME;
ffOptionInitModuleArg(&options->moduleArgs);
options->temp = false;
}
bool ffParseCPUCommandOptions(FFCPUOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_CPU_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
if (strcasecmp(subKey, "temp") == 0)
{
options->temp = ffOptionParseBoolean(value);
return true;
}
return false;
}
void ffDestroyCPUOptions(FFCPUOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
#ifdef FF_HAVE_JSONC
void ffParseCPUJsonObject(FFinstance* instance, json_object* module)
{
FFCPUOptions __attribute__((__cleanup__(ffDestroyCPUOptions))) options;
ffInitCPUOptions(&options);
if (module)
{
json_object_object_foreach(module, key, val)
{
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
continue;
if (strcasecmp(key, "temp") == 0)
{
options.temp = (bool) json_object_get_boolean(val);
continue;
}
ffPrintError(instance, FF_CPU_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
}
}
ffPrintCPU(instance, &options);
}
#endif
+15
View File
@@ -0,0 +1,15 @@
#pragma once
#include "fastfetch.h"
#define FF_CPU_MODULE_NAME "CPU"
void ffPrintCPU(FFinstance* instance, FFCPUOptions* options);
void ffInitCPUOptions(FFCPUOptions* options);
bool ffParseCPUCommandOptions(FFCPUOptions* options, const char* key, const char* value);
void ffDestroyCPUOptions(FFCPUOptions* options);
#ifdef FF_HAVE_JSONC
#include "common/jsonconfig.h"
void ffParseCPUJsonObject(FFinstance* instance, json_object* module);
#endif
+13
View File
@@ -0,0 +1,13 @@
#pragma once
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
#include "common/option.h"
typedef struct FFCPUOptions
{
const char* moduleName;
FFModuleArgs moduleArgs;
bool temp;
} FFCPUOptions;