Skip to content

Commit b500e9c

Browse files
committed
Terminal: add fast path of version and font detection for kitty
Ref: #1030 (reply in thread)
1 parent 77fbfd3 commit b500e9c

File tree

2 files changed

+53
-44
lines changed

2 files changed

+53
-44
lines changed

src/detection/terminalfont/terminalfont.c

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include "terminalfont.h"
2+
#include "common/io/io.h"
23
#include "common/properties.h"
34
#include "common/processing.h"
45
#include "detection/terminalshell/terminalshell.h"
@@ -256,42 +257,62 @@ static void detectFromWindowsTerminal(const FFstrbuf* terminalExe, FFTerminalFon
256257
}
257258
}
258259

259-
260-
261260
#endif //defined(_WIN32) || defined(__linux__)
262261

263262
FF_MAYBE_UNUSED static bool detectKitty(const FFstrbuf* exe, FFTerminalFontResult* result)
264263
{
265264
FF_STRBUF_AUTO_DESTROY fontName = ffStrbufCreate();
266265
FF_STRBUF_AUTO_DESTROY fontSize = ffStrbufCreate();
267266

268-
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
269-
if(!ffProcessAppendStdOut(&buf, (char* const[]){
270-
exe->chars,
271-
"+kitten",
272-
"query-terminal",
273-
NULL,
274-
}))
267+
char fontHex[64] = "", sizeHex[64] = "";
268+
// https://github.com/fastfetch-cli/fastfetch/discussions/1030#discussioncomment-9845233
269+
if (ffGetTerminalResponse(
270+
"\eP+q6b697474792d71756572792d666f6e745f66616d696c79;6b697474792d71756572792d666f6e745f73697a65\e\\", // kitty-query-font_family;kitty-query-font_size
271+
"\eP1+r%*[^=]=%64[^\e]\e\\\eP1+r%*[^=]=%64[^\e]\e\\", fontHex, sizeHex) == NULL && *fontHex && *sizeHex)
275272
{
276-
ffParsePropLines(buf.chars, "font_family: ", &fontName);
277-
ffParsePropLines(buf.chars, "font_size: ", &fontSize);
273+
// decode hex string
274+
for (const char* p = fontHex; p[0] && p[1]; p += 2)
275+
{
276+
unsigned value;
277+
if (sscanf(p, "%2x", &value))
278+
ffStrbufAppendC(&fontName, (char) value);
279+
}
280+
for (const char* p = sizeHex; p[0] && p[1]; p += 2)
281+
{
282+
unsigned value;
283+
if (sscanf(p, "%2x", &value))
284+
ffStrbufAppendC(&fontSize, (char) value);
285+
}
278286
}
279287
else
280288
{
281-
FFpropquery fontQuery[] = {
282-
{"font_family ", &fontName},
283-
{"font_size ", &fontSize},
284-
};
289+
FF_STRBUF_AUTO_DESTROY buf = ffStrbufCreate();
290+
if(!ffProcessAppendStdOut(&buf, (char* const[]){
291+
exe->chars,
292+
"+kitten",
293+
"query-terminal",
294+
NULL,
295+
}))
296+
{
297+
ffParsePropLines(buf.chars, "font_family: ", &fontName);
298+
ffParsePropLines(buf.chars, "font_size: ", &fontSize);
299+
}
300+
else
301+
{
302+
FFpropquery fontQuery[] = {
303+
{"font_family ", &fontName},
304+
{"font_size ", &fontSize},
305+
};
285306

286-
ffParsePropFileConfigValues("kitty/kitty.conf", 2, fontQuery);
307+
ffParsePropFileConfigValues("kitty/kitty.conf", 2, fontQuery);
287308

288-
if(fontName.length == 0)
289-
ffStrbufSetS(&fontName, "monospace");
290-
if(fontSize.length == 0)
291-
ffStrbufSetS(&fontSize, "11.0");
309+
if(fontName.length == 0)
310+
ffStrbufSetS(&fontName, "monospace");
311+
if(fontSize.length == 0)
312+
ffStrbufSetS(&fontSize, "11.0");
313+
}
292314
}
293315

294-
295316
ffFontInitValues(&result->font, fontName.chars, fontSize.chars);
296317

297318
return true;

src/detection/terminalshell/terminalshell.c

Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -480,33 +480,21 @@ static bool getTerminalVersionZellij(FFstrbuf* exe, FFstrbuf* version)
480480
#ifndef _WIN32
481481
static bool getTerminalVersionKitty(FFstrbuf* exe, FFstrbuf* version)
482482
{
483-
#if defined(__linux__) || defined(__FreeBSD__)
484-
// kitty is written in python. `kitty --version` can be expensive
485-
char buffer[1024] = {};
486-
if (
487-
#ifdef __linux__
488-
ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib64/kitty/kitty/constants.py", sizeof(buffer) - 1, buffer) ||
489-
ffReadFileData(FASTFETCH_TARGET_DIR_USR "/lib/kitty/kitty/constants.py", sizeof(buffer) - 1, buffer)
490-
#else
491-
ffReadFileData(_PATH_LOCALBASE "/share/kitty/kitty/constants.py", sizeof(buffer) - 1, buffer)
492-
#endif
493-
)
483+
char versionHex[64] = "";
484+
// https://github.com/fastfetch-cli/fastfetch/discussions/1030#discussioncomment-9845233
485+
if (ffGetTerminalResponse(
486+
"\eP+q6b697474792d71756572792d76657273696f6e\e\\", // kitty-query-version
487+
"\eP1+r%*[^=]=%64[^\e]\e\\\\", versionHex) == NULL && *versionHex)
494488
{
495-
// Starts from version 0.17.0
496-
// https://github.com/kovidgoyal/kitty/blob/master/kitty/constants.py#L25
497-
const char* p = memmem(buffer, sizeof(buffer) - 1, "version: Version = Version(", strlen("version: Version = Version("));
498-
if (p)
489+
// decode hex string
490+
for (const char* p = versionHex; p[0] && p[1]; p += 2)
499491
{
500-
p += strlen("version: Version = Version(");
501-
int major, minor, patch;
502-
if (sscanf(p, "%d,%d,%d", &major, &minor, &patch) == 3)
503-
{
504-
ffStrbufSetF(version, "%d.%d.%d", major, minor, patch);
505-
return true;
506-
}
492+
unsigned value;
493+
if (sscanf(p, "%2x", &value))
494+
ffStrbufAppendC(version, (char) value);
507495
}
496+
return true;
508497
}
509-
#endif
510498

511499
//kitty 0.21.2 created by Kovid Goyal
512500
return getExeVersionGeneral(exe, version);

0 commit comments

Comments
 (0)