mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 02:42:09 +02:00
Separate memory detection and printing logic
This commit is contained in:
@@ -167,6 +167,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/detection/host/host.c
|
||||
src/detection/os/os.c
|
||||
src/detection/cpu/cpu.c
|
||||
src/detection/memory/memory.c
|
||||
src/detection/displayserver/displayServer.c
|
||||
src/detection/displayserver/wayland.c
|
||||
src/detection/displayserver/xcb.c
|
||||
@@ -218,18 +219,21 @@ if(APPLE)
|
||||
src/detection/host/host_apple.c
|
||||
src/detection/os/os_apple.c
|
||||
src/detection/cpu/cpu_apple.c
|
||||
src/detection/memory/memory_apple.c
|
||||
)
|
||||
elseif(ANDROID)
|
||||
list(APPEND LIBFASTFETCH_SRC
|
||||
src/detection/host/host_android.c
|
||||
src/detection/os/os_android.c
|
||||
src/detection/cpu/cpu_linux.c
|
||||
src/detection/memory/memory_linux.c
|
||||
)
|
||||
elseif(UNIX)
|
||||
list(APPEND LIBFASTFETCH_SRC
|
||||
src/detection/host/host_linux.c
|
||||
src/detection/os/os_linux.c
|
||||
src/detection/cpu/cpu_linux.c
|
||||
src/detection/memory/memory_linux.c
|
||||
)
|
||||
else()
|
||||
message(FATAL_ERROR "Unsupported platform")
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
+8
-41
@@ -1,73 +1,40 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "common/parsing.h"
|
||||
|
||||
#include <stdlib.h>
|
||||
#include "detection/memory/memory.h"
|
||||
|
||||
#define FF_MEMORY_MODULE_NAME "Memory"
|
||||
#define FF_MEMORY_NUM_FORMAT_ARGS 3
|
||||
|
||||
// Impl inspired by: https://github.com/sam-barr/paleofetch/blob/b7c58a52c0de39b53c9b5f417889a5886d324bfa/paleofetch.c#L544
|
||||
void ffPrintMemory(FFinstance* instance)
|
||||
{
|
||||
FILE* meminfo = fopen("/proc/meminfo", "r");
|
||||
if(meminfo == NULL) {
|
||||
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "fopen(\"""/proc/meminfo\", \"r\") == NULL");
|
||||
return;
|
||||
}
|
||||
const FFMemoryResult* memory = ffDetectMemory();
|
||||
|
||||
char* line = NULL;
|
||||
size_t len = 0;
|
||||
|
||||
uint32_t total = 0,
|
||||
shared = 0,
|
||||
memfree = 0,
|
||||
buffers = 0,
|
||||
cached = 0,
|
||||
reclaimable = 0;
|
||||
|
||||
while (getline(&line, &len, meminfo) != -1) {
|
||||
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);
|
||||
}
|
||||
|
||||
if(line != NULL)
|
||||
free(line);
|
||||
|
||||
fclose(meminfo);
|
||||
|
||||
uint32_t used = total + shared - memfree - buffers - cached - reclaimable;
|
||||
uint8_t percentage = (uint8_t) ((used / (double) total) * 100);
|
||||
|
||||
if(used == 0 && total == 0)
|
||||
if(memory->bytesUsed == 0 && memory->bytesTotal == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "/proc/meminfo could't be parsed");
|
||||
ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "Failed to detect memory");
|
||||
return;
|
||||
}
|
||||
|
||||
FFstrbuf usedPretty;
|
||||
ffStrbufInit(&usedPretty);
|
||||
ffParseSize(used * (uint64_t) 1024, instance->config.binaryPrefixType, &usedPretty);
|
||||
ffParseSize(memory->bytesUsed, instance->config.binaryPrefixType, &usedPretty);
|
||||
|
||||
FFstrbuf totalPretty;
|
||||
ffStrbufInit(&totalPretty);
|
||||
ffParseSize(total * (uint64_t) 1024, instance->config.binaryPrefixType, &totalPretty);
|
||||
ffParseSize(memory->bytesTotal, instance->config.binaryPrefixType, &totalPretty);
|
||||
|
||||
if(instance->config.memory.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory.key);
|
||||
printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, percentage);
|
||||
printf("%s / %s (%u%%)\n", usedPretty.chars, totalPretty.chars, memory->percentage);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &percentage},
|
||||
{FF_FORMAT_ARG_TYPE_UINT8, &memory->percentage},
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user