mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-29 16:26:39 +01:00
Media: add JSON config support
This commit is contained in:
+1
-1
@@ -300,7 +300,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/shell/shell.c
|
||||
src/modules/sound/sound.c
|
||||
src/modules/swap/swap.c
|
||||
src/modules/media.c
|
||||
src/modules/media/media.c
|
||||
src/modules/terminal/terminal.c
|
||||
src/modules/terminalfont/terminalfont.c
|
||||
src/modules/theme.c
|
||||
|
||||
+2
-2
@@ -94,7 +94,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
initModuleArg(&instance->config.weather);
|
||||
ffInitWifiOptions(&instance->config.wifi);
|
||||
initModuleArg(&instance->config.player);
|
||||
initModuleArg(&instance->config.media);
|
||||
ffInitMediaOptions(&instance->config.media);
|
||||
ffInitDateTimeOptions(&instance->config.dateTime);
|
||||
ffInitVulkanOptions(&instance->config.vulkan);
|
||||
ffInitWallpaperOptions(&instance->config.wallpaper);
|
||||
@@ -314,7 +314,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
destroyModuleArg(&instance->config.weather);
|
||||
ffDestroyWifiOptions(&instance->config.wifi);
|
||||
destroyModuleArg(&instance->config.player);
|
||||
destroyModuleArg(&instance->config.media);
|
||||
ffDestroyMediaOptions(&instance->config.media);
|
||||
ffDestroyDateTimeOptions(&instance->config.dateTime);
|
||||
ffDestroyVulkanOptions(&instance->config.vulkan);
|
||||
ffDestroyOpenGLOptions(&instance->config.openGL);
|
||||
|
||||
+3
-3
@@ -1012,7 +1012,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(optionParseModuleArgs(key, value, "publicip", &instance->config.publicIP)) {}
|
||||
else if(optionParseModuleArgs(key, value, "weather", &instance->config.weather)) {}
|
||||
else if(optionParseModuleArgs(key, value, "player", &instance->config.player)) {}
|
||||
else if(optionParseModuleArgs(key, value, "media", &instance->config.media)) {}
|
||||
else if(ffParseMediaCommandOptions(&instance->config.media, key, value)) {}
|
||||
else if(ffParseDateTimeCommandOptions(&instance->config.dateTime, key, value)) {}
|
||||
else if(ffParseVulkanCommandOptions(&instance->config.vulkan, key, value)) {}
|
||||
else if(ffParseOpenGLCommandOptions(&instance->config.openGL, key, value)) {}
|
||||
@@ -1232,8 +1232,8 @@ static void parseStructureCommand(FFinstance* instance, const char* line)
|
||||
ffPrintWeather(instance);
|
||||
else if(strcasecmp(line, "player") == 0)
|
||||
ffPrintPlayer(instance);
|
||||
else if(strcasecmp(line, "media") == 0)
|
||||
ffPrintMedia(instance);
|
||||
else if(strcasecmp(line, FF_MEDIA_MODULE_NAME) == 0)
|
||||
ffPrintMedia(instance, &instance->config.media);
|
||||
else if(strcasecmp(line, FF_DATETIME_MODULE_NAME) == 0)
|
||||
ffPrintDateTime(instance, &instance->config.dateTime);
|
||||
else if(strcasecmp(line, FF_COLORS_MODULE_NAME) == 0)
|
||||
|
||||
+1
-2
@@ -86,7 +86,7 @@ typedef struct FFconfig
|
||||
FFModuleArgs publicIP;
|
||||
FFModuleArgs weather;
|
||||
FFModuleArgs player;
|
||||
FFModuleArgs media;
|
||||
FFMediaOptions media;
|
||||
FFDateTimeOptions dateTime;
|
||||
FFVulkanOptions vulkan;
|
||||
FFOpenGLOptions openGL;
|
||||
@@ -191,7 +191,6 @@ void ffPrintChassis(FFinstance* instance);
|
||||
void ffPrintProcesses(FFinstance* instance);
|
||||
void ffPrintTheme(FFinstance* instance);
|
||||
void ffPrintPlayer(FFinstance* instance);
|
||||
void ffPrintMedia(FFinstance* instance);
|
||||
void ffPrintPublicIp(FFinstance* instance);
|
||||
void ffPrintWeather(FFinstance* instance);
|
||||
|
||||
|
||||
+1
-1
@@ -51,7 +51,7 @@ int main(int argc, char** argv)
|
||||
ffPrintBattery(&instance, &instance.config.battery);
|
||||
ffPrintPowerAdapter(&instance, &instance.config.powerAdapter);
|
||||
//ffPrintPlayer(&instance);
|
||||
//ffPrintMedia(&instance);
|
||||
//ffPrintMedia(&instance, &instance.config.media);
|
||||
//ffPrintLocalIp(&instance);
|
||||
//ffPrintPublicIp(&instance);
|
||||
//ffPrintWifi(&instance);
|
||||
|
||||
@@ -97,6 +97,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
|
||||
case 'M': {
|
||||
return
|
||||
tryModule(instance, type, module, FF_MEDIA_MODULE_NAME, ffParseMediaJsonObject) ||
|
||||
tryModule(instance, type, module, FF_MEMORY_MODULE_NAME, ffParseMemoryJsonObject) ||
|
||||
false;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/media/media.h"
|
||||
#include "modules/media/media.h"
|
||||
|
||||
#include <ctype.h>
|
||||
|
||||
#define FF_MEDIA_MODULE_NAME "Media"
|
||||
#define FF_MEDIA_NUM_FORMAT_ARGS 5
|
||||
|
||||
static bool shouldIgoreChar(char c)
|
||||
static inline bool shouldIgnoreChar(char c)
|
||||
{
|
||||
return isblank(c) || c == '-' || c == '.';
|
||||
}
|
||||
@@ -19,10 +19,10 @@ static bool artistInSongTitle(const FFstrbuf* song, const FFstrbuf* artist)
|
||||
|
||||
while(true)
|
||||
{
|
||||
while(shouldIgoreChar(song->chars[songIndex]))
|
||||
while(shouldIgnoreChar(song->chars[songIndex]))
|
||||
++songIndex;
|
||||
|
||||
while(shouldIgoreChar(artist->chars[artistIndex]))
|
||||
while(shouldIgnoreChar(artist->chars[artistIndex]))
|
||||
++artistIndex;
|
||||
|
||||
if(artist->chars[artistIndex] == '\0')
|
||||
@@ -42,13 +42,13 @@ static bool artistInSongTitle(const FFstrbuf* song, const FFstrbuf* artist)
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffPrintMedia(FFinstance* instance)
|
||||
void ffPrintMedia(FFinstance* instance, FFMediaOptions* options)
|
||||
{
|
||||
const FFMediaResult* media = ffDetectMedia(instance);
|
||||
|
||||
if(media->error.length > 0)
|
||||
{
|
||||
ffPrintError(instance, FF_MEDIA_MODULE_NAME, 0, &instance->config.media, "%s", media->error.chars);
|
||||
ffPrintError(instance, FF_MEDIA_MODULE_NAME, 0, &options->moduleArgs, "%s", media->error.chars);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -68,7 +68,7 @@ void ffPrintMedia(FFinstance* instance)
|
||||
if(songPretty.length == 0)
|
||||
ffStrbufAppend(&songPretty, &media->song);
|
||||
|
||||
if(instance->config.media.outputFormat.length == 0)
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
//We don't expose artistPretty to the format, as it might be empty (when the think that the artist is already in the song title)
|
||||
FF_STRBUF_AUTO_DESTROY artistPretty = ffStrbufCreateCopy(&media->artist);
|
||||
@@ -79,7 +79,7 @@ void ffPrintMedia(FFinstance* instance)
|
||||
if(artistInSongTitle(&songPretty, &artistPretty))
|
||||
ffStrbufClear(&artistPretty);
|
||||
|
||||
ffPrintLogoAndKey(instance, FF_MEDIA_MODULE_NAME, 0, &instance->config.media.key);
|
||||
ffPrintLogoAndKey(instance, FF_MEDIA_MODULE_NAME, 0, &options->moduleArgs.key);
|
||||
|
||||
if(artistPretty.length > 0)
|
||||
{
|
||||
@@ -94,7 +94,7 @@ void ffPrintMedia(FFinstance* instance)
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_MEDIA_MODULE_NAME, 0, &instance->config.media, FF_MEDIA_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
ffPrintFormat(instance, FF_MEDIA_MODULE_NAME, 0, &options->moduleArgs, FF_MEDIA_NUM_FORMAT_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &songPretty},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &media->song},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &media->artist},
|
||||
@@ -103,3 +103,45 @@ void ffPrintMedia(FFinstance* instance)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitMediaOptions(FFMediaOptions* options)
|
||||
{
|
||||
options->moduleName = FF_MEDIA_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParseMediaCommandOptions(FFMediaOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_MEDIA_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyMediaOptions(FFMediaOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParseMediaJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFMediaOptions __attribute__((__cleanup__(ffDestroyMediaOptions))) options;
|
||||
ffInitMediaOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(instance, FF_MEDIA_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintMedia(instance, &options);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_MEDIA_MODULE_NAME "Media"
|
||||
|
||||
void ffPrintMedia(FFinstance* instance, FFMediaOptions* options);
|
||||
void ffInitMediaOptions(FFMediaOptions* options);
|
||||
bool ffParseMediaCommandOptions(FFMediaOptions* options, const char* key, const char* value);
|
||||
void ffDestroyMediaOptions(FFMediaOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParseMediaJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
||||
@@ -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 FFMediaOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFMediaOptions;
|
||||
@@ -26,6 +26,7 @@
|
||||
#include "modules/kernel/kernel.h"
|
||||
#include "modules/locale/locale.h"
|
||||
#include "modules/localip/localip.h"
|
||||
#include "modules/media/media.h"
|
||||
#include "modules/memory/memory.h"
|
||||
#include "modules/opengl/opengl.h"
|
||||
#include "modules/opencl/opencl.h"
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
#include "modules/kernel/option.h"
|
||||
#include "modules/locale/option.h"
|
||||
#include "modules/localip/option.h"
|
||||
#include "modules/media/option.h"
|
||||
#include "modules/memory/option.h"
|
||||
#include "modules/opengl/option.h"
|
||||
#include "modules/opencl/option.h"
|
||||
|
||||
Reference in New Issue
Block a user