Terminal / Shell: fix name printing

This commit is contained in:
李通洲
2022-10-09 16:51:53 +08:00
parent 7c98450acc
commit 034f444fca
5 changed files with 30 additions and 13 deletions
@@ -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;
@@ -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;
}
@@ -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;
+1 -1
View File
@@ -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)
{
+1 -1
View File
@@ -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
{