Files
fastfetch/src/modules/memory.c
T

56 lines
1.7 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
#include "common/printing.h"
2022-08-26 16:02:04 +02:00
#include "common/parsing.h"
#include "detection/memory/memory.h"
2022-06-18 14:25:31 +02:00
2021-04-15 18:59:26 +02:00
#define FF_MEMORY_MODULE_NAME "Memory"
2022-09-08 14:51:31 +02:00
#define FF_SWAP_MODULE_NAME "Swap"
2022-08-26 16:02:04 +02:00
#define FF_MEMORY_NUM_FORMAT_ARGS 3
2021-04-15 18:59:26 +02:00
2022-09-08 14:51:31 +02:00
static void printMemory(FFinstance* instance, const char* name, const FFModuleArgs* moduleArgs, const FFMemoryStorage* storage)
2021-02-18 22:25:36 +01:00
{
2022-09-08 14:51:31 +02:00
if(storage->bytesUsed == 0 && storage->bytesTotal == 0)
2021-04-15 18:59:26 +02:00
{
2022-09-08 14:51:31 +02:00
ffPrintError(instance, name, 0, moduleArgs, "Failed to detect memory");
2021-04-15 18:59:26 +02:00
return;
}
2022-08-26 16:02:04 +02:00
FFstrbuf usedPretty;
ffStrbufInit(&usedPretty);
2022-09-08 14:51:31 +02:00
ffParseSize(storage->bytesUsed, instance->config.binaryPrefixType, &usedPretty);
2022-08-26 16:02:04 +02:00
FFstrbuf totalPretty;
ffStrbufInit(&totalPretty);
2022-09-08 14:51:31 +02:00
ffParseSize(storage->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
2022-08-26 16:02:04 +02:00
2022-09-08 14:51:31 +02:00
if(moduleArgs->outputFormat.length == 0)
2021-03-19 20:57:07 +01:00
{
2022-09-08 14:51:31 +02:00
ffPrintLogoAndKey(instance, name, 0, &moduleArgs->key);
printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, storage->percentage);
2021-03-19 20:57:07 +01:00
}
else
{
2022-09-08 14:51:31 +02:00
ffPrintFormat(instance, name, 0, 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},
2022-09-08 14:51:31 +02:00
{FF_FORMAT_ARG_TYPE_UINT8, &storage->percentage},
2021-04-15 18:59:26 +02:00
});
2021-03-19 20:57:07 +01:00
}
2022-08-26 16:02:04 +02:00
ffStrbufDestroy(&usedPretty);
ffStrbufDestroy(&totalPretty);
2021-03-27 18:14:19 +01:00
}
2022-09-08 14:51:31 +02:00
void ffPrintMemory(FFinstance* instance)
{
const FFMemoryResult* memory = ffDetectMemory();
printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, &memory->ram);
}
void ffPrintSwap(FFinstance* instance)
{
const FFMemoryResult* memory = ffDetectMemory();
printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, &memory->swap);
}