Files
fastfetch/src/modules/memory.c
T

52 lines
1.6 KiB
C
Raw Normal View History

2021-02-18 22:25:36 +01:00
#include "fastfetch.h"
// Impl by: https://github.com/sam-barr/paleofetch/blob/b7c58a52c0de39b53c9b5f417889a5886d324bfa/paleofetch.c#L544
2021-02-27 18:30:05 +01:00
void ffPrintMemory(FFinstance* instance)
2021-02-18 22:25:36 +01:00
{
2021-02-19 10:07:00 +01:00
FILE* meminfo = fopen("/proc/meminfo", "r");
2021-02-18 22:25:36 +01:00
if(meminfo == NULL) {
2021-02-27 18:30:05 +01:00
ffPrintError(instance, "Memory", "fopen(\"/proc/meminfo\", \"r\") == NULL");
2021-02-18 22:25:36 +01:00
return;
}
2021-02-19 10:07:00 +01:00
char* line = NULL;
2021-02-18 22:25:36 +01:00
size_t len;
uint32_t total, shared, memfree, buffers, cached, reclaimable;
while (getline(&line, &len, meminfo) != -1) {
2021-02-19 10:07:00 +01:00
sscanf(line, "MemTotal: %u", &total);
sscanf(line, "Shmem: %u", &shared);
sscanf(line, "MemFree: %u", &memfree);
sscanf(line, "Buffers: %u", &buffers);
sscanf(line, "Cached: %u", &cached);
sscanf(line, "SReclaimable: %u", &reclaimable);
2021-02-18 22:25:36 +01:00
}
free(line);
fclose(meminfo);
uint32_t used_mem = (total + shared - memfree - buffers - cached - reclaimable) / 1024;
uint32_t total_mem = total / 1024;
uint8_t percentage = (uint8_t) ((used_mem / (double) total_mem) * 100);
2021-02-27 18:30:05 +01:00
ffPrintLogoAndKey(instance, "Memory");
2021-03-19 20:57:07 +01:00
2021-03-23 19:48:52 +01:00
if(instance->config.memoryFormat.length == 0)
2021-03-19 20:57:07 +01:00
{
printf("%uMiB / %uMiB (%u%%)\n", used_mem, total_mem, percentage);
}
else
{
FF_STRBUF_CREATE(memory);
2021-03-19 20:57:07 +01:00
ffParseFormatString(&memory, &instance->config.memoryFormat, 3,
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT, &used_mem},
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT, &total_mem},
(FFformatarg){FF_FORMAT_ARG_TYPE_UINT8, &percentage}
);
2021-03-22 20:02:32 +01:00
ffStrbufPutTo(&memory, stdout);
2021-03-19 20:57:07 +01:00
ffStrbufDestroy(&memory);
}
2021-02-18 22:25:36 +01:00
}