Files
fastfetch/src/modules/memory.c
T

86 lines
2.5 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 "common/bar.h"
#include "detection/memory/memory.h"
2022-10-18 23:46:42 +08:00
#include "detection/swap/swap.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-10-18 23:46:42 +08:00
static uint8_t calculatePercentage(const FFMemoryStorage* storage)
{
if(storage->error.length != 0 || storage->bytesTotal == 0)
return 0;
else
return (uint8_t) (((long double) storage->bytesUsed / (long double) storage->bytesTotal) * 100.0);
}
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-20 15:12:24 +02:00
if(storage->error.length > 0)
2021-04-15 18:59:26 +02:00
{
2022-09-20 15:12:24 +02:00
ffPrintError(instance, name, 0, moduleArgs, "%s", storage->error.chars);
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-10-18 23:46:42 +08:00
uint8_t percentage = calculatePercentage(storage);
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);
if (storage->bytesTotal == 0)
puts("Disabled");
else
{
FFstrbuf str;
ffStrbufInit(&str);
if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
{
ffAppendPercentBar(instance, &str, percentage, 0, 5, 8);
ffStrbufAppendC(&str, ' ');
}
ffStrbufAppendF(&str, "%s / %s", usedPretty.chars, totalPretty.chars);
if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT)
ffStrbufAppendF(&str, " (%u%%)", percentage);
ffStrbufPutTo(&str, stdout);
ffStrbufDestroy(&str);
}
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-10-18 23:46:42 +08:00
{FF_FORMAT_ARG_TYPE_UINT8, &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)
{
2022-10-18 23:46:42 +08:00
printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, ffDetectMemory());
2022-09-08 14:51:31 +02:00
}
void ffPrintSwap(FFinstance* instance)
{
2022-10-18 23:46:42 +08:00
printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, ffDetectSwap());
2022-09-08 14:51:31 +02:00
}