From 6413656ceae41fe80d66e1cf764a7821f229d7a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 27 Jun 2024 10:27:17 +0800 Subject: [PATCH] Logo: rename `--logo-separate` to `--logo-position` --- doc/json_schema.json | 13 +++++++++---- src/data/help.json | 13 ++++++++----- src/logo/image/image.c | 24 ++++++++++++------------ src/logo/logo.c | 4 ++-- src/options/logo.c | 37 ++++++++++++++++++++++++++++++------- src/options/logo.h | 9 ++++++++- 6 files changed, 69 insertions(+), 31 deletions(-) diff --git a/doc/json_schema.json b/doc/json_schema.json index ebd4ce8b4..648ef6bfa 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -219,10 +219,15 @@ "description": "If true, regenerate image logo cache", "default": false }, - "separate": { - "type": "boolean", - "description": "If true, print modules at bottom of the logo", - "default": false + "position": { + "type": "string", + "description": "Set the position of the logo should be displayed", + "enum": [ + "left", + "top", + "right" + ], + "default": "left" }, "chafa": { "type": "object", diff --git a/src/data/help.json b/src/data/help.json index eec3b5795..8c4683e15 100644 --- a/src/data/help.json +++ b/src/data/help.json @@ -257,12 +257,15 @@ } }, { - "long": "logo-separate", - "desc": "If true, print modules at bottom of the logo", + "long": "logo-position", + "desc": "Set the position of the logo should be displayed", "arg": { - "type": "bool", - "optional": true, - "default": false + "type": "enum", + "enum": { + "left": "Left", + "top": "Top", + "right": "Right" + } } }, { diff --git a/src/logo/image/image.c b/src/logo/image/image.c index 3daf8cfde..495dbccc9 100644 --- a/src/logo/image/image.c +++ b/src/logo/image/image.c @@ -72,14 +72,14 @@ static bool printImageIterm(bool printError) if (!options->width || !options->height) { - if (!options->separate) + if (options->position == FF_LOGO_POSITION_LEFT) { ffStrbufAppendF(&buf, "\e[2J\e[3J\e[%u;%uH", (unsigned) options->paddingTop, (unsigned) options->paddingLeft ); } - else + else if (options->position == FF_LOGO_POSITION_TOP) { ffStrbufAppendNC(&buf, options->paddingTop, '\n'); ffStrbufAppendNC(&buf, options->paddingLeft, ' '); @@ -90,7 +90,7 @@ static bool printImageIterm(bool printError) ffStrbufAppendF(&buf, "\e]1337;File=inline=1:%s\a", base64.chars); ffWriteFDBuffer(FFUnixFD2NativeFD(STDOUT_FILENO), &buf); - if (!options->separate) + if (options->position == FF_LOGO_POSITION_LEFT) { uint16_t X = 0, Y = 0; const char* error = ffGetTerminalResponse("\e[6n", "\e[%hu;%huR", &Y, &X); @@ -103,7 +103,7 @@ static bool printImageIterm(bool printError) instance.state.logoHeight = Y; fputs("\e[H", stdout); } - else + else if (options->position == FF_LOGO_POSITION_TOP) { instance.state.logoWidth = instance.state.logoHeight = 0; ffPrintCharTimes('\n', options->paddingRight); @@ -120,13 +120,13 @@ static bool printImageIterm(bool printError) base64.chars ); - if (!options->separate) + if (options->position == FF_LOGO_POSITION_LEFT) { instance.state.logoWidth = options->width + options->paddingLeft + options->paddingRight; instance.state.logoHeight = options->paddingTop + options->height; ffStrbufAppendF(&buf, "\e[%uA", (unsigned) instance.state.logoHeight); } - else + else if (options->position == FF_LOGO_POSITION_TOP) { instance.state.logoWidth = instance.state.logoHeight = 0; ffStrbufAppendNC(&buf, options->paddingRight, '\n'); @@ -152,7 +152,7 @@ static bool printImageKittyDirect(bool printError) if (!options->width || !options->height) { - if (!options->separate) + if (options->position == FF_LOGO_POSITION_LEFT) { // We must clear the entre screen to make sure that terminal buffer won't scroll up printf("\e[2J\e[3J\e[%u;%uH", @@ -160,7 +160,7 @@ static bool printImageKittyDirect(bool printError) (unsigned) options->paddingLeft ); } - else + else if (options->position == FF_LOGO_POSITION_TOP) { ffPrintCharTimes('\n', options->paddingTop); ffPrintCharTimes(' ', options->paddingLeft); @@ -171,7 +171,7 @@ static bool printImageKittyDirect(bool printError) else printf("\e_Ga=T,f=100,t=f;%s\e\\", base64.chars); fflush(stdout); - if (!options->separate) + if (options->position == FF_LOGO_POSITION_LEFT) { uint16_t X = 0, Y = 0; const char* error = ffGetTerminalResponse("\e[6n", "\e[%hu;%huR", &Y, &X); @@ -184,7 +184,7 @@ static bool printImageKittyDirect(bool printError) instance.state.logoHeight = Y; fputs("\e[H", stdout); } - else + else if (options->position == FF_LOGO_POSITION_TOP) { instance.state.logoWidth = instance.state.logoHeight = 0; ffPrintCharTimes('\n', options->paddingRight); @@ -200,13 +200,13 @@ static bool printImageKittyDirect(bool printError) (unsigned) options->height, base64.chars ); - if (!options->separate) + if (options->position == FF_LOGO_POSITION_LEFT) { instance.state.logoWidth = options->width + options->paddingLeft + options->paddingRight; instance.state.logoHeight = options->paddingTop + options->height; printf("\e[%uA", (unsigned) instance.state.logoHeight); } - else + else if (options->position == FF_LOGO_POSITION_TOP) { instance.state.logoWidth = instance.state.logoHeight = 0; ffPrintCharTimes('\n', options->paddingRight); diff --git a/src/logo/logo.c b/src/logo/logo.c index edada9d3e..96386336f 100644 --- a/src/logo/logo.c +++ b/src/logo/logo.c @@ -199,7 +199,7 @@ void ffLogoPrintChars(const char* data, bool doColorReplacement) if(!instance.config.display.pipe) ffStrbufAppendS(&result, FASTFETCH_TEXT_MODIFIER_RESET); - if(!options->separate) + if(options->position == FF_LOGO_POSITION_LEFT) { //Happens if the last line is the longest if(currentlineLength > instance.state.logoWidth) @@ -210,7 +210,7 @@ void ffLogoPrintChars(const char* data, bool doColorReplacement) //Go to the leftmost position and go up the height ffStrbufAppendF(&result, "\e[1G\e[%uA", instance.state.logoHeight); } - else + else if (options->position == FF_LOGO_POSITION_TOP) { instance.state.logoWidth = instance.state.logoHeight = 0; ffStrbufAppendNC(&result, options->paddingRight, '\n'); diff --git a/src/options/logo.c b/src/options/logo.c index 9c0373040..da526e2b6 100644 --- a/src/options/logo.c +++ b/src/options/logo.c @@ -17,7 +17,7 @@ void ffOptionsInitLogo(FFOptionsLogo* options) options->printRemaining = true; options->preserveAspectRatio = false; options->recache = false; - options->separate = false; + options->position = FF_LOGO_POSITION_LEFT; options->chafaFgOnly = false; ffStrbufInitStatic(&options->chafaSymbols, "block+border+space-wide-inverted"); // Chafa default @@ -110,8 +110,15 @@ logoType: options->preserveAspectRatio = ffOptionParseBoolean(value); else if(ffStrEqualsIgnCase(subKey, "recache")) options->recache = ffOptionParseBoolean(value); - else if(ffStrEqualsIgnCase(subKey, "separate")) - options->separate = ffOptionParseBoolean(value); + else if(ffStrEqualsIgnCase(subKey, "position")) + { + options->position = (FFLogoPosition) ffOptionParseEnum(key, value, (FFKeyValuePair[]) { + { "left", FF_LOGO_POSITION_LEFT }, + { "right", FF_LOGO_POSITION_RIGHT }, + { "top", FF_LOGO_POSITION_TOP }, + {}, + }); + } else return false; } @@ -355,9 +362,19 @@ const char* ffOptionsParseLogoJsonConfig(FFOptionsLogo* options, yyjson_val* roo options->recache = yyjson_get_bool(val); continue; } - else if (ffStrEqualsIgnCase(key, "separate")) + else if (ffStrEqualsIgnCase(key, "position")) { - options->separate = yyjson_get_bool(val); + int value; + const char* error = ffJsonConfigParseEnum(val, &value, (FFKeyValuePair[]) { + { "left", FF_LOGO_POSITION_LEFT }, + { "top", FF_LOGO_POSITION_TOP }, + { "right", FF_LOGO_POSITION_RIGHT }, + {}, + }); + + if (error) return error; + options->chafaCanvasMode = (uint32_t) value; + continue; } else if (ffStrEqualsIgnCase(key, "chafa")) @@ -528,8 +545,14 @@ void ffOptionsGenerateLogoJsonConfig(FFOptionsLogo* options, yyjson_mut_doc* doc if (options->recache != defaultOptions.recache) yyjson_mut_obj_add_bool(doc, obj, "recache", options->recache); - if (options->separate != defaultOptions.separate) - yyjson_mut_obj_add_bool(doc, obj, "separate", options->separate); + if (options->position != defaultOptions.position) + { + yyjson_mut_obj_add_str(doc, obj, "position", ((const char* []) { + "left", + "top", + "right", + })[options->position]); + } { yyjson_mut_val* chafa = yyjson_mut_obj(doc); diff --git a/src/options/logo.h b/src/options/logo.h index b93d0ef8a..ab22843b9 100644 --- a/src/options/logo.h +++ b/src/options/logo.h @@ -23,10 +23,18 @@ typedef enum FFLogoType FF_LOGO_TYPE_NONE, //--logo none } FFLogoType; +typedef enum FFLogoPosition +{ + FF_LOGO_POSITION_LEFT, + FF_LOGO_POSITION_TOP, + FF_LOGO_POSITION_RIGHT, +} FFLogoPosition; + typedef struct FFOptionsLogo { FFstrbuf source; FFLogoType type; + FFLogoPosition position; FFstrbuf colors[FASTFETCH_LOGO_MAX_COLORS]; uint32_t width; uint32_t height; @@ -36,7 +44,6 @@ typedef struct FFOptionsLogo bool printRemaining; bool preserveAspectRatio; bool recache; - bool separate; bool chafaFgOnly; FFstrbuf chafaSymbols;