From 5fb4b6dc045cb2ff2c186e8d2a78f277888b856e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 22 Feb 2024 16:42:23 +0800 Subject: [PATCH] Display: `--display-compact-type` support `*-with-refresh-rate` Ref: https://github.com/fastfetch-cli/fastfetch/discussions/732 --- doc/json_schema.json | 4 +++- src/data/help.json | 4 +++- src/modules/display/display.c | 42 ++++++++++++++++++++++++++++------- src/modules/display/option.h | 1 + 4 files changed, 41 insertions(+), 10 deletions(-) diff --git a/doc/json_schema.json b/doc/json_schema.json index 64f632868..198ec8e2c 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -1101,7 +1101,9 @@ "enum": [ "none", "original", - "scaled" + "scaled", + "original-with-refresh-rate", + "scaled-with-refresh-rate" ], "description": "Set if all displays should be printed in one line", "default": "none" diff --git a/src/data/help.json b/src/data/help.json index 89c7a24cf..752295eec 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -1033,7 +1033,9 @@ "enum": { "none": "Disable this compact mode", "original": "Print original resolutions", - "scaled": "Print scaled resolutions" + "scaled": "Print scaled resolutions", + "original-with-refresh-rate": "Print original resolutions with refresh rate", + "scaled-with-refresh-rate": "Print scaled resolutions with refresh rate" }, "default": "none" } diff --git a/src/modules/display/display.c b/src/modules/display/display.c index 06e1baac1..35540eea8 100644 --- a/src/modules/display/display.c +++ b/src/modules/display/display.c @@ -35,21 +35,37 @@ void ffPrintDisplay(FFDisplayOptions* options) { ffPrintLogoAndKey(FF_DISPLAY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT); - int index = 0; + FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreate(); FF_LIST_FOR_EACH(FFDisplayResult, result, dsResult->displays) { if (options->compactType & FF_DISPLAY_COMPACT_TYPE_ORIGINAL_BIT) { - if (index++) putchar(' '); - printf("%ix%i", result->width, result->height); + ffStrbufAppendF(&buffer, "%ix%i", result->width, result->height); } - if (options->compactType & FF_DISPLAY_COMPACT_TYPE_SCALED_BIT) + else { - if (index++) putchar(' '); - printf("%ix%i", result->scaledWidth, result->scaledHeight); + ffStrbufAppendF(&buffer, "%ix%i", result->scaledWidth, result->scaledHeight); + } + + if (options->compactType & FF_DISPLAY_COMPACT_TYPE_REFRESH_RATE_BIT) + { + if (result->refreshRate > 0) + { + if (options->preciseRefreshRate) + ffStrbufAppendF(&buffer, " @ %gHz", result->refreshRate); + else + ffStrbufAppendF(&buffer, " @ %iHz", (uint32_t) (result->refreshRate + 0.5)); + } + ffStrbufAppendS(&buffer, ", "); + } + else + { + ffStrbufAppendC(&buffer, ' '); } } - putchar('\n'); + ffStrbufTrimRight(&buffer, ' '); + ffStrbufTrimRight(&buffer, ','); + ffStrbufPutTo(&buffer, stdout); return; } @@ -137,6 +153,8 @@ bool ffParseDisplayCommandOptions(FFDisplayOptions* options, const char* key, co { "none", FF_DISPLAY_COMPACT_TYPE_NONE }, { "original", FF_DISPLAY_COMPACT_TYPE_ORIGINAL_BIT }, { "scaled", FF_DISPLAY_COMPACT_TYPE_SCALED_BIT }, + { "original-with-refresh-rate", FF_DISPLAY_COMPACT_TYPE_ORIGINAL_BIT | FF_DISPLAY_COMPACT_TYPE_REFRESH_RATE_BIT }, + { "scaled-with-refresh-rate", FF_DISPLAY_COMPACT_TYPE_SCALED_BIT | FF_DISPLAY_COMPACT_TYPE_REFRESH_RATE_BIT }, {}, }); return true; @@ -182,6 +200,8 @@ void ffParseDisplayJsonObject(FFDisplayOptions* options, yyjson_val* module) { "none", FF_DISPLAY_COMPACT_TYPE_NONE }, { "original", FF_DISPLAY_COMPACT_TYPE_ORIGINAL_BIT }, { "scaled", FF_DISPLAY_COMPACT_TYPE_SCALED_BIT }, + { "original-with-refresh-rate", FF_DISPLAY_COMPACT_TYPE_ORIGINAL_BIT | FF_DISPLAY_COMPACT_TYPE_REFRESH_RATE_BIT }, + { "scaled-with-refresh-rate", FF_DISPLAY_COMPACT_TYPE_SCALED_BIT | FF_DISPLAY_COMPACT_TYPE_REFRESH_RATE_BIT }, {}, }); if (error) @@ -226,7 +246,7 @@ void ffGenerateDisplayJsonConfig(FFDisplayOptions* options, yyjson_mut_doc* doc, if (options->compactType != defaultOptions.compactType) { - switch (options->compactType) + switch ((int) options->compactType) { case FF_DISPLAY_COMPACT_TYPE_NONE: yyjson_mut_obj_add_str(doc, module, "compactType", "none"); @@ -237,6 +257,12 @@ void ffGenerateDisplayJsonConfig(FFDisplayOptions* options, yyjson_mut_doc* doc, case FF_DISPLAY_COMPACT_TYPE_SCALED_BIT: yyjson_mut_obj_add_str(doc, module, "compactType", "scaled"); break; + case FF_DISPLAY_COMPACT_TYPE_ORIGINAL_BIT | FF_DISPLAY_COMPACT_TYPE_REFRESH_RATE_BIT: + yyjson_mut_obj_add_str(doc, module, "compactType", "original-with-refresh-rate"); + break; + case FF_DISPLAY_COMPACT_TYPE_SCALED_BIT | FF_DISPLAY_COMPACT_TYPE_REFRESH_RATE_BIT: + yyjson_mut_obj_add_str(doc, module, "compactType", "scaled-with-refresh-rate"); + break; } } diff --git a/src/modules/display/option.h b/src/modules/display/option.h index bc84ff886..d6238e1b1 100644 --- a/src/modules/display/option.h +++ b/src/modules/display/option.h @@ -9,6 +9,7 @@ typedef enum FFDisplayCompactType FF_DISPLAY_COMPACT_TYPE_NONE = 0, FF_DISPLAY_COMPACT_TYPE_ORIGINAL_BIT = 1 << 0, FF_DISPLAY_COMPACT_TYPE_SCALED_BIT = 1 << 1, + FF_DISPLAY_COMPACT_TYPE_REFRESH_RATE_BIT = 1 << 2, } FFDisplayCompactType; typedef enum FFDisplayOrder