From 034f444fca184443bc41274263bcc1b85ce98130 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 9 Oct 2022 16:51:53 +0800 Subject: [PATCH] Terminal / Shell: fix name printing --- src/detection/terminalshell/terminalshell.h | 2 ++ .../terminalshell/terminalshell_linux.c | 8 +++++ .../terminalshell/terminalshell_windows.cpp | 29 ++++++++++++------- src/modules/shell.c | 2 +- src/modules/terminal.c | 2 +- 5 files changed, 30 insertions(+), 13 deletions(-) diff --git a/src/detection/terminalshell/terminalshell.h b/src/detection/terminalshell/terminalshell.h index 2791856ac..8783e3410 100644 --- a/src/detection/terminalshell/terminalshell.h +++ b/src/detection/terminalshell/terminalshell.h @@ -10,10 +10,12 @@ typedef struct FFTerminalShellResult FFstrbuf shellProcessName; FFstrbuf shellExe; const char* shellExeName; //pointer to a char in shellExe + FFstrbuf shellPrettyName; FFstrbuf shellVersion; FFstrbuf terminalProcessName; FFstrbuf terminalExe; + FFstrbuf terminalPrettyName; const char* terminalExeName; //pointer to a char in terminalExe FFstrbuf userShellExe; diff --git a/src/detection/terminalshell/terminalshell_linux.c b/src/detection/terminalshell/terminalshell_linux.c index 5d9a392f6..3adca984f 100644 --- a/src/detection/terminalshell/terminalshell_linux.c +++ b/src/detection/terminalshell/terminalshell_linux.c @@ -322,6 +322,14 @@ const FFTerminalShellResult* else ffStrbufSet(&result.userShellVersion, &result.shellVersion); + // https://github.com/LinusDierheimer/fastfetch/discussions/280#discussioncomment-3831734 + ffStrbufInitS(&result.shellPrettyName, result.shellExeName); + + if(strncmp(result.terminalExeName, result.terminalProcessName.chars, result.terminalProcessName.length) == 0) // if exeName starts with processName, print it. Otherwise print processName + ffStrbufInitS(&result.terminalPrettyName, result.terminalExeName); + else + ffStrbufInitCopy(&result.terminalPrettyName, &result.terminalProcessName); + pthread_mutex_unlock(&mutex); return &result; } diff --git a/src/detection/terminalshell/terminalshell_windows.cpp b/src/detection/terminalshell/terminalshell_windows.cpp index 9ef0d9c6b..d4deea9b3 100644 --- a/src/detection/terminalshell/terminalshell_windows.cpp +++ b/src/detection/terminalshell/terminalshell_windows.cpp @@ -66,15 +66,16 @@ static uint32_t getShellInfo(FFTerminalShellResult* result, uint32_t pid) return 0; result->shellExeName = result->shellExe.chars + ffStrbufLastIndexC(&result->shellExe, '\\') + 1; - if(ffStrbufEndsWithIgnCaseS(&result->shellProcessName, ".exe")) - ffStrbufSubstrBefore(&result->shellProcessName, result->shellProcessName.length - 4); + ffStrbufSet(&result->shellPrettyName, &result->shellProcessName); + if(ffStrbufEndsWithIgnCaseS(&result->shellPrettyName, ".exe")) + ffStrbufSubstrBefore(&result->shellPrettyName, result->shellPrettyName.length - 4); - if(ffStrbufIgnCaseCompS(&result->shellProcessName, "pwsh") == 0) + if(ffStrbufIgnCaseCompS(&result->shellPrettyName, "pwsh") == 0) { - ffStrbufSetS(&result->shellProcessName, "PowerShell"); + ffStrbufSetS(&result->shellPrettyName, "PowerShell"); getShellVersion(&result->shellExe, &result->shellVersion); } - else if(ffStrbufIgnCaseCompS(&result->shellProcessName, "powershell") == 0) + else if(ffStrbufIgnCaseCompS(&result->shellPrettyName, "powershell") == 0) ffStrbufSetS(&result->shellProcessName, "Windows PowerShell"); return ppid; @@ -88,13 +89,14 @@ static uint32_t getTerminalInfo(FFTerminalShellResult* result, uint32_t pid) return 0; result->terminalExeName = result->terminalExe.chars + ffStrbufLastIndexC(&result->terminalExe, '\\'); - if(ffStrbufEndsWithIgnCaseS(&result->terminalProcessName, ".exe")) + ffStrbufSet(&result->terminalPrettyName, &result->terminalProcessName); + if(ffStrbufEndsWithIgnCaseS(&result->terminalPrettyName, ".exe")) result->terminalProcessName.length -= 4; - if(ffStrbufIgnCaseCompS(&result->terminalProcessName, "WindowsTerminal")) - ffStrbufSetS(&result->terminalProcessName, "Windows Terminal"); - else if(ffStrbufIgnCaseCompS(&result->terminalProcessName, "conhost")) - ffStrbufSetS(&result->terminalProcessName, "Console Window Host"); + if(ffStrbufIgnCaseCompS(&result->terminalPrettyName, "WindowsTerminal")) + ffStrbufSetS(&result->terminalPrettyName, "Windows Terminal"); + else if(ffStrbufIgnCaseCompS(&result->terminalPrettyName, "conhost")) + ffStrbufSetS(&result->terminalPrettyName, "Console Window Host"); return ppid; } @@ -108,7 +110,7 @@ const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) { #ifdef __MSYS__ // This is hacky. - // When running inside MSYS2, the real Windows parent process doesn't exist and we must find it in Linux way ( /proc/self/xxx ) + // When running inside of MSYS2, the real Windows parent process doesn't exist and we must find it in Linux way ( /proc/self/xxx ) // When running outside of MSYS2, /proc/self/xxx doesn't exist and we must find it in Windows way if(getenv("MSYSTEM")) return ffDetectTerminalShellPosix(instance); @@ -123,11 +125,13 @@ const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) ffStrbufInit(&result.shellProcessName); ffStrbufInitA(&result.shellExe, 128); result.shellExeName = result.shellExe.chars; + ffStrbufInit(&result.shellPrettyName); ffStrbufInit(&result.shellVersion); ffStrbufInit(&result.terminalProcessName); ffStrbufInitA(&result.terminalExe, 128); result.terminalExeName = result.terminalExe.chars; + ffStrbufInit(&result.terminalPrettyName); ffStrbufInit(&result.userShellExe); result.userShellExeName = result.userShellExe.chars; @@ -141,6 +145,9 @@ const FFTerminalShellResult* ffDetectTerminalShell(const FFinstance* instance) if(ppid == 0) return &result; + // TODO: handle nested shells + // TODO: handle running without shells ( dblclick exe in Windows Explorer ) + ppid = getTerminalInfo(&result, ppid); if(ppid == 0) return &result; diff --git a/src/modules/shell.c b/src/modules/shell.c index b9486bc2c..607f5ec69 100644 --- a/src/modules/shell.c +++ b/src/modules/shell.c @@ -18,7 +18,7 @@ void ffPrintShell(FFinstance* instance) if(instance->config.shell.outputFormat.length == 0) { ffPrintLogoAndKey(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shell.key); - ffStrbufWriteTo(&result->shellProcessName, stdout); + ffStrbufWriteTo(&result->shellPrettyName, stdout); if(result->shellVersion.length > 0) { diff --git a/src/modules/terminal.c b/src/modules/terminal.c index c89afbb78..469ece23b 100644 --- a/src/modules/terminal.c +++ b/src/modules/terminal.c @@ -20,7 +20,7 @@ void ffPrintTerminal(FFinstance* instance) if(instance->config.terminal.outputFormat.length == 0) { ffPrintLogoAndKey(instance, FF_TERMINAL_MODULE_NAME, 0, &instance->config.terminal.key); - ffStrbufPutTo(&result->terminalProcessName, stdout); + ffStrbufPutTo(&result->terminalPrettyName, stdout); } else {