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

158 lines
5.0 KiB
C
Raw Normal View History

#include "common/printing.h"
2023-06-10 15:01:54 +08:00
#include "common/jsonconfig.h"
2022-08-26 16:02:04 +02:00
#include "common/parsing.h"
2024-01-20 11:28:38 +08:00
#include "common/percent.h"
#include "detection/memory/memory.h"
2023-05-11 21:07:04 +08:00
#include "modules/memory/memory.h"
#include "util/stringUtils.h"
2022-06-18 14:25:31 +02:00
2022-08-26 16:02:04 +02:00
#define FF_MEMORY_NUM_FORMAT_ARGS 3
2021-04-15 18:59:26 +02:00
void ffPrintMemory(FFMemoryOptions* options)
2022-10-18 23:46:42 +08:00
{
2023-05-11 20:39:54 +08:00
FFMemoryResult storage;
const char* error = ffDetectMemory(&storage);
2022-10-18 23:46:42 +08:00
2023-05-11 20:39:54 +08:00
if(error)
2021-04-15 18:59:26 +02:00
{
ffPrintError(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
2021-04-15 18:59:26 +02:00
return;
}
FF_STRBUF_AUTO_DESTROY usedPretty = ffStrbufCreate();
2023-07-27 15:17:54 +08:00
ffParseSize(storage.bytesUsed, &usedPretty);
2022-08-26 16:02:04 +02:00
FF_STRBUF_AUTO_DESTROY totalPretty = ffStrbufCreate();
2023-07-27 15:17:54 +08:00
ffParseSize(storage.bytesTotal, &totalPretty);
2022-08-26 16:02:04 +02:00
2023-08-16 16:46:21 +08:00
double percentage = storage.bytesTotal == 0
2023-05-11 20:39:54 +08:00
? 0
2023-08-16 16:46:21 +08:00
: (double) storage.bytesUsed / (double) storage.bytesTotal * 100.0;
2022-10-18 23:46:42 +08:00
2023-05-11 21:07:04 +08:00
if(options->moduleArgs.outputFormat.length == 0)
2021-03-19 20:57:07 +01:00
{
ffPrintLogoAndKey(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
2023-05-11 20:39:54 +08:00
if (storage.bytesTotal == 0)
puts("Disabled");
else
{
FF_STRBUF_AUTO_DESTROY str = ffStrbufCreate();
2023-10-26 10:26:15 +08:00
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
ffPercentAppendBar(&str, percentage, options->percent);
ffStrbufAppendC(&str, ' ');
}
2023-10-26 10:26:15 +08:00
if(!(instance.config.display.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT))
ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars);
2023-10-26 10:26:15 +08:00
if(instance.config.display.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
ffPercentAppendNum(&str, percentage, options->percent, str.length > 0);
ffStrbufTrimRight(&str, ' ');
ffStrbufPutTo(&str, stdout);
}
2021-03-19 20:57:07 +01:00
}
else
{
FF_STRBUF_AUTO_DESTROY percentageStr = ffStrbufCreate();
ffPercentAppendNum(&percentageStr, percentage, options->percent, false);
ffPrintFormat(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){
2022-08-26 16:02:04 +02:00
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
{FF_FORMAT_ARG_TYPE_STRBUF, &percentageStr},
2021-04-15 18:59:26 +02:00
});
2021-03-19 20:57:07 +01:00
}
2022-09-08 14:51:31 +02:00
}
2023-05-11 21:07:04 +08:00
bool ffParseMemoryCommandOptions(FFMemoryOptions* options, const char* key, const char* value)
{
const char* subKey = ffOptionTestPrefix(key, FF_MEMORY_MODULE_NAME);
if (!subKey) return false;
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
return true;
if (ffPercentParseCommandOptions(key, subKey, value, &options->percent))
return true;
2023-05-11 21:07:04 +08:00
return false;
}
2023-08-17 10:56:54 +08:00
void ffParseMemoryJsonObject(FFMemoryOptions* options, yyjson_val* module)
2023-05-11 21:07:04 +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-05-11 21:07:04 +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-05-11 21:07:04 +08:00
if (ffPercentParseJsonObject(key, val, &options->percent))
continue;
2023-08-17 16:09:41 +08:00
ffPrintError(FF_MEMORY_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key);
2023-05-11 21:07:04 +08:00
}
}
2023-09-06 15:58:43 +08:00
2023-10-24 14:41:29 +08:00
void ffGenerateMemoryJsonConfig(FFMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
{
__attribute__((__cleanup__(ffDestroyMemoryOptions))) FFMemoryOptions defaultOptions;
ffInitMemoryOptions(&defaultOptions);
ffJsonConfigGenerateModuleArgsConfig(doc, module, &defaultOptions.moduleArgs, &options->moduleArgs);
ffPercentGenerateJsonConfig(doc, module, defaultOptions.percent, options->percent);
2023-10-24 14:41:29 +08:00
}
2023-10-23 10:14:22 +08:00
void ffGenerateMemoryJsonResult(FF_MAYBE_UNUSED FFMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
2023-09-06 15:58:43 +08:00
{
FFMemoryResult storage;
const char* error = ffDetectMemory(&storage);
if(error)
{
yyjson_mut_obj_add_str(doc, module, "error", error);
return;
}
yyjson_mut_val* obj = yyjson_mut_obj_add_obj(doc, module, "result");
yyjson_mut_obj_add_uint(doc, obj, "total", storage.bytesTotal);
yyjson_mut_obj_add_uint(doc, obj, "used", storage.bytesUsed);
}
2023-10-07 13:40:40 +08:00
void ffPrintMemoryHelpFormat(void)
{
ffPrintModuleFormatHelp(FF_MEMORY_MODULE_NAME, "{1} / {2} ({3})", FF_MEMORY_NUM_FORMAT_ARGS, (const char* []) {
"Used size",
"Total size",
"Percentage used"
});
}
2023-10-23 13:32:18 +08:00
void ffInitMemoryOptions(FFMemoryOptions* options)
{
ffOptionInitModuleBaseInfo(
&options->moduleInfo,
FF_MEMORY_MODULE_NAME,
"Print system memory usage info",
ffParseMemoryCommandOptions,
ffParseMemoryJsonObject,
ffPrintMemory,
ffGenerateMemoryJsonResult,
ffPrintMemoryHelpFormat,
2023-10-24 14:41:29 +08:00
ffGenerateMemoryJsonConfig
);
2023-10-23 13:32:18 +08:00
ffOptionInitModuleArg(&options->moduleArgs);
options->percent = (FFColorRangeConfig) { 50, 80 };
2023-10-23 13:32:18 +08:00
}
void ffDestroyMemoryOptions(FFMemoryOptions* options)
{
ffOptionDestroyModuleArg(&options->moduleArgs);
}