mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-09-13 10:52:08 +02:00
Command: major code refactoring
ditto; and multi-command support was removed in favor of JSON config support
This commit is contained in:
+1
-1
@@ -271,7 +271,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/cpuUsage.c
|
||||
src/modules/cursor.c
|
||||
src/modules/custom.c
|
||||
src/modules/command.c
|
||||
src/modules/command/command.c
|
||||
src/modules/date.c
|
||||
src/modules/datetime.c
|
||||
src/modules/de.c
|
||||
|
||||
+2
-20
@@ -77,6 +77,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
initModuleArg(&instance->config.board);
|
||||
initModuleArg(&instance->config.brightness);
|
||||
initModuleArg(&instance->config.chassis);
|
||||
ffInitCommandOptions(&instance->config.command);
|
||||
initModuleArg(&instance->config.kernel);
|
||||
initModuleArg(&instance->config.uptime);
|
||||
initModuleArg(&instance->config.processes);
|
||||
@@ -182,18 +183,6 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffStrbufInitA(&instance->config.playerName, 0);
|
||||
|
||||
instance->config.percentType = 1;
|
||||
|
||||
ffStrbufInitS(&instance->config.commandShell,
|
||||
#ifdef _WIN32
|
||||
"cmd"
|
||||
#elif defined(__FreeBSD__)
|
||||
"csh"
|
||||
#else
|
||||
"bash"
|
||||
#endif
|
||||
);
|
||||
ffListInit(&instance->config.commandKeys, sizeof(FFstrbuf));
|
||||
ffListInit(&instance->config.commandTexts, sizeof(FFstrbuf));
|
||||
}
|
||||
|
||||
void ffInitInstance(FFinstance* instance)
|
||||
@@ -335,6 +324,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
destroyModuleArg(&instance->config.bios);
|
||||
destroyModuleArg(&instance->config.board);
|
||||
destroyModuleArg(&instance->config.chassis);
|
||||
ffDestroyCommandOptions(&instance->config.command);
|
||||
destroyModuleArg(&instance->config.kernel);
|
||||
destroyModuleArg(&instance->config.uptime);
|
||||
destroyModuleArg(&instance->config.processes);
|
||||
@@ -409,14 +399,6 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffStrbufDestroy(&instance->config.publicIpUrl);
|
||||
ffStrbufDestroy(&instance->config.weatherOutputFormat);
|
||||
ffStrbufDestroy(&instance->config.playerName);
|
||||
|
||||
ffStrbufDestroy(&instance->config.commandShell);
|
||||
FF_LIST_FOR_EACH(FFstrbuf, item, instance->config.commandKeys)
|
||||
ffStrbufDestroy(item);
|
||||
ffListDestroy(&instance->config.commandKeys);
|
||||
FF_LIST_FOR_EACH(FFstrbuf, item, instance->config.commandTexts)
|
||||
ffStrbufDestroy(item);
|
||||
ffListDestroy(&instance->config.commandTexts);
|
||||
}
|
||||
|
||||
static void destroyState(FFinstance* instance)
|
||||
|
||||
+2
-15
@@ -1137,6 +1137,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(optionParseModuleArgs(key, value, "bios", &instance->config.bios)) {}
|
||||
else if(optionParseModuleArgs(key, value, "board", &instance->config.board)) {}
|
||||
else if(optionParseModuleArgs(key, value, "chassis", &instance->config.chassis)) {}
|
||||
else if(ffParseCommandCommandOptions(&instance->config.command, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "kernel", &instance->config.kernel)) {}
|
||||
else if(optionParseModuleArgs(key, value, "uptime", &instance->config.uptime)) {}
|
||||
else if(optionParseModuleArgs(key, value, "processes", &instance->config.processes)) {}
|
||||
@@ -1332,20 +1333,6 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
}
|
||||
else if(strcasecmp(key, "--percent-type") == 0)
|
||||
instance->config.percentType = optionParseUInt32(key, value);
|
||||
else if(strcasecmp(key, "--command-shell") == 0)
|
||||
optionParseString(key, value, &instance->config.commandShell);
|
||||
else if(strcasecmp(key, "--command-key") == 0)
|
||||
{
|
||||
FFstrbuf* result = (FFstrbuf*) ffListAdd(&instance->config.commandKeys);
|
||||
ffStrbufInit(result);
|
||||
optionParseString(key, value, result);
|
||||
}
|
||||
else if(strcasecmp(key, "--command-text") == 0)
|
||||
{
|
||||
FFstrbuf* result = (FFstrbuf*) ffListAdd(&instance->config.commandTexts);
|
||||
ffStrbufInit(result);
|
||||
optionParseString(key, value, result);
|
||||
}
|
||||
|
||||
//////////////////
|
||||
//Unknown option//
|
||||
@@ -1502,7 +1489,7 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
||||
else if(strcasecmp(line, "users") == 0)
|
||||
ffPrintUsers(instance);
|
||||
else if(strcasecmp(line, "command") == 0)
|
||||
ffPrintCommand(instance);
|
||||
ffPrintCommand(instance, &instance->config.command);
|
||||
else if(strcasecmp(line, "bluetooth") == 0)
|
||||
ffPrintBluetooth(instance);
|
||||
else if(strcasecmp(line, "sound") == 0)
|
||||
|
||||
+1
-5
@@ -139,6 +139,7 @@ typedef struct FFconfig
|
||||
FFModuleArgs board;
|
||||
FFModuleArgs brightness;
|
||||
FFModuleArgs chassis;
|
||||
FFCommandOptions command;
|
||||
FFModuleArgs kernel;
|
||||
FFModuleArgs uptime;
|
||||
FFModuleArgs processes;
|
||||
@@ -244,10 +245,6 @@ typedef struct FFconfig
|
||||
FFstrbuf playerName;
|
||||
|
||||
uint32_t percentType;
|
||||
|
||||
FFstrbuf commandShell;
|
||||
FFlist commandKeys;
|
||||
FFlist commandTexts;
|
||||
} FFconfig;
|
||||
|
||||
typedef struct FFstate
|
||||
@@ -349,7 +346,6 @@ void ffPrintVulkan(FFinstance* instance);
|
||||
void ffPrintOpenGL(FFinstance* instance);
|
||||
void ffPrintOpenCL(FFinstance* instance);
|
||||
void ffPrintUsers(FFinstance* instance);
|
||||
void ffPrintCommand(FFinstance* instance);
|
||||
void ffPrintBluetooth(FFinstance* instance);
|
||||
void ffPrintSound(FFinstance* instance);
|
||||
void ffPrintGamepad(FFinstance* instance);
|
||||
|
||||
@@ -1,67 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include "common/printing.h"
|
||||
#include "common/processing.h"
|
||||
#include "util/textModifier.h"
|
||||
|
||||
#define FF_COMMAND_MODULE_NAME "Command"
|
||||
|
||||
static void printError(FFinstance* instance, const char* key, const char* error)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key, 0, NULL);
|
||||
|
||||
if(instance->config.pipe)
|
||||
fputs(FASTFETCH_TEXT_MODIFIER_ERROR, stdout);
|
||||
|
||||
fputs(error, stdout);
|
||||
|
||||
if(!instance->config.pipe)
|
||||
fputs(FASTFETCH_TEXT_MODIFIER_RESET, stdout);
|
||||
|
||||
putchar('\n');
|
||||
}
|
||||
|
||||
void ffPrintCommand(FFinstance* instance)
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY key;
|
||||
ffStrbufInit(&key);
|
||||
|
||||
if(!ffListShift(&instance->config.commandKeys, &key))
|
||||
ffStrbufInitS(&key, FF_COMMAND_MODULE_NAME);
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY text;
|
||||
ffStrbufInit(&text);
|
||||
if(!ffListShift(&instance->config.commandTexts, &text))
|
||||
{
|
||||
printError(instance, key.chars, "No command text left");
|
||||
return;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY result;
|
||||
ffStrbufInit(&result);
|
||||
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
|
||||
instance->config.commandShell.chars,
|
||||
#ifdef _WIN32
|
||||
"/c",
|
||||
#else
|
||||
"-c",
|
||||
#endif
|
||||
text.chars,
|
||||
NULL
|
||||
});
|
||||
|
||||
if(error)
|
||||
{
|
||||
printError(instance, key.chars, error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!result.length)
|
||||
{
|
||||
printError(instance, key.chars, "No result printed");
|
||||
return;
|
||||
}
|
||||
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
puts(result.chars);
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
#include "fastfetch.h"
|
||||
|
||||
#include "common/printing.h"
|
||||
#include "common/processing.h"
|
||||
#include "modules/command/command.h"
|
||||
|
||||
#define FF_COMMAND_MODULE_NAME "Command"
|
||||
|
||||
void ffPrintCommand(FFinstance* instance, FFCommandOptions* options)
|
||||
{
|
||||
FF_STRBUF_AUTO_DESTROY result;
|
||||
ffStrbufInit(&result);
|
||||
const char* error = ffProcessAppendStdOut(&result, (char* const[]){
|
||||
options->shell.chars,
|
||||
#ifdef _WIN32
|
||||
"/c",
|
||||
#else
|
||||
"-c",
|
||||
#endif
|
||||
options->text.chars,
|
||||
NULL
|
||||
});
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(!result.length)
|
||||
{
|
||||
ffPrintError(instance, FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs, "No result printed");
|
||||
return;
|
||||
}
|
||||
|
||||
ffPrintLogoAndKey(instance, FF_COMMAND_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
ffStrbufPutTo(&result, stdout);
|
||||
}
|
||||
|
||||
void ffInitCommandOptions(FFCommandOptions* options)
|
||||
{
|
||||
options->moduleName = FF_COMMAND_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
|
||||
ffStrbufInitS(&options->shell,
|
||||
#ifdef _WIN32
|
||||
"cmd"
|
||||
#elif defined(__FreeBSD__)
|
||||
"csh"
|
||||
#else
|
||||
"bash"
|
||||
#endif
|
||||
);
|
||||
|
||||
ffStrbufInit(&options->text);
|
||||
}
|
||||
|
||||
bool ffParseCommandCommandOptions(FFCommandOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_COMMAND_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
if(strcasecmp(subKey, "shell") == 0)
|
||||
{
|
||||
ffOptionParseString(key, value, &options->shell);
|
||||
return true;
|
||||
}
|
||||
|
||||
if(strcasecmp(subKey, "text") == 0)
|
||||
{
|
||||
ffOptionParseString(key, value, &options->text);
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyCommandOptions(FFCommandOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
ffStrbufDestroy(&options->shell);
|
||||
ffStrbufDestroy(&options->text);
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
#include "modules/command/option.h"
|
||||
|
||||
void ffPrintCommand(FFinstance* instance, FFCommandOptions* options);
|
||||
void ffInitCommandOptions(FFCommandOptions* options);
|
||||
bool ffParseCommandCommandOptions(FFCommandOptions* options, const char* key, const char* value);
|
||||
void ffDestroyCommandOptions(FFCommandOptions* options);
|
||||
@@ -0,0 +1,14 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFCommandOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
|
||||
FFstrbuf shell;
|
||||
FFstrbuf text;
|
||||
} FFCommandOptions;
|
||||
@@ -4,3 +4,4 @@
|
||||
|
||||
#include "modules/os/os.h"
|
||||
#include "modules/battery/battery.h"
|
||||
#include "modules/command/command.h"
|
||||
|
||||
@@ -4,3 +4,4 @@
|
||||
|
||||
#include "modules/battery/option.h"
|
||||
#include "modules/os/option.h"
|
||||
#include "modules/command/option.h"
|
||||
|
||||
+1
-2
@@ -108,7 +108,7 @@ void ffPrintOS(FFinstance* instance, FFOSOptions* options)
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
FFstrbuf result;
|
||||
FF_STRBUF_AUTO_DESTROY result;
|
||||
ffStrbufInit(&result);
|
||||
|
||||
if(ffStrbufIgnCaseCompS(&os->id, "nixos") == 0)
|
||||
@@ -118,7 +118,6 @@ void ffPrintOS(FFinstance* instance, FFOSOptions* options)
|
||||
|
||||
ffPrintLogoAndKey(instance, FF_OS_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
ffStrbufPutTo(&result, stdout);
|
||||
ffStrbufDestroy(&result);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user