From f1ebcd7c31e9aad8605bcb585bb30ec79c4cbcb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 18 Jul 2023 21:09:37 +0800 Subject: [PATCH] Logo: add `kitty-direct` protocol --- CHANGELOG.md | 5 +++++ doc/json_schema.json | 1 + src/logo/image/image.c | 16 +++++++--------- src/logo/option.c | 7 +++++++ src/logo/option.h | 1 + 5 files changed, 21 insertions(+), 9 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 930f7a4fb..756fcf836 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,11 @@ Bugfixes: * Fix false errors when built without libnm support (Wifi, Linux) * Properly detect CPU on POWER (CPU, Linux) +Logo: +* Change the special handling of `kitty` protocol with `.png` image file to a new image protocol `kitty-direct`. This is the fastest image protocol because fastfetch doesn't need to pre-encode the image to base64 or something and the image content doesn't need to be transmitted via tty. Note: + 1. Although konsole was said to support `kitty` image protocol, it doesn't support `kitty-direct` + 2. wezterm support more image formats other than `.png` (tested with `.jpg` and `.webp`) + # 1.12.2 Features: diff --git a/doc/json_schema.json b/doc/json_schema.json index 025437c64..9e4238b81 100644 --- a/doc/json_schema.json +++ b/doc/json_schema.json @@ -52,6 +52,7 @@ "data-raw", "sixel", "kitty", + "kitty-direct", "iterm", "chafa", "raw", diff --git a/src/logo/image/image.c b/src/logo/image/image.c index 79dad505b..c2379bbb0 100644 --- a/src/logo/image/image.c +++ b/src/logo/image/image.c @@ -68,9 +68,14 @@ static bool printImageIterm(void) static bool printImageKittyDirect(void) { + if (!instance.config.logo.width || !instance.config.logo.height) + { + fputs("Logo: `kitty-direct` protocol only works when both `--logo-width` and `--logo-height` being specified\n", stderr); + return false; + } ffPrintCharTimes(' ', instance.config.logo.paddingLeft); ffPrintCharTimes('\n', instance.config.logo.paddingTop); - FFstrbuf base64 = base64Encode(&instance.config.logo.source); + FF_STRBUF_AUTO_DESTROY base64 = base64Encode(&instance.config.logo.source); printf("\033_Ga=T,f=100,t=f,c=%u,r=%u,C=1;%s\033\\\033[9999999D", (unsigned) instance.config.logo.width, (unsigned) instance.config.logo.height, @@ -80,8 +85,6 @@ static bool printImageKittyDirect(void) instance.state.logoWidth = instance.config.logo.width + instance.config.logo.paddingLeft + instance.config.logo.paddingRight; instance.state.logoHeight = instance.config.logo.paddingTop + instance.config.logo.height; - ffStrbufDestroy(&base64); - return true; } @@ -741,12 +744,7 @@ bool ffLogoPrintImageIfExists(FFLogoType type, bool printError) if(type == FF_LOGO_TYPE_IMAGE_ITERM) return printImageIterm(); - if( - type == FF_LOGO_TYPE_IMAGE_KITTY && - ffStrbufEndsWithIgnCaseS(&instance.config.logo.source, ".png") && - instance.config.logo.width && - instance.config.logo.height - ) + if(type == FF_LOGO_TYPE_IMAGE_KITTY_DIRECT) return printImageKittyDirect(); #if !defined(FF_HAVE_CHAFA) diff --git a/src/logo/option.c b/src/logo/option.c index 425dd056f..a3b59b806 100644 --- a/src/logo/option.c +++ b/src/logo/option.c @@ -67,6 +67,7 @@ logoType: { "data-raw", FF_LOGO_TYPE_DATA_RAW }, { "sixel", FF_LOGO_TYPE_IMAGE_SIXEL }, { "kitty", FF_LOGO_TYPE_IMAGE_KITTY }, + { "kitty-direct", FF_LOGO_TYPE_IMAGE_KITTY_DIRECT }, { "iterm", FF_LOGO_TYPE_IMAGE_ITERM }, { "chafa", FF_LOGO_TYPE_IMAGE_CHAFA }, { "raw", FF_LOGO_TYPE_IMAGE_RAW }, @@ -155,6 +156,11 @@ logoType: ffOptionParseString(key, value, &options->source); options->type = FF_LOGO_TYPE_IMAGE_KITTY; } + else if(strcasecmp(key, "--kitty-direct") == 0) + { + ffOptionParseString(key, value, &options->source); + options->type = FF_LOGO_TYPE_IMAGE_KITTY_DIRECT; + } else if(strcasecmp(key, "--iterm") == 0) { ffOptionParseString(key, value, &options->source); @@ -248,6 +254,7 @@ const char* ffParseLogoJsonConfig(void) { "data-raw", FF_LOGO_TYPE_DATA_RAW }, { "sixel", FF_LOGO_TYPE_IMAGE_SIXEL }, { "kitty", FF_LOGO_TYPE_IMAGE_KITTY }, + { "kitty-direct", FF_LOGO_TYPE_IMAGE_KITTY_DIRECT }, { "iterm", FF_LOGO_TYPE_IMAGE_ITERM }, { "chafa", FF_LOGO_TYPE_IMAGE_CHAFA }, { "raw", FF_LOGO_TYPE_IMAGE_RAW }, diff --git a/src/logo/option.h b/src/logo/option.h index 9039b4b57..d31ca53f4 100644 --- a/src/logo/option.h +++ b/src/logo/option.h @@ -15,6 +15,7 @@ typedef enum FFLogoType FF_LOGO_TYPE_DATA_RAW, //text data, printed as is FF_LOGO_TYPE_IMAGE_SIXEL, //image file, printed as sixel codes. FF_LOGO_TYPE_IMAGE_KITTY, //image file, printed as kitty graphics protocol + FF_LOGO_TYPE_IMAGE_KITTY_DIRECT, //image file, tell the terminal emulator to read image data from the specified file (Supported by kitty and wezterm) FF_LOGO_TYPE_IMAGE_ITERM, //image file, printed as iterm graphics protocol FF_LOGO_TYPE_IMAGE_CHAFA, //image file, printed as ascii art using libchafa FF_LOGO_TYPE_IMAGE_RAW, //image file, printed as raw binary string