mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-08 06:00:08 +01:00
Loadavg: new module
This commit is contained in:
@@ -0,0 +1,114 @@
|
||||
#include "common/printing.h"
|
||||
#include "common/jsonconfig.h"
|
||||
#include "detection/loadavg/loadavg.h"
|
||||
#include "modules/loadavg/loadavg.h"
|
||||
#include "util/stringUtils.h"
|
||||
|
||||
#define FF_LOADAVG_NUM_FORMAT_ARGS 3
|
||||
|
||||
void ffPrintLoadavg(FFLoadavgOptions* options)
|
||||
{
|
||||
double result[3] = { 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0 };
|
||||
|
||||
const char* error = ffDetectLoadavg(result);
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(FF_LOADAVG_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(FF_LOADAVG_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
||||
printf("%.2f, %.2f, %.2f\n", result[0], result[1], result[2]);
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_PRINT_FORMAT_CHECKED(FF_LOADAVG_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_LOADAVG_NUM_FORMAT_ARGS, ((FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_DOUBLE, &result[0]},
|
||||
{FF_FORMAT_ARG_TYPE_DOUBLE, &result[1]},
|
||||
{FF_FORMAT_ARG_TYPE_DOUBLE, &result[2]},
|
||||
}));
|
||||
}
|
||||
}
|
||||
|
||||
bool ffParseLoadavgCommandOptions(FFLoadavgOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_LOADAVG_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffParseLoadavgJsonObject(FFLoadavgOptions* options, yyjson_val* module)
|
||||
{
|
||||
yyjson_val *key_, *val;
|
||||
size_t idx, max;
|
||||
yyjson_obj_foreach(module, idx, max, key_, val)
|
||||
{
|
||||
const char* key = yyjson_get_str(key_);
|
||||
if(ffStrEqualsIgnCase(key, "type"))
|
||||
continue;
|
||||
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(FF_LOADAVG_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
void ffGenerateLoadavgJsonConfig(FFLoadavgOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
||||
{
|
||||
__attribute__((__cleanup__(ffDestroyLoadavgOptions))) FFLoadavgOptions defaultOptions;
|
||||
ffInitLoadavgOptions(&defaultOptions);
|
||||
|
||||
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
|
||||
}
|
||||
|
||||
void ffGenerateLoadavgJsonResult(FF_MAYBE_UNUSED FFLoadavgOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
||||
{
|
||||
double result[3] = { 0.0 / 0.0, 0.0 / 0.0, 0.0 / 0.0 };
|
||||
|
||||
const char* error = ffDetectLoadavg(result);
|
||||
if(error)
|
||||
{
|
||||
yyjson_mut_obj_add_str(doc, module, "error", error);
|
||||
return;
|
||||
}
|
||||
|
||||
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
|
||||
for (size_t i = 0; i < 3; i++)
|
||||
yyjson_mut_arr_add_real(doc, arr, result[i]);
|
||||
}
|
||||
|
||||
void ffPrintLoadavgHelpFormat(void)
|
||||
{
|
||||
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_LOADAVG_MODULE_NAME, "{1}, {2}, {3}", FF_LOADAVG_NUM_FORMAT_ARGS, ((const char* []) {
|
||||
"Load average over 1min",
|
||||
"Load average over 5min",
|
||||
"Load average over 15min",
|
||||
}));
|
||||
}
|
||||
|
||||
void ffInitLoadavgOptions(FFLoadavgOptions* options)
|
||||
{
|
||||
ffOptionInitModuleBaseInfo(
|
||||
&options->moduleInfo,
|
||||
FF_LOADAVG_MODULE_NAME,
|
||||
"Print system load averages",
|
||||
ffParseLoadavgCommandOptions,
|
||||
ffParseLoadavgJsonObject,
|
||||
ffPrintLoadavg,
|
||||
ffGenerateLoadavgJsonResult,
|
||||
ffPrintLoadavgHelpFormat,
|
||||
ffGenerateLoadavgJsonConfig
|
||||
);
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
void ffDestroyLoadavgOptions(FFLoadavgOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_LOADAVG_MODULE_NAME "Loadavg"
|
||||
|
||||
void ffPrintLoadavg(FFLoadavgOptions* options);
|
||||
void ffInitLoadavgOptions(FFLoadavgOptions* options);
|
||||
void ffDestroyLoadavgOptions(FFLoadavgOptions* options);
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFLoadavgOptions
|
||||
{
|
||||
FFModuleBaseInfo moduleInfo;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFLoadavgOptions;
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "modules/icons/icons.h"
|
||||
#include "modules/kernel/kernel.h"
|
||||
#include "modules/lm/lm.h"
|
||||
#include "modules/loadavg/loadavg.h"
|
||||
#include "modules/locale/locale.h"
|
||||
#include "modules/localip/localip.h"
|
||||
#include "modules/media/media.h"
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "modules/gpu/option.h"
|
||||
#include "modules/icons/option.h"
|
||||
#include "modules/kernel/option.h"
|
||||
#include "modules/loadavg/option.h"
|
||||
#include "modules/locale/option.h"
|
||||
#include "modules/lm/option.h"
|
||||
#include "modules/localip/option.h"
|
||||
|
||||
Reference in New Issue
Block a user