Files
fastfetch/src/modules/cpuusage/cpuusage.c
T

84 lines
2.7 KiB
C
Raw Normal View History

2023-03-17 17:56:31 +08:00
#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2023-03-17 17:56:31 +08:00
#include "common/bar.h"
#include "detection/cpuusage/cpuusage.h"
#include "modules/cpuusage/cpuusage.h"
#include "util/stringUtils.h"
2023-03-17 17:56:31 +08:00
#define FF_CPUUSAGE_DISPLAY_NAME "CPU Usage"
#define FF_CPUUSAGE_NUM_FORMAT_ARGS 1
void ffPrintCPUUsage(FFCPUUsageOptions* options)
2023-03-17 17:56:31 +08:00
{
double percentage = 0.0/0.0;
const char* error = ffGetCpuUsageResult(&percentage);
if(error)
{
ffPrintError(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, "%s", error);
2023-03-17 17:56:31 +08:00
return;
}
if(options->moduleArgs.outputFormat.length == 0)
{
ffPrintLogoAndKey(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-03-17 17:56:31 +08:00
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
2023-08-16 16:46:21 +08:00
ffAppendPercentBar(&str, percentage, 0, 50, 80);
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
2023-03-17 17:56:31 +08:00
{
if(str.length > 0)
ffStrbufAppendC(&str, ' ');
2023-08-16 16:46:21 +08:00
ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0);
2023-03-17 17:56:31 +08:00
}
ffStrbufPutTo(&str, stdout);
}
else
{
FF_STRBUF_AUTO_DESTROY percentageStr = ffStrbufCreate();
ffAppendPercentNum(&percentageStr, percentage, 50, 80, false);
ffPrintFormat(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_CPUUSAGE_NUM_FORMAT_ARGS, (FFformatarg[]){
{FF_FORMAT_ARG_TYPE_STRBUF, &percentageStr}
2023-03-17 17:56:31 +08:00
});
}
}
void ffInitCPUUsageOptions(FFCPUUsageOptions* options)
{
2023-08-29 11:13:55 +08:00
ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_CPUUSAGE_MODULE_NAME, ffParseCPUUsageCommandOptions, ffParseCPUUsageJsonObject, ffPrintCPUUsage, NULL);
2023-03-17 17:56:31 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
}
bool ffParseCPUUsageCommandOptions(FFCPUUsageOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_CPUUSAGE_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
return false;
}
void ffDestroyCPUUsageOptions(FFCPUUsageOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}
2023-08-17 10:56:54 +08:00
void ffParseCPUUsageJsonObject(FFCPUUsageOptions* options, yyjson_val* module)
2023-03-17 17:56:31 +08:00
{
2023-08-17 16:09:41 +08:00
yyjson_val *key_, *val;
size_t idx, max;
yyjson_obj_foreach(module, idx, max, key_, val)
2023-03-17 17:56:31 +08:00
{
2023-08-17 16:09:41 +08:00
const char* key = yyjson_get_str(key_);
if(ffStrEqualsIgnCase(key, "type"))
continue;
2023-06-10 15:01:54 +08:00
2023-08-17 16:09:41 +08:00
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
continue;
2023-03-17 17:56:31 +08:00
2023-08-17 16:09:41 +08:00
ffPrintError(FF_CPUUSAGE_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-03-17 17:56:31 +08:00
}
}