mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-31 10:02:49 +01:00
PhysicalMemory (Linux): adds option showEmptySlots to show empty memory slots
Fixes #2222
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "common/option.h"
|
||||
#include "common/percent.h"
|
||||
|
||||
typedef struct FFPhysicalMemoryOptions
|
||||
{
|
||||
FFModuleArgs moduleArgs;
|
||||
bool showEmptySlots;
|
||||
} FFPhysicalMemoryOptions;
|
||||
|
||||
static_assert(sizeof(FFPhysicalMemoryOptions) <= FF_OPTION_MAX_SIZE, "FFPhysicalMemoryOptions size exceeds maximum allowed size");
|
||||
|
||||
@@ -1,8 +1,6 @@
|
||||
#include "common/printing.h"
|
||||
#include "common/jsonconfig.h"
|
||||
#include "common/percent.h"
|
||||
#include "common/size.h"
|
||||
#include "common/stringUtils.h"
|
||||
#include "detection/physicalmemory/physicalmemory.h"
|
||||
#include "modules/physicalmemory/physicalmemory.h"
|
||||
|
||||
@@ -25,35 +23,61 @@ bool ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options)
|
||||
return false;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY prettySize = ffStrbufCreate();
|
||||
|
||||
uint32_t i = 0;
|
||||
FF_LIST_AUTO_DESTROY filtered = ffListCreate(sizeof(FFPhysicalMemoryResult*));
|
||||
FF_LIST_FOR_EACH(FFPhysicalMemoryResult, device, result)
|
||||
{
|
||||
++i;
|
||||
if (!options->showEmptySlots && !device->installed)
|
||||
continue;
|
||||
|
||||
*(FFPhysicalMemoryResult**) ffListAdd(&filtered) = device;
|
||||
}
|
||||
|
||||
if (filtered.length == 0)
|
||||
{
|
||||
ffPrintError(FF_PHYSICALMEMORY_DISPLAY_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No installed physical memory detected");
|
||||
return false;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY prettySize = ffStrbufCreate();
|
||||
|
||||
for (uint32_t i = 0; i < filtered.length; ++i)
|
||||
{
|
||||
FFPhysicalMemoryResult* device = *FF_LIST_GET(FFPhysicalMemoryResult*, filtered, i);
|
||||
ffStrbufClear(&prettySize);
|
||||
ffSizeAppendNum(device->size, &prettySize);
|
||||
if (device->installed)
|
||||
ffSizeAppendNum(device->size, &prettySize);
|
||||
|
||||
if (options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(FF_PHYSICALMEMORY_DISPLAY_NAME, result.length == 1 ? 0 : (uint8_t) i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
||||
ffPrintLogoAndKey(FF_PHYSICALMEMORY_DISPLAY_NAME, filtered.length == 1 ? 0 : (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT);
|
||||
|
||||
fputs(prettySize.chars, stdout);
|
||||
fputs(" - ", stdout);
|
||||
ffStrbufWriteTo(&device->type, stdout);
|
||||
if (device->maxSpeed > 0)
|
||||
printf("-%u", device->maxSpeed);
|
||||
if (device->runningSpeed > 0 && device->runningSpeed != device->maxSpeed)
|
||||
printf(" @ %u MT/s", device->runningSpeed);
|
||||
if (device->vendor.length > 0)
|
||||
printf(" (%s)", device->vendor.chars);
|
||||
if (device->ecc)
|
||||
fputs(" - ECC", stdout);
|
||||
if (device->installed)
|
||||
{
|
||||
fputs(prettySize.chars, stdout);
|
||||
fputs(" - ", stdout);
|
||||
ffStrbufWriteTo(&device->type, stdout);
|
||||
if (device->maxSpeed > 0)
|
||||
printf("-%u", device->maxSpeed);
|
||||
if (device->runningSpeed > 0 && device->runningSpeed != device->maxSpeed)
|
||||
printf(" @ %u MT/s", device->runningSpeed);
|
||||
if (device->vendor.length > 0)
|
||||
printf(" (%s)", device->vendor.chars);
|
||||
if (device->ecc)
|
||||
fputs(" - ECC", stdout);
|
||||
}
|
||||
else
|
||||
{
|
||||
fputs("Empty", stdout);
|
||||
if (device->formFactor.length > 0)
|
||||
printf(" - %s", device->formFactor.chars);
|
||||
if (device->locator.length > 0)
|
||||
printf(" (%s)", device->locator.chars);
|
||||
}
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
FF_PRINT_FORMAT_CHECKED(FF_PHYSICALMEMORY_DISPLAY_NAME, (uint8_t) i, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
||||
FF_PRINT_FORMAT_CHECKED(FF_PHYSICALMEMORY_DISPLAY_NAME, (uint8_t) (i + 1), &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
|
||||
FF_FORMAT_ARG(device->size, "bytes"),
|
||||
FF_FORMAT_ARG(prettySize, "size"),
|
||||
FF_FORMAT_ARG(device->maxSpeed, "max-speed"),
|
||||
@@ -65,6 +89,7 @@ bool ffPrintPhysicalMemory(FFPhysicalMemoryOptions* options)
|
||||
FF_FORMAT_ARG(device->serial, "serial"),
|
||||
FF_FORMAT_ARG(device->partNumber, "part-number"),
|
||||
FF_FORMAT_ARG(device->ecc, "is-ecc-enabled"),
|
||||
FF_FORMAT_ARG(device->installed, "is-installed"),
|
||||
}));
|
||||
}
|
||||
}
|
||||
@@ -91,6 +116,12 @@ void ffParsePhysicalMemoryJsonObject(FFPhysicalMemoryOptions* options, yyjson_va
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options->moduleArgs))
|
||||
continue;
|
||||
|
||||
if (unsafe_yyjson_equals_str(key, "showEmptySlots"))
|
||||
{
|
||||
options->showEmptySlots = yyjson_get_bool(val);
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPrintError(FF_PHYSICALMEMORY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "Unknown JSON key %s", unsafe_yyjson_get_str(key));
|
||||
}
|
||||
}
|
||||
@@ -98,6 +129,7 @@ void ffParsePhysicalMemoryJsonObject(FFPhysicalMemoryOptions* options, yyjson_va
|
||||
void ffGeneratePhysicalMemoryJsonConfig(FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
||||
{
|
||||
ffJsonConfigGenerateModuleArgsConfig(doc, module, &options->moduleArgs);
|
||||
yyjson_mut_obj_add_bool(doc, module, "showEmptySlots", options->showEmptySlots);
|
||||
}
|
||||
|
||||
bool ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
|
||||
@@ -116,6 +148,7 @@ bool ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions*
|
||||
{
|
||||
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
|
||||
yyjson_mut_obj_add_uint(doc, obj, "size", device->size);
|
||||
yyjson_mut_obj_add_bool(doc, obj, "installed", device->installed);
|
||||
yyjson_mut_obj_add_uint(doc, obj, "maxSpeed", device->maxSpeed);
|
||||
yyjson_mut_obj_add_uint(doc, obj, "runningSpeed", device->runningSpeed);
|
||||
yyjson_mut_obj_add_strbuf(doc, obj, "type", &device->type);
|
||||
@@ -143,6 +176,7 @@ bool ffGeneratePhysicalMemoryJsonResult(FF_MAYBE_UNUSED FFPhysicalMemoryOptions*
|
||||
void ffInitPhysicalMemoryOptions(FFPhysicalMemoryOptions* options)
|
||||
{
|
||||
ffOptionInitModuleArg(&options->moduleArgs, "");
|
||||
options->showEmptySlots = false;
|
||||
}
|
||||
|
||||
void ffDestroyPhysicalMemoryOptions(FFPhysicalMemoryOptions* options)
|
||||
@@ -171,5 +205,6 @@ FFModuleBaseInfo ffPhysicalMemoryModuleInfo = {
|
||||
{"Serial number", "serial"},
|
||||
{"Part number", "part-number"},
|
||||
{"True if ECC enabled", "is-ecc-enabled"},
|
||||
{"True if a memory module is installed in the slot", "is-installed"},
|
||||
}))
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user