diff --git a/CMakeLists.txt b/CMakeLists.txt index 479c7cd1d..69171c5d1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -296,6 +296,7 @@ set(LIBFASTFETCH_SRC src/modules/separator.c src/modules/shell.c src/modules/sound.c + src/modules/swap.c src/modules/media.c src/modules/terminal.c src/modules/terminalfont.c diff --git a/src/detection/memory/memory.h b/src/detection/memory/memory.h index ee34c8038..bc140e689 100644 --- a/src/detection/memory/memory.h +++ b/src/detection/memory/memory.h @@ -3,8 +3,14 @@ #ifndef FF_INCLUDED_detection_memory_memory #define FF_INCLUDED_detection_memory_memory -#include "detection/storage.h" +#include "fastfetch.h" -void ffDetectMemory(FFMemoryStorage* result); +typedef struct FFMemoryResult +{ + uint64_t bytesUsed; + uint64_t bytesTotal; +} FFMemoryResult; + +const char* ffDetectMemory(FFMemoryResult* result); #endif diff --git a/src/detection/memory/memory_apple.c b/src/detection/memory/memory_apple.c index 37b7addb7..752c588c4 100644 --- a/src/detection/memory/memory_apple.c +++ b/src/detection/memory/memory_apple.c @@ -3,31 +3,25 @@ #include #include #include +#include -void ffDetectMemory(FFMemoryStorage* ram) +const char* ffDetectMemory(FFMemoryResult* ram) { size_t length = sizeof(ram->bytesTotal); if (sysctl((int[]){ CTL_HW, HW_MEMSIZE }, 2, &ram->bytesTotal, &length, NULL, 0)) - { - ffStrbufAppendS(&ram->error, "Failed to read hw.memsize"); - return; - } + return "Failed to read hw.memsize"; uint32_t pagesize; length = sizeof(pagesize); if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pagesize, &length, NULL, 0)) - { - ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize"); - return; - } + return "Failed to read hw.pagesize"; mach_msg_type_number_t count = HOST_VM_INFO64_COUNT; vm_statistics64_data_t vmstat; if(host_statistics64(mach_host_self(), HOST_VM_INFO64, (host_info64_t) (&vmstat), &count) != KERN_SUCCESS) - { - ffStrbufAppendS(&ram->error, "Failed to read host_statistics64"); - return; - } + return "Failed to read host_statistics64"; ram->bytesUsed = ((uint64_t) vmstat.active_count + vmstat.wire_count) * pagesize; + + return NULL; } diff --git a/src/detection/memory/memory_bsd.c b/src/detection/memory/memory_bsd.c index 52d19a0c9..05c74172f 100644 --- a/src/detection/memory/memory_bsd.c +++ b/src/detection/memory/memory_bsd.c @@ -1,24 +1,21 @@ #include "memory.h" #include "common/sysctl.h" -void ffDetectMemory(FFMemoryStorage* ram) +const char* ffDetectMemory(FFMemoryResult* ram) { - uint32_t pageSize = (uint32_t) ffSysctlGetInt("hw.pagesize", 0); - if(pageSize == 0) - { - ffStrbufAppendS(&ram->error, "Failed to read hw.pagesize"); - return; - } + uint32_t pageSize; + uint64_t length = sizeof(pageSize); + if (sysctl((int[]){ CTL_HW, HW_PAGESIZE }, 2, &pageSize, &length, NULL, 0)) + return "Failed to read hw.pagesize"; ram->bytesTotal = (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_page_count", 0) * pageSize; if(ram->bytesTotal == 0) - { - ffStrbufAppendS(&ram->error, "Failed to read vm.stats.vm.v_page_count"); - return; - } + return "Failed to read vm.stats.vm.v_page_count"; ram->bytesUsed = ram->bytesTotal - (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_free_count", 0) * pageSize - (uint64_t) ffSysctlGetInt64("vm.stats.vm.v_inactive_count", 0) * pageSize ; + + return NULL; } diff --git a/src/detection/memory/memory_linux.c b/src/detection/memory/memory_linux.c index 339dda4fe..60187185f 100644 --- a/src/detection/memory/memory_linux.c +++ b/src/detection/memory/memory_linux.c @@ -1,18 +1,16 @@ #include "memory.h" +#include "util/mallocHelper.h" #include #include -void ffDetectMemory(FFMemoryStorage* ram) +const char* ffDetectMemory(FFMemoryResult* ram) { FILE* meminfo = fopen("/proc/meminfo", "r"); if(meminfo == NULL) - { - ffStrbufAppendS(&ram->error, "Failed to open /proc/meminfo"); - return; - } + return "Failed to open /proc/meminfo"; - char* line = NULL; + char* FF_AUTO_FREE line = NULL; size_t len = 0; uint32_t memTotal = 0, @@ -32,14 +30,13 @@ void ffDetectMemory(FFMemoryStorage* ram) sscanf(line, "SReclaimable: %u", &sReclaimable); } - if(line != NULL) - free(line); - fclose(meminfo); ram->bytesTotal = memTotal * (uint64_t) 1024; if(ram->bytesTotal == 0) - ffStrbufAppendS(&ram->error, "Failed to read MemTotal"); + return "Failed to read MemTotal"; else ram->bytesUsed = (memTotal + shmem - memFree - buffers - cached - sReclaimable) * (uint64_t) 1024; + + return NULL; } diff --git a/src/detection/memory/memory_windows.c b/src/detection/memory/memory_windows.c index 838b2534e..666d79727 100644 --- a/src/detection/memory/memory_windows.c +++ b/src/detection/memory/memory_windows.c @@ -2,16 +2,15 @@ #include -void ffDetectMemory(FFMemoryStorage* ram) +const char* ffDetectMemory(FFMemoryResult* ram) { MEMORYSTATUSEX statex = { .dwLength = sizeof(statex), }; if (!GlobalMemoryStatusEx(&statex)) - { - ffStrbufAppendS(&ram->error, "GlobalMemoryStatusEx() failed"); - return; - } + return "GlobalMemoryStatusEx() failed"; + ram->bytesTotal = statex.ullTotalPhys; ram->bytesUsed = statex.ullTotalPhys - statex.ullAvailPhys; + return NULL; } diff --git a/src/detection/storage.h b/src/detection/storage.h deleted file mode 100644 index c8cf7ed0d..000000000 --- a/src/detection/storage.h +++ /dev/null @@ -1,15 +0,0 @@ -#pragma once - -#ifndef FF_INCLUDED_detection_storage -#define FF_INCLUDED_detection_storage - -#include "fastfetch.h" - -typedef struct FFMemoryStorage -{ - FFstrbuf error; - uint64_t bytesUsed; - uint64_t bytesTotal; -} FFMemoryStorage; - -#endif diff --git a/src/detection/swap/swap.h b/src/detection/swap/swap.h index 246b2bfa3..18e74c7ba 100644 --- a/src/detection/swap/swap.h +++ b/src/detection/swap/swap.h @@ -3,8 +3,14 @@ #ifndef FF_INCLUDED_detection_swap_swap #define FF_INCLUDED_detection_swap_swap -#include "detection/storage.h" +#include "fastfetch.h" -void ffDetectSwap(FFMemoryStorage* result); +typedef struct FFSwapResult +{ + uint64_t bytesUsed; + uint64_t bytesTotal; +} FFSwapResult; + +const char* ffDetectSwap(FFSwapResult* result); #endif diff --git a/src/detection/swap/swap_apple.c b/src/detection/swap/swap_apple.c index 15200da15..07bb477ee 100644 --- a/src/detection/swap/swap_apple.c +++ b/src/detection/swap/swap_apple.c @@ -3,16 +3,14 @@ #include #include -void ffDetectSwap(FFMemoryStorage* swap) +const char* ffDetectSwap(FFSwapResult* swap) { struct xsw_usage xsw; size_t size = sizeof(xsw); if(sysctl((int[]){ CTL_VM, VM_SWAPUSAGE }, 2, &xsw, &size, NULL, 0) != 0) - { - ffStrbufAppendS(&swap->error, "Failed to read vm.swapusage"); - return; - } + return "Failed to read vm.swapusage"; swap->bytesTotal = xsw.xsu_total; swap->bytesUsed = xsw.xsu_used; + return NULL; } diff --git a/src/detection/swap/swap_bsd.c b/src/detection/swap/swap_bsd.c index b4c5fa7c7..82ec66779 100644 --- a/src/detection/swap/swap_bsd.c +++ b/src/detection/swap/swap_bsd.c @@ -1,8 +1,9 @@ #include "swap.h" #include "common/sysctl.h" -void ffDetectSwap(FFMemoryStorage* swap) +const char* ffDetectSwap(FFSwapResult* swap) { swap->bytesTotal = (uint64_t)ffSysctlGetInt64("vm.swap_total", 0); swap->bytesUsed = (uint64_t)ffSysctlGetInt64("vm.swap_reserved", 0); + return NULL; } diff --git a/src/detection/swap/swap_linux.c b/src/detection/swap/swap_linux.c index ee623c0c4..4ed2e6475 100644 --- a/src/detection/swap/swap_linux.c +++ b/src/detection/swap/swap_linux.c @@ -2,12 +2,14 @@ #include -void ffDetectSwap(FFMemoryStorage* swap) +const char* ffDetectSwap(FFSwapResult* swap) { struct sysinfo info; if(sysinfo(&info) != 0) - ffStrbufAppendS(&swap->error, "sysinfo() failed"); + return "sysinfo() failed"; swap->bytesTotal = info.totalswap * (uint64_t) info.mem_unit; swap->bytesUsed = (info.totalswap - info.freeswap) * (uint64_t) info.mem_unit; + + return NULL; } diff --git a/src/detection/swap/swap_windows.c b/src/detection/swap/swap_windows.c index f367b0d59..0755d52f2 100644 --- a/src/detection/swap/swap_windows.c +++ b/src/detection/swap/swap_windows.c @@ -5,7 +5,7 @@ #include #include -void ffDetectSwap(FFMemoryStorage* swap) +const char* ffDetectSwap(FFSwapResult* swap) { SYSTEM_INFO sysInfo; GetNativeSystemInfo(&sysInfo); @@ -18,16 +18,10 @@ void ffDetectSwap(FFMemoryStorage* swap) if(status == STATUS_INFO_LENGTH_MISMATCH) { if(!(pstart = (SYSTEM_PAGEFILE_INFORMATION*)realloc(pstart, size))) - { - ffStrbufAppendF(&swap->error, "realloc(pstart, %lu) failed", size); - return; - } + return "realloc(pstart, size) failed"; } else if(!NT_SUCCESS(status)) - { - ffStrbufAppendF(&swap->error, "NtQuerySystemInformation(SystemPagefileInformation, %lu) failed", size); - return; - } + return "NtQuerySystemInformation(SystemPagefileInformation, size) failed"; break; } swap->bytesUsed = (uint64_t)pstart->TotalUsed * sysInfo.dwPageSize; diff --git a/src/fastfetch.h b/src/fastfetch.h index 0d25d31a8..cd43cdb01 100644 --- a/src/fastfetch.h +++ b/src/fastfetch.h @@ -326,7 +326,7 @@ void ffPrintCPU(FFinstance* instance); void ffPrintCPUUsage(FFinstance* instance); void ffPrintGPU(FFinstance* instance); void ffPrintMemory(FFinstance* instance); -void ffPrintSwap(FFinstance* instance); //Also in modules/memory.c +void ffPrintSwap(FFinstance* instance); void ffPrintDisk(FFinstance* instance); void ffPrintBattery(FFinstance* instance); void ffPrintPowerAdapter(FFinstance* instance); diff --git a/src/modules/memory.c b/src/modules/memory.c index 05d7c33cf..3c82b1a7b 100644 --- a/src/modules/memory.c +++ b/src/modules/memory.c @@ -3,47 +3,41 @@ #include "common/parsing.h" #include "common/bar.h" #include "detection/memory/memory.h" -#include "detection/swap/swap.h" #define FF_MEMORY_MODULE_NAME "Memory" -#define FF_SWAP_MODULE_NAME "Swap" - #define FF_MEMORY_NUM_FORMAT_ARGS 3 -static uint8_t calculatePercentage(const FFMemoryStorage* storage) +void ffPrintMemory(FFinstance* instance) { - if(storage->error.length != 0 || storage->bytesTotal == 0) - return 0; - else - return (uint8_t) (((long double) storage->bytesUsed / (long double) storage->bytesTotal) * 100.0); -} + FFMemoryResult storage; + const char* error = ffDetectMemory(&storage); -static void printMemory(FFinstance* instance, const char* name, const FFModuleArgs* moduleArgs, const FFMemoryStorage* storage) -{ - if(storage->error.length > 0) + if(error) { - ffPrintError(instance, name, 0, moduleArgs, "%s", storage->error.chars); + ffPrintError(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory, "%s", error); return; } - FFstrbuf usedPretty; + FF_STRBUF_AUTO_DESTROY usedPretty; ffStrbufInit(&usedPretty); - ffParseSize(storage->bytesUsed, instance->config.binaryPrefixType, &usedPretty); + ffParseSize(storage.bytesUsed, instance->config.binaryPrefixType, &usedPretty); - FFstrbuf totalPretty; + FF_STRBUF_AUTO_DESTROY totalPretty; ffStrbufInit(&totalPretty); - ffParseSize(storage->bytesTotal, instance->config.binaryPrefixType, &totalPretty); + ffParseSize(storage.bytesTotal, instance->config.binaryPrefixType, &totalPretty); - uint8_t percentage = calculatePercentage(storage); + uint8_t percentage = storage.bytesTotal == 0 + ? 0 + : (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0); - if(moduleArgs->outputFormat.length == 0) + if(instance->config.memory.outputFormat.length == 0) { - ffPrintLogoAndKey(instance, name, 0, &moduleArgs->key); - if (storage->bytesTotal == 0) + ffPrintLogoAndKey(instance, FF_MEMORY_MODULE_NAME, 0, &instance->config.memory.key); + if (storage.bytesTotal == 0) puts("Disabled"); else { - FFstrbuf str; + FF_STRBUF_AUTO_DESTROY str; ffStrbufInit(&str); if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) @@ -60,36 +54,14 @@ static void printMemory(FFinstance* instance, const char* name, const FFModuleAr ffStrbufTrimRight(&str, ' '); ffStrbufPutTo(&str, stdout); - ffStrbufDestroy(&str); } } else { - ffPrintFormat(instance, name, 0, moduleArgs, FF_MEMORY_NUM_FORMAT_ARGS, (FFformatarg[]){ + 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}, }); } - - ffStrbufDestroy(&usedPretty); - ffStrbufDestroy(&totalPretty); -} - -void ffPrintMemory(FFinstance* instance) -{ - FFMemoryStorage result; - ffStrbufInit(&result.error); - ffDetectMemory(&result); - printMemory(instance, FF_MEMORY_MODULE_NAME, &instance->config.memory, &result); - ffStrbufDestroy(&result.error); -} - -void ffPrintSwap(FFinstance* instance) -{ - FFMemoryStorage result; - ffStrbufInit(&result.error); - ffDetectSwap(&result); - printMemory(instance, FF_SWAP_MODULE_NAME, &instance->config.swap, &result); - ffStrbufDestroy(&result.error); } diff --git a/src/modules/swap.c b/src/modules/swap.c new file mode 100644 index 000000000..1f6175b15 --- /dev/null +++ b/src/modules/swap.c @@ -0,0 +1,67 @@ +#include "fastfetch.h" +#include "common/printing.h" +#include "common/parsing.h" +#include "common/bar.h" +#include "detection/swap/swap.h" + +#define FF_SWAP_MODULE_NAME "Swap" +#define FF_SWAP_NUM_FORMAT_ARGS 3 + +void ffPrintSwap(FFinstance* instance) +{ + FFSwapResult storage; + const char* error = ffDetectSwap(&storage); + + if(error) + { + ffPrintError(instance, FF_SWAP_MODULE_NAME, 0, &instance->config.swap, "%s", error); + return; + } + + FF_STRBUF_AUTO_DESTROY usedPretty; + ffStrbufInit(&usedPretty); + ffParseSize(storage.bytesUsed, instance->config.binaryPrefixType, &usedPretty); + + FF_STRBUF_AUTO_DESTROY totalPretty; + ffStrbufInit(&totalPretty); + ffParseSize(storage.bytesTotal, instance->config.binaryPrefixType, &totalPretty); + + uint8_t percentage = storage.bytesTotal == 0 + ? 0 + : (uint8_t) (((long double) storage.bytesUsed / (long double) storage.bytesTotal) * 100.0); + + if(instance->config.swap.outputFormat.length == 0) + { + ffPrintLogoAndKey(instance, FF_SWAP_MODULE_NAME, 0, &instance->config.swap.key); + if (storage.bytesTotal == 0) + puts("Disabled"); + else + { + FF_STRBUF_AUTO_DESTROY str; + ffStrbufInit(&str); + + if(instance->config.percentType & FF_PERCENTAGE_TYPE_BAR_BIT) + { + ffAppendPercentBar(instance, &str, percentage, 0, 5, 8); + ffStrbufAppendC(&str, ' '); + } + + if(!(instance->config.percentType & FF_PERCENTAGE_TYPE_HIDE_OTHERS_BIT)) + ffStrbufAppendF(&str, "%s / %s ", usedPretty.chars, totalPretty.chars); + + if(instance->config.percentType & FF_PERCENTAGE_TYPE_NUM_BIT) + ffAppendPercentNum(instance, &str, (uint8_t) percentage, 50, 80, str.length > 0); + + ffStrbufTrimRight(&str, ' '); + ffStrbufPutTo(&str, stdout); + } + } + else + { + ffPrintFormat(instance, FF_SWAP_MODULE_NAME, 0, &instance->config.swap, FF_SWAP_NUM_FORMAT_ARGS, (FFformatarg[]){ + {FF_FORMAT_ARG_TYPE_STRBUF, &usedPretty}, + {FF_FORMAT_ARG_TYPE_STRBUF, &totalPretty}, + {FF_FORMAT_ARG_TYPE_UINT8, &percentage}, + }); + } +}