mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 16:26:39 +01:00
Shell: add JSON config support
This commit is contained in:
@@ -109,6 +109,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
case 'S': {
|
||||
return
|
||||
tryModule(instance, type, module, FF_SEPARATOR_MODULE_NAME, ffParseSeparatorJsonObject) ||
|
||||
tryModule(instance, type, module, FF_SHELL_MODULE_NAME, ffParseShellJsonObject) ||
|
||||
tryModule(instance, type, module, FF_SOUND_MODULE_NAME, ffParseSoundJsonObject) ||
|
||||
tryModule(instance, type, module, FF_SWAP_MODULE_NAME, ffParseSwapJsonObject) ||
|
||||
false;
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include "modules/os/os.h"
|
||||
#include "modules/packages/packages.h"
|
||||
#include "modules/separator/separator.h"
|
||||
#include "modules/shell/shell.h"
|
||||
#include "modules/sound/sound.h"
|
||||
#include "modules/swap/swap.h"
|
||||
#include "modules/title/title.h"
|
||||
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "modules/os/option.h"
|
||||
#include "modules/packages/option.h"
|
||||
#include "modules/separator/option.h"
|
||||
#include "modules/shell/option.h"
|
||||
#include "modules/sound/option.h"
|
||||
#include "modules/swap/option.h"
|
||||
#include "modules/title/option.h"
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/terminalshell/terminalshell.h"
|
||||
|
||||
#define FF_SHELL_MODULE_NAME "Shell"
|
||||
#define FF_SHELL_NUM_FORMAT_ARGS 7
|
||||
|
||||
void ffPrintShell(FFinstance* instance)
|
||||
{
|
||||
const FFTerminalShellResult* result = ffDetectTerminalShell(instance);
|
||||
|
||||
if(result->shellProcessName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shell, "Couldn't detect shell");
|
||||
return;
|
||||
}
|
||||
|
||||
if(instance->config.shell.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shell.key);
|
||||
ffStrbufWriteTo(&result->shellPrettyName, stdout);
|
||||
|
||||
if(result->shellVersion.length > 0)
|
||||
{
|
||||
putchar(' ');
|
||||
ffStrbufWriteTo(&result->shellVersion, stdout);
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_SHELL_MODULE_NAME, 0, &instance->config.shell, 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}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFShellOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
|
||||
bool version;
|
||||
} FFShellOptions;
|
||||
@@ -0,0 +1,99 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/terminalshell/terminalshell.h"
|
||||
#include "modules/shell/shell.h"
|
||||
|
||||
#define FF_SHELL_NUM_FORMAT_ARGS 7
|
||||
|
||||
void ffPrintShell(FFinstance* instance, FFShellOptions* options)
|
||||
{
|
||||
const FFTerminalShellResult* result = ffDetectTerminalShell(instance);
|
||||
|
||||
if(result->shellProcessName.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, "Couldn't detect shell");
|
||||
return;
|
||||
}
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_SHELL_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
ffStrbufWriteTo(&result->shellPrettyName, stdout);
|
||||
|
||||
if(result->shellVersion.length > 0)
|
||||
{
|
||||
putchar(' ');
|
||||
ffStrbufWriteTo(&result->shellVersion, stdout);
|
||||
}
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_SHELL_MODULE_NAME, 0, &options->moduleArgs, 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}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitShellOptions(FFShellOptions* options)
|
||||
{
|
||||
options->moduleName = FF_SHELL_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
|
||||
options->version = true;
|
||||
}
|
||||
|
||||
bool ffParseShellCommandOptions(FFShellOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_SHELL_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
if (strcasecmp(subKey, "version") == 0)
|
||||
{
|
||||
options->version = ffOptionParseBoolean(value);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyShellOptions(FFShellOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseShellJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFShellOptions __attribute__((__cleanup__(ffDestroyShellOptions))) options;
|
||||
ffInitShellOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
if (strcasecmp(key, "version") == 0)
|
||||
{
|
||||
options.version = (bool) json_object_get_boolean(val);
|
||||
continue;
|
||||
}
|
||||
|
||||
ffPrintError(instance, FF_SHELL_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintShell(instance, &options);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_SHELL_MODULE_NAME "Shell"
|
||||
|
||||
void ffPrintShell(FFinstance* instance, FFShellOptions* options);
|
||||
void ffInitShellOptions(FFShellOptions* options);
|
||||
bool ffParseShellCommandOptions(FFShellOptions* options, const char* key, const char* value);
|
||||
void ffDestroyShellOptions(FFShellOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseShellJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
||||
Reference in New Issue
Block a user