mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-06 13:14:21 +01:00
Separate memory detection and printing logic
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
#include "memory.h"
|
||||
#include "detection/internal.h"
|
||||
|
||||
void ffDetectMemoryImpl(FFMemoryResult* memory);
|
||||
|
||||
const FFMemoryResult* ffDetectMemory()
|
||||
{
|
||||
FF_DETECTION_INTERNAL_GUARD(FFMemoryResult,
|
||||
ffDetectMemoryImpl(&result);
|
||||
|
||||
if(result.bytesTotal > 0)
|
||||
result.percentage = (uint8_t) ((result.bytesUsed / (long double) result.bytesTotal) * 100.0);
|
||||
else
|
||||
result.percentage = 0;
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#ifndef FF_INCLUDED_detection_memory_memory
|
||||
#define FF_INCLUDED_detection_memory_memory
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
typedef struct FFMemoryResult
|
||||
{
|
||||
uint64_t bytesUsed;
|
||||
uint64_t bytesTotal;
|
||||
uint8_t percentage;
|
||||
} FFMemoryResult;
|
||||
|
||||
const FFMemoryResult* ffDetectMemory();
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "memory.h"
|
||||
|
||||
void ffDetectMemoryImpl(FFMemoryResult* memory)
|
||||
{
|
||||
memory->bytesUsed = 0;
|
||||
memory->bytesTotal = 0;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "memory.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
void ffDetectMemoryImpl(FFMemoryResult* memory)
|
||||
{
|
||||
memory->bytesUsed = 0;
|
||||
memory->bytesTotal = 0;
|
||||
|
||||
FILE* meminfo = fopen("/proc/meminfo", "r");
|
||||
if(meminfo == NULL)
|
||||
return;
|
||||
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
|
||||
uint32_t memTotal = 0,
|
||||
shmem = 0,
|
||||
memFree = 0,
|
||||
buffers = 0,
|
||||
cached = 0,
|
||||
sReclaimable = 0;
|
||||
|
||||
while (getline(&line, &len, meminfo) != EOF)
|
||||
{
|
||||
sscanf(line, "MemTotal: %u", &memTotal);
|
||||
sscanf(line, "Shmem: %u", &shmem);
|
||||
sscanf(line, "MemFree: %u", &memFree);
|
||||
sscanf(line, "Buffers: %u", &buffers);
|
||||
sscanf(line, "Cached: %u", &cached);
|
||||
sscanf(line, "SReclaimable: %u", &sReclaimable);
|
||||
}
|
||||
|
||||
if(line != NULL)
|
||||
free(line);
|
||||
|
||||
fclose(meminfo);
|
||||
|
||||
memory->bytesTotal = memTotal * (uint64_t) 1024;
|
||||
memory->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024;
|
||||
}
|
||||
Reference in New Issue
Block a user