mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 09:33:39 +01:00
Users: add JSON config support
This commit is contained in:
+1
-1
@@ -306,7 +306,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/theme.c
|
||||
src/modules/title/title.c
|
||||
src/modules/uptime/uptime.c
|
||||
src/modules/users.c
|
||||
src/modules/users/users.c
|
||||
src/modules/vulkan/vulkan.c
|
||||
src/modules/wallpaper/wallpaper.c
|
||||
src/modules/weather.c
|
||||
|
||||
+2
-2
@@ -100,7 +100,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffInitWallpaperOptions(&instance->config.wallpaper);
|
||||
ffInitOpenGLOptions(&instance->config.openGL);
|
||||
ffInitOpenCLOptions(&instance->config.openCL);
|
||||
initModuleArg(&instance->config.users);
|
||||
ffInitUsersOptions(&instance->config.users);
|
||||
ffInitBluetoothOptions(&instance->config.bluetooth);
|
||||
ffInitSoundOptions(&instance->config.sound);
|
||||
ffInitSeparatorOptions(&instance->config.separator);
|
||||
@@ -319,7 +319,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffDestroyVulkanOptions(&instance->config.vulkan);
|
||||
ffDestroyOpenGLOptions(&instance->config.openGL);
|
||||
ffDestroyOpenCLOptions(&instance->config.openCL);
|
||||
destroyModuleArg(&instance->config.users);
|
||||
ffDestroyUsersOptions(&instance->config.users);
|
||||
ffDestroyBluetoothOptions(&instance->config.bluetooth);
|
||||
ffDestroySeparatorOptions(&instance->config.separator);
|
||||
ffDestroySoundOptions(&instance->config.sound);
|
||||
|
||||
+3
-3
@@ -1017,7 +1017,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(ffParseVulkanCommandOptions(&instance->config.vulkan, key, value)) {}
|
||||
else if(ffParseOpenGLCommandOptions(&instance->config.openGL, key, value)) {}
|
||||
else if(ffParseOpenCLCommandOptions(&instance->config.openCL, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "users", &instance->config.users)) {}
|
||||
else if(ffParseUsersCommandOptions(&instance->config.users, key, value)) {}
|
||||
else if(ffParseBluetoothCommandOptions(&instance->config.bluetooth, key, value)) {}
|
||||
else if(ffParseSeparatorCommandOptions(&instance->config.separator, key, value)) {}
|
||||
else if(ffParseSoundCommandOptions(&instance->config.sound, key, value)) {}
|
||||
@@ -1244,8 +1244,8 @@ static void parseStructureCommand(FFinstance* instance, const char* line)
|
||||
ffPrintOpenGL(instance, &instance->config.openGL);
|
||||
else if(strcasecmp(line, FF_OPENCL_MODULE_NAME) == 0)
|
||||
ffPrintOpenCL(instance, &instance->config.openCL);
|
||||
else if(strcasecmp(line, "users") == 0)
|
||||
ffPrintUsers(instance);
|
||||
else if(strcasecmp(line, FF_USERS_MODULE_NAME) == 0)
|
||||
ffPrintUsers(instance, &instance->config.users);
|
||||
else if(strcasecmp(line, FF_COMMAND_MODULE_NAME) == 0)
|
||||
ffPrintCommand(instance, &instance->config.command);
|
||||
else if(strcasecmp(line, FF_BLUETOOTH_MODULE_NAME) == 0)
|
||||
|
||||
+1
-2
@@ -91,7 +91,7 @@ typedef struct FFconfig
|
||||
FFVulkanOptions vulkan;
|
||||
FFOpenGLOptions openGL;
|
||||
FFOpenCLOptions openCL;
|
||||
FFModuleArgs users;
|
||||
FFUsersOptions users;
|
||||
FFBluetoothOptions bluetooth;
|
||||
FFSeparatorOptions separator;
|
||||
FFSoundOptions sound;
|
||||
@@ -195,6 +195,5 @@ void ffPrintMedia(FFinstance* instance);
|
||||
void ffPrintPublicIp(FFinstance* instance);
|
||||
void ffPrintWeather(FFinstance* instance);
|
||||
void ffPrintColors(FFinstance* instance);
|
||||
void ffPrintUsers(FFinstance* instance);
|
||||
|
||||
#endif
|
||||
|
||||
+1
-1
@@ -63,7 +63,7 @@ int main(int argc, char** argv)
|
||||
//ffPrintVulkan(&instance, &instance.config.vulkan);
|
||||
//ffPrintOpenGL(&instance, &instance.config.openGL);
|
||||
//ffPrintOpenCL(&instance, &instance.config.openCL);
|
||||
//ffPrintUsers(&instance);
|
||||
//ffPrintUsers(&instance, &instance.config.users);
|
||||
//ffPrintWeather(&instance);
|
||||
//ffPrintBluetooth(&instance);
|
||||
//ffPrintSound(&instance, &instance.config.sound);
|
||||
|
||||
@@ -136,6 +136,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
case 'U': {
|
||||
return
|
||||
tryModule(instance, type, module, FF_UPTIME_MODULE_NAME, ffParseUptimeJsonObject) ||
|
||||
tryModule(instance, type, module, FF_USERS_MODULE_NAME, ffParseUsersJsonObject) ||
|
||||
false;
|
||||
}
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@
|
||||
#include "modules/title/title.h"
|
||||
#include "modules/jsonconfig/jsonconfig.h"
|
||||
#include "modules/uptime/uptime.h"
|
||||
#include "modules/users/users.h"
|
||||
#include "modules/vulkan/vulkan.h"
|
||||
#include "modules/wallpaper/wallpaper.h"
|
||||
#include "modules/wifi/wifi.h"
|
||||
|
||||
@@ -38,6 +38,7 @@
|
||||
#include "modules/terminalfont/option.h"
|
||||
#include "modules/title/option.h"
|
||||
#include "modules/uptime/option.h"
|
||||
#include "modules/users/option.h"
|
||||
#include "modules/vulkan/option.h"
|
||||
#include "modules/wallpaper/option.h"
|
||||
#include "modules/wifi/option.h"
|
||||
|
||||
@@ -1,43 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/users/users.h"
|
||||
|
||||
#define FF_USERS_MODULE_NAME "Users"
|
||||
#define FF_USERS_NUM_FORMAT_ARGS 1
|
||||
|
||||
void ffPrintUsers(FFinstance* instance)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY users = ffListCreate(sizeof(FFstrbuf));
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY error = ffStrbufCreate();
|
||||
|
||||
ffDetectUsers(&users, &error);
|
||||
|
||||
if(error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_USERS_MODULE_NAME, 0, &instance->config.users, "%*s", error.length, error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
|
||||
for(uint32_t i = 0; i < users.length; ++i)
|
||||
{
|
||||
if(i > 0)
|
||||
ffStrbufAppendS(&result, ", ");
|
||||
FFstrbuf* user = (FFstrbuf*)ffListGet(&users, i);
|
||||
ffStrbufAppend(&result, user);
|
||||
ffStrbufDestroy(user);
|
||||
}
|
||||
|
||||
if(instance->config.users.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_USERS_MODULE_NAME, 0, &instance->config.users.key);
|
||||
puts(result.chars);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_USERS_MODULE_NAME, 0, &instance->config.users, FF_USERS_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result},
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
// This file will be included in "fastfetch.h", do NOT put unnecessary things here
|
||||
|
||||
#include "common/option.h"
|
||||
|
||||
typedef struct FFUsersOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFUsersOptions;
|
||||
@@ -0,0 +1,85 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/users/users.h"
|
||||
#include "modules/users/users.h"
|
||||
|
||||
#define FF_USERS_NUM_FORMAT_ARGS 1
|
||||
|
||||
void ffPrintUsers(FFinstance* instance, FFUsersOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY users = ffListCreate(sizeof(FFstrbuf));
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY error = ffStrbufCreate();
|
||||
|
||||
ffDetectUsers(&users, &error);
|
||||
|
||||
if(error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_USERS_MODULE_NAME, 0, &options->moduleArgs, "%*s", error.length, error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY result = ffStrbufCreate();
|
||||
for(uint32_t i = 0; i < users.length; ++i)
|
||||
{
|
||||
if(i > 0)
|
||||
ffStrbufAppendS(&result, ", ");
|
||||
FFstrbuf* user = (FFstrbuf*)ffListGet(&users, i);
|
||||
ffStrbufAppend(&result, user);
|
||||
ffStrbufDestroy(user);
|
||||
}
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_USERS_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
puts(result.chars);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_USERS_MODULE_NAME, 0, &options->moduleArgs, FF_USERS_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitUsersOptions(FFUsersOptions* options)
|
||||
{
|
||||
options->moduleName = FF_USERS_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParseUsersCommandOptions(FFUsersOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_USERS_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyUsersOptions(FFUsersOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseUsersJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFUsersOptions __attribute__((__cleanup__(ffDestroyUsersOptions))) options;
|
||||
ffInitUsersOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(instance, FF_USERS_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintUsers(instance, &options);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_USERS_MODULE_NAME "Users"
|
||||
|
||||
void ffPrintUsers(FFinstance* instance, FFUsersOptions* options);
|
||||
void ffInitUsersOptions(FFUsersOptions* options);
|
||||
bool ffParseUsersCommandOptions(FFUsersOptions* options, const char* key, const char* value);
|
||||
void ffDestroyUsersOptions(FFUsersOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseUsersJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
||||
Reference in New Issue
Block a user