mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-10-30 17:41:41 +01:00
Brightness: support JSON config
This commit is contained in:
+1
-1
@@ -265,7 +265,7 @@ set(LIBFASTFETCH_SRC
|
||||
src/modules/bios/bios.c
|
||||
src/modules/bluetooth/bluetooth.c
|
||||
src/modules/board/board.c
|
||||
src/modules/brightness.c
|
||||
src/modules/brightness/brightness.c
|
||||
src/modules/break/break.c
|
||||
src/modules/chassis.c
|
||||
src/modules/colors.c
|
||||
|
||||
+2
-1
@@ -62,7 +62,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffInitHostOptions(&instance->config.host);
|
||||
ffInitBiosOptions(&instance->config.bios);
|
||||
ffInitBoardOptions(&instance->config.board);
|
||||
initModuleArg(&instance->config.brightness);
|
||||
ffInitBrightnessOptions(&instance->config.brightness);
|
||||
initModuleArg(&instance->config.chassis);
|
||||
ffInitCommandOptions(&instance->config.command);
|
||||
ffInitKernelOptions(&instance->config.kernel);
|
||||
@@ -298,6 +298,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
ffDestroyHostOptions(&instance->config.host);
|
||||
ffDestroyBiosOptions(&instance->config.bios);
|
||||
ffDestroyBoardOptions(&instance->config.board);
|
||||
ffDestroyBrightnessOptions(&instance->config.brightness);
|
||||
destroyModuleArg(&instance->config.chassis);
|
||||
ffDestroyCommandOptions(&instance->config.command);
|
||||
ffDestroyKernelOptions(&instance->config.kernel);
|
||||
|
||||
+2
-2
@@ -1021,7 +1021,7 @@ static void parseOption(FFinstance* instance, FFdata* data, const char* key, con
|
||||
else if(optionParseModuleArgs(key, value, "packages", &instance->config.packages)) {}
|
||||
else if(optionParseModuleArgs(key, value, "shell", &instance->config.shell)) {}
|
||||
else if(ffParseDisplayCommandOptions(&instance->config.display, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "brightness", &instance->config.brightness)) {}
|
||||
else if(ffParseBrightnessCommandOptions(&instance->config.brightness, key, value)) {}
|
||||
else if(optionParseModuleArgs(key, value, "de", &instance->config.de)) {}
|
||||
else if(optionParseModuleArgs(key, value, "wifi", &instance->config.wifi)) {}
|
||||
else if(optionParseModuleArgs(key, value, "wm", &instance->config.wm)) {}
|
||||
@@ -1265,7 +1265,7 @@ static void parseStructureCommand(FFinstance* instance, FFdata* data, const char
|
||||
else if(strcasecmp(line, "board") == 0)
|
||||
ffPrintBoard(instance, &instance->config.board);
|
||||
else if(strcasecmp(line, "brightness") == 0)
|
||||
ffPrintBrightness(instance);
|
||||
ffPrintBrightness(instance, &instance->config.brightness);
|
||||
else if(strcasecmp(line, "chassis") == 0)
|
||||
ffPrintChassis(instance);
|
||||
else if(strcasecmp(line, "kernel") == 0)
|
||||
|
||||
+1
-2
@@ -96,7 +96,7 @@ typedef struct FFconfig
|
||||
FFHostOptions host;
|
||||
FFBiosOptions bios;
|
||||
FFBoardOptions board;
|
||||
FFModuleArgs brightness;
|
||||
FFBrightnessOptions brightness;
|
||||
FFModuleArgs chassis;
|
||||
FFCommandOptions command;
|
||||
FFKernelOptions kernel;
|
||||
@@ -255,7 +255,6 @@ void ffPrintUptime(FFinstance* instance);
|
||||
void ffPrintProcesses(FFinstance* instance);
|
||||
void ffPrintPackages(FFinstance* instance);
|
||||
void ffPrintShell(FFinstance* instance);
|
||||
void ffPrintBrightness(FFinstance* instance);
|
||||
void ffPrintDesktopEnvironment(FFinstance* instance);
|
||||
void ffPrintWM(FFinstance* instance);
|
||||
void ffPrintWMTheme(FFinstance* instance);
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/brightness/brightness.h"
|
||||
|
||||
#define FF_BRIGHTNESS_MODULE_NAME "Brightness"
|
||||
#define FF_BRIGHTNESS_NUM_FORMAT_ARGS 2
|
||||
|
||||
void ffPrintBrightness(FFinstance* instance)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFBrightnessResult));
|
||||
const char* error = ffDetectBrightness(&result);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &instance->config.brightness, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(result.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &instance->config.brightness, "No result is detected.");
|
||||
return;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY key;
|
||||
ffStrbufInit(&key);
|
||||
|
||||
FF_LIST_FOR_EACH(FFBrightnessResult, item, result)
|
||||
{
|
||||
if(instance->config.brightness.key.length == 0)
|
||||
{
|
||||
ffStrbufAppendF(&key, "%s (%s)", FF_BRIGHTNESS_MODULE_NAME, item->name.chars);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffParseFormatString(&key, &instance->config.brightness.key, 1, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &item->name}
|
||||
});
|
||||
}
|
||||
|
||||
if(instance->config.brightness.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
printf("%.0f%%\n", item->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &instance->config.brightness.outputFormat, FF_BRIGHTNESS_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &item->value},
|
||||
{FF_FORMAT_ARG_TYPE_FLOAT, &item->name}
|
||||
});
|
||||
}
|
||||
|
||||
ffStrbufClear(&key);
|
||||
ffStrbufDestroy(&item->name);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,111 @@
|
||||
#include "fastfetch.h"
|
||||
#include "common/printing.h"
|
||||
#include "detection/brightness/brightness.h"
|
||||
#include "modules/brightness/brightness.h"
|
||||
|
||||
#define FF_BRIGHTNESS_MODULE_NAME "Brightness"
|
||||
#define FF_BRIGHTNESS_NUM_FORMAT_ARGS 2
|
||||
|
||||
void ffPrintBrightness(FFinstance* instance, FFBrightnessOptions* options)
|
||||
{
|
||||
FF_LIST_AUTO_DESTROY result;
|
||||
ffListInit(&result, sizeof(FFBrightnessResult));
|
||||
const char* error = ffDetectBrightness(&result);
|
||||
|
||||
if(error)
|
||||
{
|
||||
ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &options->moduleArgs, "%s", error);
|
||||
return;
|
||||
}
|
||||
|
||||
if(result.length == 0)
|
||||
{
|
||||
ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &options->moduleArgs, "No result is detected.");
|
||||
return;
|
||||
}
|
||||
|
||||
FF_STRBUF_AUTO_DESTROY key;
|
||||
ffStrbufInit(&key);
|
||||
|
||||
FF_LIST_FOR_EACH(FFBrightnessResult, item, result)
|
||||
{
|
||||
if(options->moduleArgs.key.length == 0)
|
||||
{
|
||||
ffStrbufAppendF(&key, "%s (%s)", FF_BRIGHTNESS_MODULE_NAME, item->name.chars);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffParseFormatString(&key, &options->moduleArgs.key, 1, (FFformatarg[]){
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &item->name}
|
||||
});
|
||||
}
|
||||
|
||||
if(options->moduleArgs.outputFormat.length == 0)
|
||||
{
|
||||
ffPrintLogoAndKey(instance, key.chars, 0, NULL);
|
||||
printf("%.0f%%\n", item->value);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffPrintFormatString(instance, key.chars, 0, NULL, &options->moduleArgs.outputFormat, FF_BRIGHTNESS_NUM_FORMAT_ARGS, (FFformatarg[]) {
|
||||
{FF_FORMAT_ARG_TYPE_STRBUF, &item->value},
|
||||
{FF_FORMAT_ARG_TYPE_FLOAT, &item->name}
|
||||
});
|
||||
}
|
||||
|
||||
ffStrbufClear(&key);
|
||||
ffStrbufDestroy(&item->name);
|
||||
}
|
||||
}
|
||||
|
||||
void ffInitBrightnessOptions(FFBrightnessOptions* options)
|
||||
{
|
||||
options->moduleName = FF_BRIGHTNESS_MODULE_NAME;
|
||||
ffOptionInitModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
bool ffParseBrightnessCommandOptions(FFBrightnessOptions* options, const char* key, const char* value)
|
||||
{
|
||||
const char* subKey = ffOptionTestPrefix(key, FF_BRIGHTNESS_MODULE_NAME);
|
||||
if (!subKey) return false;
|
||||
if (ffOptionParseModuleArgs(key, subKey, value, &options->moduleArgs))
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
void ffDestroyBrightnessOptions(FFBrightnessOptions* options)
|
||||
{
|
||||
ffOptionDestroyModuleArg(&options->moduleArgs);
|
||||
}
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
bool ffParseBrightnessJsonObject(FFinstance* instance, const char* type, JSONCData* data, json_object* module)
|
||||
{
|
||||
if (strcasecmp(type, FF_BRIGHTNESS_MODULE_NAME) != 0)
|
||||
return false;
|
||||
|
||||
FFBrightnessOptions __attribute__((__cleanup__(ffDestroyBrightnessOptions))) options;
|
||||
ffInitBrightnessOptions(&options);
|
||||
|
||||
if (module)
|
||||
{
|
||||
struct lh_entry* entry;
|
||||
lh_foreach(data->ffjson_object_get_object(module), entry)
|
||||
{
|
||||
const char* key = (const char *)lh_entry_k(entry);
|
||||
if (strcasecmp(key, "type") == 0)
|
||||
continue;
|
||||
json_object* val = (struct json_object *)lh_entry_v(entry);
|
||||
|
||||
if (ffJsonConfigParseModuleArgs(data, key, val, &options.moduleArgs))
|
||||
continue;
|
||||
|
||||
ffPrintError(instance, FF_BRIGHTNESS_MODULE_NAME, 0, &options.moduleArgs, "Unknown JSON key %s", key);
|
||||
}
|
||||
}
|
||||
|
||||
ffPrintBrightness(instance, &options);
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include "fastfetch.h"
|
||||
|
||||
void ffPrintBrightness(FFinstance* instance, FFBrightnessOptions* options);
|
||||
void ffInitBrightnessOptions(FFBrightnessOptions* options);
|
||||
bool ffParseBrightnessCommandOptions(FFBrightnessOptions* options, const char* key, const char* value);
|
||||
void ffDestroyBrightnessOptions(FFBrightnessOptions* options);
|
||||
|
||||
#ifdef FF_HAVE_JSONC
|
||||
#include "common/config.h"
|
||||
bool ffParseBrightnessJsonObject(FFinstance* instance, const char* type, JSONCData* data, 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 FFBrightnessOptions
|
||||
{
|
||||
const char* moduleName;
|
||||
FFModuleArgs moduleArgs;
|
||||
} FFBrightnessOptions;
|
||||
@@ -18,6 +18,7 @@ static inline bool parseModuleJsonObject(FFinstance* instance, const char* type,
|
||||
ffParseBluetoothJsonObject(instance, type, data, module) ||
|
||||
ffParseBoardJsonObject(instance, type, data, module) ||
|
||||
ffParseBreakJsonObject(instance, type, data, module) ||
|
||||
ffParseBrightnessJsonObject(instance, type, data, module) ||
|
||||
ffParseCommandJsonObject(instance, type, data, module) ||
|
||||
ffParseDateTimeJsonObject(instance, type, data, module) ||
|
||||
ffParseDisplayJsonObject(instance, type, data, module) ||
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "modules/battery/battery.h"
|
||||
#include "modules/bios/bios.h"
|
||||
#include "modules/bluetooth/bluetooth.h"
|
||||
#include "modules/brightness/brightness.h"
|
||||
#include "modules/board/board.h"
|
||||
#include "modules/break/break.h"
|
||||
#include "modules/command/command.h"
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
#include "modules/bios/option.h"
|
||||
#include "modules/bluetooth/option.h"
|
||||
#include "modules/board/option.h"
|
||||
#include "modules/brightness/option.h"
|
||||
#include "modules/command/option.h"
|
||||
#include "modules/datetime/option.h"
|
||||
#include "modules/display/option.h"
|
||||
|
||||
Reference in New Issue
Block a user