mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2025-02-20 11:43:27 +08:00
Loadavg: new module
This commit is contained in:
parent
4204cfc500
commit
738daa1c77
@ -334,6 +334,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/gamepad/gamepad.c
|
||||
src/modules/kernel/kernel.c
|
||||
src/modules/lm/lm.c
|
||||
src/modules/loadavg/loadavg.c
|
||||
src/modules/locale/locale.c
|
||||
src/modules/localip/localip.c
|
||||
src/modules/memory/memory.c
|
||||
@ -417,6 +418,7 @@ if(LINUX)
|
||||
src/detection/icons/icons_linux.c
|
||||
src/detection/libc/libc_linux.c
|
||||
src/detection/lm/lm_linux.c
|
||||
src/detection/loadavg/loadavg_linux.c
|
||||
src/detection/locale/locale_linux.c
|
||||
src/detection/localip/localip_linux.c
|
||||
src/detection/gamepad/gamepad_linux.c
|
||||
@ -472,6 +474,7 @@ elseif(ANDROID)
|
||||
src/detection/icons/icons_nosupport.c
|
||||
src/detection/libc/libc_android.c
|
||||
src/detection/lm/lm_nosupport.c
|
||||
src/detection/loadavg/loadavg_linux.c
|
||||
src/detection/locale/locale_linux.c
|
||||
src/detection/localip/localip_linux.c
|
||||
src/detection/gamepad/gamepad_nosupport.c
|
||||
@ -538,6 +541,7 @@ elseif(BSD)
|
||||
src/detection/lm/lm_linux.c
|
||||
src/detection/icons/icons_linux.c
|
||||
src/detection/libc/libc_bsd.c
|
||||
src/detection/loadavg/loadavg_linux.c
|
||||
src/detection/locale/locale_linux.c
|
||||
src/detection/localip/localip_linux.c
|
||||
src/detection/gamepad/gamepad_bsd.c
|
||||
@ -592,8 +596,9 @@ elseif(APPLE)
|
||||
src/detection/gpu/gpu_apple.c
|
||||
src/detection/gpu/gpu_apple.m
|
||||
src/detection/host/host_apple.c
|
||||
src/detection/lm/lm_nosupport.c
|
||||
src/detection/icons/icons_nosupport.c
|
||||
src/detection/lm/lm_nosupport.c
|
||||
src/detection/loadavg/loadavg_linux.c
|
||||
src/detection/libc/libc_apple.c
|
||||
src/detection/locale/locale_linux.c
|
||||
src/detection/localip/localip_linux.c
|
||||
@ -651,6 +656,7 @@ elseif(WIN32)
|
||||
src/detection/icons/icons_windows.c
|
||||
src/detection/libc/libc_windows.cpp
|
||||
src/detection/lm/lm_nosupport.c
|
||||
src/detection/loadavg/loadavg_nosupport.c
|
||||
src/detection/locale/locale_windows.c
|
||||
src/detection/localip/localip_windows.c
|
||||
src/detection/gamepad/gamepad_windows.c
|
||||
|
@ -702,6 +702,7 @@
|
||||
"icons",
|
||||
"kernel",
|
||||
"lm",
|
||||
"loadavg",
|
||||
"locale",
|
||||
"localip",
|
||||
"media",
|
||||
@ -1555,6 +1556,32 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Loadavg",
|
||||
"type": "object",
|
||||
"additionalProperties": false,
|
||||
"properties": {
|
||||
"type": {
|
||||
"const": "loadavg",
|
||||
"description": "Print system load averages"
|
||||
},
|
||||
"key": {
|
||||
"$ref": "#/$defs/key"
|
||||
},
|
||||
"keyColor": {
|
||||
"$ref": "#/$defs/keyColor"
|
||||
},
|
||||
"outputColor": {
|
||||
"$ref": "#/$defs/outputColor"
|
||||
},
|
||||
"keyWidth": {
|
||||
"$ref": "#/$defs/keyWidth"
|
||||
},
|
||||
"format": {
|
||||
"$ref": "#/$defs/format"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "NetIO",
|
||||
"type": "object",
|
||||
|
@ -71,6 +71,7 @@ static FFModuleBaseInfo* K[] = {
|
||||
|
||||
static FFModuleBaseInfo* L[] = {
|
||||
(void*) &instance.config.modules.lm,
|
||||
(void*) &instance.config.modules.loadavg,
|
||||
(void*) &instance.config.modules.locale,
|
||||
(void*) &instance.config.modules.localIP,
|
||||
NULL,
|
||||
|
5
src/detection/loadavg/loadavg.h
Normal file
5
src/detection/loadavg/loadavg.h
Normal file
@ -0,0 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
const char* ffDetectLoadavg(double result[3]);
|
8
src/detection/loadavg/loadavg_linux.c
Normal file
8
src/detection/loadavg/loadavg_linux.c
Normal file
@ -0,0 +1,8 @@
|
||||
#include "detection/loadavg/loadavg.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
const char* ffDetectLoadavg(double result[3])
|
||||
{
|
||||
return getloadavg(result, 3) < 0 ? "getloadavg() failed" : NULL;
|
||||
}
|
6
src/detection/loadavg/loadavg_nosupport.c
Normal file
6
src/detection/loadavg/loadavg_nosupport.c
Normal file
@ -0,0 +1,6 @@
|
||||
#include "detection/loadavg/loadavg.h"
|
||||
|
||||
const char* ffDetectLoadavg(FF_MAYBE_UNUSED double result[3])
|
||||
{
|
||||
return "Not supported on this platform";
|
||||
}
|
114
src/modules/loadavg/loadavg.c
Normal file
114
src/modules/loadavg/loadavg.c
Normal file
@ -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);
|
||||
}
|
9
src/modules/loadavg/loadavg.h
Normal file
9
src/modules/loadavg/loadavg.h
Normal file
@ -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);
|
11
src/modules/loadavg/option.h
Normal file
11
src/modules/loadavg/option.h
Normal file
@ -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"
|
||||
|
@ -29,6 +29,7 @@ void ffOptionsInitModules(FFOptionsModules* options)
|
||||
ffInitIconsOptions(&options->icons);
|
||||
ffInitKernelOptions(&options->kernel);
|
||||
ffInitLMOptions(&options->lm);
|
||||
ffInitLoadavgOptions(&options->loadavg);
|
||||
ffInitLocalIpOptions(&options->localIP);
|
||||
ffInitLocaleOptions(&options->locale);
|
||||
ffInitMediaOptions(&options->media);
|
||||
@ -93,6 +94,7 @@ void ffOptionsDestroyModules(FFOptionsModules* options)
|
||||
ffDestroyIconsOptions(&options->icons);
|
||||
ffDestroyKernelOptions(&options->kernel);
|
||||
ffDestroyLMOptions(&options->lm);
|
||||
ffDestroyLoadavgOptions(&options->loadavg);
|
||||
ffDestroyLocalIpOptions(&options->localIP);
|
||||
ffDestroyLocaleOptions(&options->locale);
|
||||
ffDestroyMediaOptions(&options->media);
|
||||
|
@ -30,6 +30,7 @@ typedef struct FFOptionsModules
|
||||
FFIconsOptions icons;
|
||||
FFKernelOptions kernel;
|
||||
FFLMOptions lm;
|
||||
FFLoadavgOptions loadavg;
|
||||
FFLocalIpOptions localIP;
|
||||
FFLocaleOptions locale;
|
||||
FFMediaOptions media;
|
||||
|
Loading…
x
Reference in New Issue
Block a user