mirror of
https://github.com/fastfetch-cli/fastfetch.git
synced 2026-11-02 11:23:41 +01:00
Display: init support of JSON config
This commit is contained in:
@@ -23,4 +23,39 @@ bool ffJsonConfigParseModuleArgs(JSONCData* data, const char* key, json_object*
|
||||
return false;
|
||||
}
|
||||
|
||||
const char* ffJsonConfigParseEnum(JSONCData* data, json_object* val, int* result, FFKeyValuePair pairs[])
|
||||
{
|
||||
if (data->ffjson_object_is_type(val, json_type_int))
|
||||
{
|
||||
int intVal = data->ffjson_object_get_int(val);
|
||||
|
||||
for (const FFKeyValuePair* pPair = pairs; pPair->key; ++pPair)
|
||||
{
|
||||
if (intVal == pPair->value)
|
||||
{
|
||||
*result = pPair->value;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return "Invalid enum integer";
|
||||
}
|
||||
else if (data->ffjson_object_is_type(val, json_type_string))
|
||||
{
|
||||
const char* strVal = data->ffjson_object_get_string(val);
|
||||
for (const FFKeyValuePair* pPair = pairs; pPair->key; ++pPair)
|
||||
{
|
||||
if (strcasecmp(strVal, pPair->key) == 0)
|
||||
{
|
||||
*result = pPair->value;
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
return "Invalid enum string";
|
||||
}
|
||||
else
|
||||
return "Invalid enum value type; must be a string or integer";
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
@@ -13,6 +13,7 @@ typedef struct JSONCData
|
||||
FF_LIBRARY_SYMBOL(json_object_get_array)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_boolean)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_double)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_int)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_string_len)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_string)
|
||||
FF_LIBRARY_SYMBOL(json_object_get_object)
|
||||
@@ -23,5 +24,6 @@ typedef struct JSONCData
|
||||
} JSONCData;
|
||||
|
||||
bool ffJsonConfigParseModuleArgs(JSONCData* data, const char* key, json_object* val, FFModuleArgs* moduleArgs);
|
||||
const char* ffJsonConfigParseEnum(JSONCData* data, json_object* val, int* result, FFKeyValuePair pairs[]);
|
||||
|
||||
#endif
|
||||
|
||||
+2
-6
@@ -85,7 +85,7 @@ static void defaultConfig(FFinstance* instance)
|
||||
initModuleArg(&instance->config.processes);
|
||||
initModuleArg(&instance->config.packages);
|
||||
initModuleArg(&instance->config.shell);
|
||||
initModuleArg(&instance->config.display);
|
||||
ffInitDisplayOptions(&instance->config.display);
|
||||
initModuleArg(&instance->config.de);
|
||||
initModuleArg(&instance->config.wm);
|
||||
initModuleArg(&instance->config.wmTheme);
|
||||
@@ -160,10 +160,6 @@ static void defaultConfig(FFinstance* instance)
|
||||
ffStrbufInitA(&instance->config.diskFolders, 0);
|
||||
instance->config.diskShowTypes = FF_DISK_TYPE_REGULAR_BIT | FF_DISK_TYPE_EXTERNAL_BIT;
|
||||
|
||||
instance->config.displayCompactType = FF_DISPLAY_COMPACT_TYPE_NONE;
|
||||
instance->config.displayDetectName = false;
|
||||
instance->config.displayPreciseRefreshRate = false;
|
||||
|
||||
instance->config.bluetoothShowDisconnected = false;
|
||||
|
||||
instance->config.soundType = FF_SOUND_TYPE_MAIN;
|
||||
@@ -328,7 +324,7 @@ static void destroyConfig(FFinstance* instance)
|
||||
destroyModuleArg(&instance->config.processes);
|
||||
destroyModuleArg(&instance->config.packages);
|
||||
destroyModuleArg(&instance->config.shell);
|
||||
destroyModuleArg(&instance->config.display);
|
||||
ffDestroyDisplayOptions(&instance->config.display);
|
||||
destroyModuleArg(&instance->config.de);
|
||||
destroyModuleArg(&instance->config.wm);
|
||||
destroyModuleArg(&instance->config.wmTheme);
|
||||
|
||||
+4
-19
@@ -72,7 +72,7 @@ uint32_t ffOptionParseUInt32(const char* argumentKey, const char* value)
|
||||
return num;
|
||||
}
|
||||
|
||||
void ffOptionParseEnum(const char* argumentKey, const char* requestedKey, void* result, ...)
|
||||
int ffOptionParseEnum(const char* argumentKey, const char* requestedKey, FFKeyValuePair pairs[])
|
||||
{
|
||||
if(requestedKey == NULL)
|
||||
{
|
||||
@@ -80,27 +80,12 @@ void ffOptionParseEnum(const char* argumentKey, const char* requestedKey, void*
|
||||
exit(476);
|
||||
}
|
||||
|
||||
va_list args;
|
||||
va_start(args, result);
|
||||
|
||||
while(true)
|
||||
for (const FFKeyValuePair* pPair = pairs; pPair->key; ++pPair)
|
||||
{
|
||||
const char* key = va_arg(args, const char*);
|
||||
if(key == NULL)
|
||||
break;
|
||||
|
||||
int value = va_arg(args, int); //C standard guarantees that enumeration constants are presented as ints
|
||||
|
||||
if(strcasecmp(requestedKey, key) == 0)
|
||||
{
|
||||
*(int*)result = value;
|
||||
va_end(args);
|
||||
return;
|
||||
}
|
||||
if(strcasecmp(requestedKey, pPair->key) == 0)
|
||||
return pPair->value;
|
||||
}
|
||||
|
||||
va_end(args);
|
||||
|
||||
fprintf(stderr, "Error: unknown %s value: %s\n", argumentKey, requestedKey);
|
||||
exit(478);
|
||||
}
|
||||
|
||||
+9
-3
@@ -9,11 +9,17 @@ typedef struct FFModuleArgs
|
||||
FFstrbuf errorFormat;
|
||||
} FFModuleArgs;
|
||||
|
||||
typedef struct FFKeyValuePair
|
||||
{
|
||||
const char* key;
|
||||
int value;
|
||||
} FFKeyValuePair;
|
||||
|
||||
const char* ffOptionTestPrefix(const char* argumentKey, const char* moduleName);
|
||||
bool ffOptionParseModuleArgs(const char* argumentKey, const char* pkey, const char* value, FFModuleArgs* result);
|
||||
void ffOptionParseString(const char* argumentKey, const char* value, FFstrbuf* buffer);
|
||||
uint32_t ffOptionParseUInt32(const char* argumentKey, const char* value);
|
||||
void ffOptionParseEnum(const char* argumentKey, const char* requestedKey, void* result, ...);
|
||||
bool ffOptionParseBoolean(const char* str);
|
||||
FF_C_NODISCARD uint32_t ffOptionParseUInt32(const char* argumentKey, const char* value);
|
||||
FF_C_NODISCARD int ffOptionParseEnum(const char* argumentKey, const char* requestedKey, FFKeyValuePair pairs[]);
|
||||
FF_C_NODISCARD bool ffOptionParseBoolean(const char* str);
|
||||
void ffOptionInitModuleArg(FFModuleArgs* args);
|
||||
void ffOptionDestroyModuleArg(FFModuleArgs* args);
|
||||
|
||||
Reference in New Issue
Block a user