mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-06 13:14:21 +01:00
PowerAdapter: support JSON config
This commit is contained in:
@@ -103,6 +103,7 @@ static bool parseModuleJsonObject(FFinstance* instance, const char* type, json_o
|
||||
case 'P': {
|
||||
return
|
||||
tryModule(instance, type, module, FF_PACKAGES_MODULE_NAME, ffParsePackagesJsonObject) ||
|
||||
tryModule(instance, type, module, FF_POWERADAPTER_MODULE_NAME, ffParsePowerAdapterJsonObject) ||
|
||||
false;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include "modules/memory/memory.h"
|
||||
#include "modules/os/os.h"
|
||||
#include "modules/packages/packages.h"
|
||||
#include "modules/poweradapter/poweradapter.h"
|
||||
#include "modules/separator/separator.h"
|
||||
#include "modules/shell/shell.h"
|
||||
#include "modules/sound/sound.h"
|
||||
|
||||
@@ -25,6 +25,7 @@
|
||||
#include "modules/memory/option.h"
|
||||
#include "modules/os/option.h"
|
||||
#include "modules/packages/option.h"
|
||||
#include "modules/poweradapter/option.h"
|
||||
#include "modules/separator/option.h"
|
||||
#include "modules/shell/option.h"
|
||||
#include "modules/sound/option.h"
|
||||
|
||||
@@ -1,66 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/poweradapter/poweradapter.h"
|
||||
|
||||
#define FF_POWER_ADAPTER_MODULE_NAME "Power Adapter"
|
||||
#define FF_POWER_ADAPTER_MODULE_ARGS 5
|
||||
|
||||
static void printPowerAdapter(FFinstance* instance, const PowerAdapterResult* result, uint8_t index)
|
||||
{
|
||||
if(result->watts != FF_POWER_ADAPTER_UNSET)
|
||||
{
|
||||
if(instance->config.powerAdapter.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_POWER_ADAPTER_MODULE_NAME, index, &instance->config.powerAdapter.key);
|
||||
|
||||
if(result->name.length > 0)
|
||||
puts(result->name.chars);
|
||||
else if(result->watts == FF_POWER_ADAPTER_NOT_CONNECTED)
|
||||
puts("not connected");
|
||||
else
|
||||
printf("%dW\n", result->watts);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_POWER_ADAPTER_MODULE_NAME, index, &instance->config.powerAdapter, FF_POWER_ADAPTER_MODULE_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_INT, &result->watts},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->name},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->manufacturer},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->modelName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->description},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ffPrintPowerAdapter(FFinstance* instance)
|
||||
{
|
||||
FFlist results;
|
||||
ffListInitA(&results, sizeof(PowerAdapterResult), 0);
|
||||
|
||||
const char* error = ffDetectPowerAdapterImpl(instance, &results);
|
||||
|
||||
if (error)
|
||||
{
|
||||
ffPrintError(instance, FF_POWER_ADAPTER_MODULE_NAME, 0, &instance->config.powerAdapter, "%s", error);
|
||||
}
|
||||
else if(results.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_POWER_ADAPTER_MODULE_NAME, 0, &instance->config.powerAdapter, "No power adapters found");
|
||||
}
|
||||
else
|
||||
{
|
||||
for(uint8_t i = 0; i < (uint8_t) results.length; i++)
|
||||
{
|
||||
PowerAdapterResult* result = ffListGet(&results, i);
|
||||
printPowerAdapter(instance, result, i);
|
||||
|
||||
ffStrbufDestroy(&result->manufacturer);
|
||||
ffStrbufDestroy(&result->description);
|
||||
ffStrbufDestroy(&result->modelName);
|
||||
ffStrbufDestroy(&result->name);
|
||||
}
|
||||
}
|
||||
|
||||
ffListDestroy(&results);
|
||||
}
|
||||
@@ -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 FFPowerAdapterOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFPowerAdapterOptions;
|
||||
@@ -0,0 +1,105 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/poweradapter/poweradapter.h"
|
||||
#include "modules/poweradapter/poweradapter.h"
|
||||
|
||||
#define FF_POWERADAPTER_DISPLAY_NAME "Power Adapter"
|
||||
#define FF_POWERADAPTER_MODULE_ARGS 5
|
||||
|
||||
void ffPrintPowerAdapter(FFinstance* instance, FFPowerAdapterOptions* options)
|
||||
{
|
||||
FFlist results;
|
||||
ffListInit(&results, sizeof(PowerAdapterResult));
|
||||
|
||||
const char* error = ffDetectPowerAdapterImpl(instance, &results);
|
||||
|
||||
if (error)
|
||||
{
|
||||
ffPrintError(instance, FF_POWERADAPTER_DISPLAY_NAME, 0, &options->moduleArgs, "%s", error);
|
||||
}
|
||||
else if(results.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_POWERADAPTER_DISPLAY_NAME, 0, &options->moduleArgs, "No power adapters found");
|
||||
}
|
||||
else
|
||||
{
|
||||
for(uint8_t i = 0; i < (uint8_t) results.length; i++)
|
||||
{
|
||||
PowerAdapterResult* result = ffListGet(&results, i);
|
||||
|
||||
if(result->watts != FF_POWERADAPTER_UNSET)
|
||||
{
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, FF_POWERADAPTER_DISPLAY_NAME, i, &options->moduleArgs.key);
|
||||
|
||||
if(result->name.length > 0)
|
||||
puts(result->name.chars);
|
||||
else if(result->watts == FF_POWERADAPTER_NOT_CONNECTED)
|
||||
puts("not connected");
|
||||
else
|
||||
printf("%dW\n", result->watts);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormat(instance, FF_POWERADAPTER_DISPLAY_NAME, i, &options->moduleArgs, FF_POWERADAPTER_MODULE_ARGS, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_INT, &result->watts},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->name},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->manufacturer},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->modelName},
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &result->description},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
ffStrbufDestroy(&result->manufacturer);
|
||||
ffStrbufDestroy(&result->description);
|
||||
ffStrbufDestroy(&result->modelName);
|
||||
ffStrbufDestroy(&result->name);
|
||||
}
|
||||
}
|
||||
|
||||
ffListDestroy(&results);
|
||||
}
|
||||
|
||||
void ffInitPowerAdapterOptions(FFPowerAdapterOptions* options)
|
||||
{
|
||||
options->moduleName = FF_POWERADAPTER_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParsePowerAdapterCommandOptions(FFPowerAdapterOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_POWERADAPTER_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyPowerAdapterOptions(FFPowerAdapterOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
void ffParsePowerAdapterJsonObject(FFinstance* instance, json_object* module)
|
||||
{
|
||||
FFPowerAdapterOptions __attribute__((__cleanup__(ffDestroyPowerAdapterOptions))) options;
|
||||
ffInitPowerAdapterOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
json_object_object_foreach(module, key, val)
|
||||
{
|
||||
if (ffJsonConfigParseModuleArgs(key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(instance, FF_POWERADAPTER_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintPowerAdapter(instance, &options);
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,15 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
#define FF_POWERADAPTER_MODULE_NAME "PowerAdapter"
|
||||
|
||||
void ffPrintPowerAdapter(FFinstance* instance, FFPowerAdapterOptions* options);
|
||||
void ffInitPowerAdapterOptions(FFPowerAdapterOptions* options);
|
||||
bool ffParsePowerAdapterCommandOptions(FFPowerAdapterOptions* options, const char* key, const char* value);
|
||||
void ffDestroyPowerAdapterOptions(FFPowerAdapterOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/jsonconfig.h"
|
||||
void ffParsePowerAdapterJsonObject(FFinstance* instance, json_object* module);
|
||||
#endif
|
||||
Reference in New Issue
Block a user