Terminal: add fast path of version and font detection for kitty

Ref: https://github.com/fastfetch-cli/fastfetch/discussions/1030#discussioncomment-9845233
This commit is contained in:
李通洲
2024-06-22 15:42:56 +08:00
parent 77fbfd3fbd
commit b500e9c09b
2 changed files with 53 additions and 44 deletions
+42 -21
View File
@@ -1,4 +1,5 @@
#include "terminalfont.h"
#include "common/io/io.h"
#include "common/properties.h"
#include "common/processing.h"
#include "detection/terminalshell/terminalshell.h"
@@ -256,8 +257,6 @@ static void detectFromWindowsTerminal(const FFstrbuf* terminalExe, FFTerminalFon
}
}
#endif //defined(_WIN32) || defined(__linux__)
FF_MAYBE_UNUSED static bool detectKitty(const FFstrbuf* exe, FFTerminalFontResult* result)
@@ -265,33 +264,55 @@ FF_MAYBE_UNUSED static bool detectKitty(const FFstrbuf* exe, FFTerminalFontResul
FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
if(!ffProcessAppendStdOut(&buf, (char* const[]){
exe->chars,
"+kitten",
"query-terminal",
NULL,
}))
char fontHex[64] = "", sizeHex[64] = "";
// https://github.com/fastfetch-cli/fastfetch/discussions/1030#discussioncomment-9845233
if (ffGetTerminalResponse(
"\eP+q6b697474792d71756572792d666f6e745f66616d696c79;6b697474792d71756572792d666f6e745f73697a65\e\\", // kitty-query-font_family;kitty-query-font_size
"\eP1+r%*[^=]=%64[^\e]\e\\\eP1+r%*[^=]=%64[^\e]\e\\", fontHex, sizeHex) == NULL && *fontHex && *sizeHex)
{
ffParsePropLines(buf.chars, "font_family: ", &fontName);
ffParsePropLines(buf.chars, "font_size: ", &fontSize);
// decode hex string
for (const char* p = fontHex; p[0] && p[1]; p += 2)
{
unsigned value;
if (sscanf(p, "%2x", &value))
ffStrbufAppendC(&fontName, (char) value);
}
for (const char* p = sizeHex; p[0] && p[1]; p += 2)
{
unsigned value;
if (sscanf(p, "%2x", &value))
ffStrbufAppendC(&fontSize, (char) value);
}
}
else
{
FFpropquery fontQuery[] = {
{"font_family ", &fontName},
{"font_size ", &fontSize},
};
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
if(!ffProcessAppendStdOut(&buf, (char* const[]){
exe->chars,
"+kitten",
"query-terminal",
NULL,
}))
{
ffParsePropLines(buf.chars, "font_family: ", &fontName);
ffParsePropLines(buf.chars, "font_size: ", &fontSize);
}
else
{
FFpropquery fontQuery[] = {
{"font_family ", &fontName},
{"font_size ", &fontSize},
};
ffParsePropFileConfigValues("kitty/kitty.conf", 2, fontQuery);
ffParsePropFileConfigValues("kitty/kitty.conf", 2, fontQuery);
if(fontName.length == 0)
ffStrbufSetS(&fontName, "monospace");
if(fontSize.length == 0)
ffStrbufSetS(&fontSize, "11.0");
if(fontName.length == 0)
ffStrbufSetS(&fontName, "monospace");
if(fontSize.length == 0)
ffStrbufSetS(&fontSize, "11.0");
}
}
ffFontInitValues(&result->font, fontName.chars, fontSize.chars);
return true;
+11 -23
View File
@@ -480,33 +480,21 @@ static bool getTerminalVersionZellij(FFstrbuf* exe, FFstrbuf* version)
#ifndef _WIN32
static bool getTerminalVersionKitty(FFstrbuf* exe, FFstrbuf* version)
{
#if defined(__linux__) || defined(__FreeBSD__)
// kitty is written in python. `kitty --version` can be expensive
char buffer[1024] = {};
if (
#ifdef __linux__
ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib64/kitty/kitty/constants.py", sizeof(buffer) - 1, buffer) ||
ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib/kitty/kitty/constants.py", sizeof(buffer) - 1, buffer)
#else
ffReadFileData(_PATH_LOCALBASE "/share/kitty/kitty/constants.py", sizeof(buffer) - 1, buffer)
#endif
)
char versionHex[64] = "";
// https://github.com/fastfetch-cli/fastfetch/discussions/1030#discussioncomment-9845233
if (ffGetTerminalResponse(
"\eP+q6b697474792d71756572792d76657273696f6e\e\\", // kitty-query-version
"\eP1+r%*[^=]=%64[^\e]\e\\\\", versionHex) == NULL && *versionHex)
{
// Starts from version 0.17.0
// https://github.com/kovidgoyal/kitty/blob/master/kitty/constants.py#L25
const char* p = memmem(buffer, sizeof(buffer) - 1, "version: Version = Version(", strlen("version: Version = Version("));
if (p)
// decode hex string
for (const char* p = versionHex; p[0] && p[1]; p += 2)
{
p += strlen("version: Version = Version(");
int major, minor, patch;
if (sscanf(p, "%d,%d,%d", &major, &minor, &patch) == 3)
{
ffStrbufSetF(version, "%d.%d.%d", major, minor, patch);
return true;
}
unsigned value;
if (sscanf(p, "%2x", &value))
ffStrbufAppendC(version, (char) value);
}
return true;
}
#endif
//kitty 0.21.2 created by Kovid Goyal
return getExeVersionGeneral(exe, version);