From ef2a5110fe75834efd0b67aee4fd3001790161b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Tue, 10 Jan 2023 19:24:59 +0800 Subject: [PATCH] TerminalShell: detect terminal version when available --- CHANGELOG.md | 2 ++ src/detection/terminalshell/terminalshell.c | 26 +++++++++++++++++++ src/detection/terminalshell/terminalshell.h | 1 + .../terminalshell/terminalshell_linux.c | 5 ++++ .../terminalshell/terminalshell_windows.cpp | 5 ++++ src/modules/terminal.c | 6 ++++- 6 files changed, 44 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 44402b68a..4809204dc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ Notable Changes: * fastfetch no longer creates a sample config file silently. Use `--gen-config` to generate one. * fastfetch now search for user config file in the order of `fastfetch --list-config-paths` +* Unknown disks are hidden by default. Features: * `--logo-padding-top` option (@CarterLi, #372) @@ -14,6 +15,7 @@ Features: * Add `Brightness` module support * Add `Battery` module support for FreeBSD * Add `--disk-show-unknown` option for Disk module +* Detect terminal version when available Logos: * Raspbian (@IamNoRobot, #373) diff --git a/src/detection/terminalshell/terminalshell.c b/src/detection/terminalshell/terminalshell.c index a99c498e4..3a80ce153 100644 --- a/src/detection/terminalshell/terminalshell.c +++ b/src/detection/terminalshell/terminalshell.c @@ -155,3 +155,29 @@ bool fftsGetShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* version) ok = false; return ok; } + +bool fftsGetTerminalVersion(FFstrbuf* processName, FF_UNUSED_PARAM FFstrbuf* exe, FFstrbuf* version) +{ + const char* termProgramVersion = getenv("TERM_PROGRAM_VERSION"); + if(termProgramVersion) + { + const char* termProgram = getenv("TERM_PROGRAM"); + if(termProgram) + { + if(ffStrbufStartsWithIgnCaseS(processName, termProgram) || // processName ends with `.exe` on Windows + (strcmp(termProgram, "vscode") == 0 && ffStrbufStartsWithIgnCaseS(processName, "code")) + ) { + ffStrbufSetS(version, termProgramVersion); + return true; + } + } + } + + #ifdef _WIN32 + + return getFileVersion(exe->chars, version); + + #endif + + return false; +} diff --git a/src/detection/terminalshell/terminalshell.h b/src/detection/terminalshell/terminalshell.h index 8783e3410..a2a1f4200 100644 --- a/src/detection/terminalshell/terminalshell.h +++ b/src/detection/terminalshell/terminalshell.h @@ -17,6 +17,7 @@ typedef struct FFTerminalShellResult FFstrbuf terminalExe; FFstrbuf terminalPrettyName; const char* terminalExeName; //pointer to a char in terminalExe + FFstrbuf terminalVersion; FFstrbuf userShellExe; const char* userShellExeName; //pointer to a char in userShellExe diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index 80c0d93d1..fb3ad0a76 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -310,6 +310,8 @@ static void getShellVersion(FFstrbuf* exe, const char* exeName, FFstrbuf* versio getShellVersionGeneric(exe, exeName, version); } +bool fftsGetTerminalVersion(FFstrbuf* processName, FFstrbuf* exe, FFstrbuf* version); + const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) { FF_UNUSED(instance); @@ -378,6 +380,9 @@ const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) else ffStrbufInitCopy(&result.terminalPrettyName, &result.terminalProcessName); + ffStrbufInit(&result.terminalVersion); + fftsGetTerminalVersion(&result.terminalProcessName, &result.terminalExe, &result.terminalVersion); + ffThreadMutexUnlock(&mutex); return &result; } diff --git a/src/detection/terminalshell/terminalshell_windows.cpp b/src/detection/terminalshell/terminalshell_windows.cpp index cb2da1ca4..e4355666e 100644 --- a/src/detection/terminalshell/terminalshell_windows.cpp +++ b/src/detection/terminalshell/terminalshell_windows.cpp @@ -263,6 +263,8 @@ static void getTerminalFromEnv(FFTerminalShellResult* result) } } +extern "C" bool fftsGetTerminalVersion(FFstrbuf* processName, FFstrbuf* exe, FFstrbuf* version); + const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) { FF_UNUSED(instance); @@ -302,6 +304,9 @@ const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) if(result.terminalProcessName.length == 0) getTerminalFromEnv(&result); + ffStrbufInit(&result.terminalVersion); + fftsGetTerminalVersion(&result.terminalProcessName, &result.terminalExe, &result.terminalVersion); + exit: ffThreadMutexUnlock(&mutex); return &result; diff --git a/src/modules/terminal.c b/src/modules/terminal.c index 469ece23b..28e8d14a5 100644 --- a/src/modules/terminal.c +++ b/src/modules/terminal.c @@ -20,7 +20,11 @@ void ffPrintTerminal(FFinstance* instance) if(instance->config.terminal.outputFormat.length == 0) { ffPrintLogoAndKey(instance, FF_TERMINAL_MODULE_NAME, 0, &instance->config.terminal.key); - ffStrbufPutTo(&result->terminalPrettyName, stdout); + + if(result->terminalVersion.length) + printf("%s %s\n", result->terminalPrettyName.chars, result->terminalVersion.chars); + else + ffStrbufPutTo(&result->terminalPrettyName, stdout); } else {