mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 16:26:39 +01:00
CPUUsage: support display value per core
This commit is contained in:
@@ -6,12 +6,12 @@
|
||||
#include "util/stringUtils.h"
|
||||
|
||||
#define FF_CPUUSAGE_DISPLAY_NAME "CPU Usage"
|
||||
#define FF_CPUUSAGE_NUM_FORMAT_ARGS 1
|
||||
#define FF_CPUUSAGE_NUM_FORMAT_ARGS 5
|
||||
|
||||
void ffPrintCPUUsage(FFCPUUsageOptions* options)
|
||||
{
|
||||
double percentage = 0.0/0.0;
|
||||
const char* error = ffGetCpuUsageResult(&percentage);
|
||||
FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double));
|
||||
const char* error = ffGetCpuUsageResult(&percentages);
|
||||
|
||||
if(error)
|
||||
{
|
||||
@@ -19,27 +19,68 @@ void ffPrintCPUUsage(FFCPUUsageOptions* options)
|
||||
return;
|
||||
}
|
||||
|
||||
double maxValue = -999, minValue = 999, sumValue = 0;
|
||||
uint32_t maxIndex = 999, minIndex = 999;
|
||||
|
||||
uint32_t index = 0;
|
||||
FF_LIST_FOR_EACH(double, percent, percentages)
|
||||
{
|
||||
sumValue += *percent;
|
||||
if (*percent > maxValue)
|
||||
{
|
||||
maxValue = *percent;
|
||||
maxIndex = index;
|
||||
}
|
||||
if (*percent < minValue)
|
||||
{
|
||||
minValue = *percent;
|
||||
minIndex = index;
|
||||
}
|
||||
++index;
|
||||
}
|
||||
double avgValue = sumValue / (double) percentages.length;
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
|
||||
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
ffAppendPercentBar(&str, percentage, 0, 50, 80);
|
||||
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
if (options->displayType == FF_CPUUSAGE_DISPLAY_TYPE_DEFAULT)
|
||||
{
|
||||
if(str.length > 0)
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
ffAppendPercentNum(&str, percentage, 50, 80, str.length > 0);
|
||||
if(instance.config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
|
||||
ffAppendPercentBar(&str, avgValue, 0, 50, 80);
|
||||
if(instance.config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
|
||||
{
|
||||
if(str.length > 0)
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
ffAppendPercentNum(&str, avgValue, 50, 80, str.length > 0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_LIST_FOR_EACH(double, percent, percentages)
|
||||
{
|
||||
if(str.length > 0)
|
||||
ffStrbufAppendC(&str, ' ');
|
||||
ffAppendPercentNum(&str, *percent, 50, 80, false);
|
||||
}
|
||||
}
|
||||
ffStrbufPutTo(&str, stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY percentageStr = ffStrbufCreate();
|
||||
ffAppendPercentNum(&percentageStr, percentage, 50, 80, false);
|
||||
FF_STRBUF_AUTO_DESTROY avgStr = ffStrbufCreate();
|
||||
ffAppendPercentNum(&avgStr, avgValue, 50, 80, false);
|
||||
FF_STRBUF_AUTO_DESTROY minStr = ffStrbufCreate();
|
||||
ffAppendPercentNum(&minStr, minValue, 50, 80, false);
|
||||
FF_STRBUF_AUTO_DESTROY maxStr = ffStrbufCreate();
|
||||
ffAppendPercentNum(&maxStr, maxValue, 50, 80, false);
|
||||
ffPrintFormat(FF_CPUUSAGE_DISPLAY_NAME, 0, &options->moduleArgs, FF_CPUUSAGE_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &percentageStr}
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &avgStr},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &maxStr},
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &maxIndex},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &minStr},
|
||||
{FF_FORMAT_ARG_TYPE_UINT, &minIndex},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -57,6 +98,16 @@ bool ffParseCPUUsageCommandOptions(FFCPUUsageOptions* options, const char* key,
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
if (ffStrEqualsIgnCase(subKey, "display-type"))
|
||||
{
|
||||
options->displayType = (FFCPUUsageDisplayType) ffOptionParseEnum(key, value, (FFKeyValuePair[]) {
|
||||
{ "default", FF_CPUUSAGE_DISPLAY_TYPE_DEFAULT },
|
||||
{ "separate", FF_CPUUSAGE_DISPLAY_TYPE_SEPARATE },
|
||||
{},
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -78,26 +129,49 @@ void ffParseCPUUsageJsonObject(FFCPUUsageOptions* options, yyjson_val* module)
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
||||
continue;
|
||||
|
||||
if (ffStrEqualsIgnCase(key, "display-type"))
|
||||
{
|
||||
int value;
|
||||
const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) {
|
||||
{ "default", FF_CPUUSAGE_DISPLAY_TYPE_DEFAULT },
|
||||
{ "separate", FF_CPUUSAGE_DISPLAY_TYPE_SEPARATE },
|
||||
{},
|
||||
});
|
||||
if (error)
|
||||
ffPrintErrorString(FF_CPUUSAGE_MODULE_NAME, 0, NULL, FF_PRINT_TYPE_NO_CUSTOM_KEY, "Invalid %s value: %s", key, error);
|
||||
else
|
||||
options->displayType = (FFCPUUsageDisplayType) value;
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPrintError(FF_CPUUSAGE_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
void ffGenerateCPUUsageJson(FF_MAYBE_UNUSED FFCPUUsageOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
||||
{
|
||||
double percentage = 0.0/0.0;
|
||||
const char* error = ffGetCpuUsageResult(&percentage);
|
||||
FF_LIST_AUTO_DESTROY percentages = ffListCreate(sizeof(double));
|
||||
const char* error = ffGetCpuUsageResult(&percentages);
|
||||
|
||||
if(error)
|
||||
{
|
||||
yyjson_mut_obj_add_str(doc, module, "error", error);
|
||||
return;
|
||||
}
|
||||
yyjson_mut_obj_add_real(doc, module, "result", percentage);
|
||||
yyjson_mut_val* result = yyjson_mut_obj_add_arr(doc, module, "result");
|
||||
FF_LIST_FOR_EACH(double, percent, percentages)
|
||||
{
|
||||
yyjson_mut_arr_add_real(doc, result, *percent);
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintCPUUsageHelpFormat(void)
|
||||
{
|
||||
ffPrintModuleFormatHelp(FF_CPUUSAGE_MODULE_NAME, "{1}", FF_CPUUSAGE_NUM_FORMAT_ARGS, (const char* []) {
|
||||
"CPU usage (percentage)"
|
||||
"CPU usage (percentage, average)",
|
||||
"CPU usage (percentage, maximum)",
|
||||
"CPU core index of maximum usage",
|
||||
"CPU usage (percentage, minimum)",
|
||||
"CPU core index of minimum usage",
|
||||
});
|
||||
}
|
||||
|
||||
@@ -4,8 +4,15 @@
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef enum FFCPUUsageDisplayType {
|
||||
FF_CPUUSAGE_DISPLAY_TYPE_DEFAULT,
|
||||
FF_CPUUSAGE_DISPLAY_TYPE_SEPARATE,
|
||||
} FFCPUUsageDisplayType;
|
||||
|
||||
typedef struct FFCPUUsageOptions
|
||||
{
|
||||
FFModuleBaseInfo moduleInfo;
|
||||
FFModuleArgs moduleArgs;
|
||||
|
||||
FFCPUUsageDisplayType displayType;
|
||||
} FFCPUUsageOptions;
|
||||
|
||||
Reference in New Issue
Block a user