mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:52:08 +02:00
rewrote shell / terminal detection
This commit is contained in:
+33
-61
@@ -4,81 +4,53 @@
|
||||
#include <ctype.h>
|
||||
|
||||
#define FF_SHELL_MODULE_NAME "Shell"
|
||||
#define FF_SHELL_NUM_FORMAT_ARGS 4
|
||||
#define FF_SHELL_NUM_FORMAT_ARGS 7
|
||||
|
||||
void ffPrintShell(FFinstance* instance)
|
||||
{
|
||||
if(ffPrintFromCache(instance, FF_SHELL_MODULE_NAME, &instance->config.shellKey, &instance->config.shellFormat, FF_SHELL_NUM_FORMAT_ARGS))
|
||||
return;
|
||||
const FFTerminalShellResult* result = ffDetectTerminalShell(instance);
|
||||
|
||||
char* shellPath = getenv("SHELL");
|
||||
if(shellPath == NULL)
|
||||
if(result->shellProcessName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shellKey, &instance->config.shellFormat, FF_SHELL_NUM_FORMAT_ARGS, "getenv(\"SHELL\") == NULL");
|
||||
ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shellKey, &instance->config.shellFormat, FF_SHELL_NUM_FORMAT_ARGS, "Couldn't detect shell");
|
||||
return;
|
||||
}
|
||||
|
||||
char* shellName = strrchr(shellPath, '/');
|
||||
if(shellName != NULL)
|
||||
++shellName;
|
||||
FFstrbuf shell;
|
||||
ffStrbufInit(&shell);
|
||||
ffStrbufAppendS(&shell, result->shellExeName);
|
||||
|
||||
FFstrbuf version;
|
||||
ffStrbufInit(&version);
|
||||
|
||||
FFstrbuf command;
|
||||
ffStrbufInit(&command);
|
||||
ffStrbufAppendS(&command, "printf %s \"$");
|
||||
ffStrbufAppendTransformS(&command, shellName != NULL ? shellName : shellPath, toupper);
|
||||
ffStrbufAppendS(&command, "_VERSION\"");
|
||||
|
||||
ffProcessAppendStdOut(&version, (char* const[]){
|
||||
shellPath,
|
||||
"-c",
|
||||
command.chars,
|
||||
NULL
|
||||
});
|
||||
|
||||
ffStrbufDestroy(&command);
|
||||
|
||||
FFstrbuf versionPretty;
|
||||
ffStrbufInitCopy(&versionPretty, &version);
|
||||
ffStrbufSubstrBeforeFirstC(&versionPretty, '(');
|
||||
ffStrbufRemoveStrings(&versionPretty, 1, "-release");
|
||||
|
||||
char null = '\0';
|
||||
|
||||
if(shellName == NULL)
|
||||
if(result->shellVersion.length > 0)
|
||||
{
|
||||
shellName = shellPath;
|
||||
shellPath = &null;
|
||||
ffStrbufAppendC(&shell, ' ');
|
||||
ffStrbufAppend(&shell, &result->shellVersion);
|
||||
}
|
||||
|
||||
if(instance->config.shellFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shellKey);
|
||||
fputs(result->shellExeName, stdout);
|
||||
|
||||
if(result->shellVersion.length > 0)
|
||||
{
|
||||
putchar(' ');
|
||||
ffStrbufWriteTo(&result->shellVersion, stdout);
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
*(shellName - 1) = '\0';
|
||||
ffPrintFormatString(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shellKey, &instance->config.shellFormat, NULL, FF_SHELL_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->shellProcessName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->shellExe},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, result->shellExeName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->shellVersion},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->userShellExe},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, &result->userShellExeName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->userShellVersion}
|
||||
});
|
||||
}
|
||||
|
||||
FF_STRBUF_CREATE(shell);
|
||||
ffStrbufSetS(&shell, shellName);
|
||||
|
||||
if(versionPretty.length > 0)
|
||||
{
|
||||
ffStrbufAppendC(&shell, ' ');
|
||||
ffStrbufAppend(&shell, &versionPretty);
|
||||
}
|
||||
else if(version.length > 0)
|
||||
{
|
||||
ffStrbufAppendC(&shell, ' ');
|
||||
ffStrbufAppend(&shell, &version);
|
||||
}
|
||||
|
||||
ffPrintAndSaveToCache(instance, FF_SHELL_MODULE_NAME, &instance->config.shellKey, &shell, &instance->config.shellFormat, FF_SHELL_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRING, shellPath},
|
||||
{FF_FORMAT_ARG_TYPE_STRING, shellName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &version},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &versionPretty}
|
||||
});
|
||||
|
||||
ffStrbufDestroy(&shell);
|
||||
ffStrbufDestroy(&versionPretty);
|
||||
ffStrbufDestroy(&version);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user