From 8a789784cc56b495efa078582088c1acce79c0b9 Mon Sep 17 00:00:00 2001 From: Your Name Date: Fri, 15 Dec 2023 18:02:11 +0300 Subject: [PATCH] GetMem fix --- config.h | 20 ++++++++++---------- src/fetch.c | 36 +++++++++++++++++------------------- src/main.c | 2 +- 3 files changed, 28 insertions(+), 30 deletions(-) diff --git a/config.h b/config.h index 218fff4..ec3b354 100644 --- a/config.h +++ b/config.h @@ -13,16 +13,16 @@ typedef struct { #define PC_CHAR "%" static PARAMETR CONFIG[] = { - {" OS ", PrintOs}, - {" KERNEL ", GetKernel}, - {" USER ", GetUser}, - {" UPTIME ", GetUptime}, - {" ARCH ", GetArch}, - {" SHELL ", GetShell}, - {" PKGS ", GetPkg}, - {" RAM ", GetMem}, - {" ", Blank}, - {" COLORS ", PrintColors} + {" OS ", PrintOs}, + {" KERNEL ", GetKernel}, + {" USER ", GetUser}, + {" UPTIME ", GetUptime}, + {" ARCH ", GetArch}, + {" SHELL ", GetShell}, + {" PKGS ", GetPkg}, + {" RAM ", GetMem}, + {" ", Blank}, + {" COLORS ", PrintColors} }; #endif diff --git a/src/fetch.c b/src/fetch.c index aeb43bf..eddd4ef 100644 --- a/src/fetch.c +++ b/src/fetch.c @@ -5,11 +5,6 @@ #include #include #include - -#if defined(__LINUX__) || defined(__ANDROID__) -#include -#endif - #include "fetch.h" #include "config.h" #include "logo.h" @@ -124,17 +119,20 @@ int GetUser(const char *title, const FETCH fetch) { } int GetUptime(const char *title, const FETCH fetch) { - int hours = 0; - int mins = 0; - #ifdef CLOCK struct timespec uptime; clock_gettime(CLOCK, &uptime); - hours = uptime.tv_sec / 3600; - mins = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60); + int days = uptime.tv_sec / 86400; + int hours = uptime.tv_sec / 3600; + int mins = (uptime.tv_sec / 60) - (uptime.tv_sec / 3600 * 60); + + if (days > 0) + printf("%s%s%s%dd %dh %dm", fetch.color, title, fetch.font_color, days, hours, mins); + + else + printf("%s%s%s%dh %dm", fetch.color, title, fetch.font_color, hours, mins); - printf("%s%s%s%dh %dm", fetch.color, title, fetch.font_color, hours, mins); return 0; #endif @@ -182,17 +180,17 @@ int GetPkg(const char *title, const FETCH fetch) { } int GetMem(const char *title, const FETCH fetch) { -#if defined(__LINUX__) || defined(__ANDROID__) - struct sysinfo sinfo; - if (sysinfo(&sinfo) < 0) + unsigned int free = 0, cache = 0, total = 0, unused; + UNUSED(unused); + + FILE *fp = fopen("/proc/meminfo", "r"); + if (fp == NULL) return 1; - printf("%s%s%s%lumb / %lumb", fetch.color, title, fetch.font_color, sinfo.freeram / 1048576, sinfo.totalram / 1048576); + fscanf(fp, "MemTotal: %u kB\nMemFree: %u kB\nMemAvailable: %u kB\nBuffers: %u kB\nCached: %u kB", &total, &free, &unused, &unused, &cache); -#else - printf("%s%s%s0mb / 0mb", fetch.color, title, fetch.font_color); - -#endif + printf("%s%s%s%umb / %umb", fetch.color, title, fetch.font_color, (free + cache) / 1024, total / 1024); + fclose(fp); return 0; } diff --git a/src/main.c b/src/main.c index af920c8..5e2e611 100644 --- a/src/main.c +++ b/src/main.c @@ -23,6 +23,6 @@ int main(void) { for (; i < fetch.logo_size; i++) printf("%s\n", fetch.logo[i]); - printf("\033[0m\n"); + printf("\033[0m"); return 0; }