Skip to content

Commit 9a36d63

Browse files
committed
Global: refactor code of -h <module>-format
1 parent e45ff54 commit 9a36d63

File tree

78 files changed

+1454
-1872
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

78 files changed

+1454
-1872
lines changed

src/common/format.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,5 @@ typedef struct FFformatarg
4242

4343
void ffFormatAppendFormatArg(FFstrbuf* buffer, const FFformatarg* formatarg);
4444
void ffParseFormatString(FFstrbuf* buffer, const FFstrbuf* formatstr, uint32_t numArgs, const FFformatarg* arguments);
45-
#define FF_PARSE_FORMAT_STRING_CHECKED(buffer, formatstr, numArgs, arguments) do {\
46-
static_assert(sizeof(arguments) / sizeof(*arguments) == (numArgs), "Invalid number of format arguments");\
47-
ffParseFormatString((buffer), (formatstr), (numArgs), (arguments));\
48-
} while (0)
45+
#define FF_PARSE_FORMAT_STRING_CHECKED(buffer, formatstr, arguments) \
46+
ffParseFormatString((buffer), (formatstr), sizeof(arguments) / sizeof(*arguments), (arguments));

src/common/option.h

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,20 @@ struct yyjson_val;
66
struct yyjson_mut_doc;
77
struct yyjson_mut_val;
88

9+
typedef struct FFModuleFormatArg
10+
{
11+
const char* desc;
12+
const char* name;
13+
} FFModuleFormatArg;
14+
15+
typedef struct FFModuleFormatArgList
16+
{
17+
FFModuleFormatArg* args;
18+
uint32_t count;
19+
} FFModuleFormatArgList;
20+
21+
#define FF_FORMAT_ARG_LIST(list) { .args = list, .count = sizeof(list) / sizeof(FFModuleFormatArg) }
22+
923
// Must be the first field of FFModuleOptions
1024
typedef struct FFModuleBaseInfo
1125
{
@@ -19,32 +33,10 @@ typedef struct FFModuleBaseInfo
1933
void (*parseJsonObject)(void* options, struct yyjson_val *module);
2034
void (*printModule)(void* options);
2135
void (*generateJsonResult)(void* options, struct yyjson_mut_doc* doc, struct yyjson_mut_val* module);
22-
void (*printHelpFormat)(void);
2336
void (*generateJsonConfig)(void* options, struct yyjson_mut_doc* doc, struct yyjson_mut_val* obj);
37+
FFModuleFormatArgList formatArgs;
2438
} FFModuleBaseInfo;
2539

26-
static inline void ffOptionInitModuleBaseInfo(
27-
FFModuleBaseInfo* baseInfo,
28-
const char* name,
29-
const char* description,
30-
void* parseCommandOptions, // bool (*const parseCommandOptions)(void* options, const char* key, const char* value)
31-
void* parseJsonObject, // void (*const parseJsonObject)(void* options, yyjson_val *module)
32-
void* printModule, // void (*const printModule)(void* options)
33-
void* generateJsonResult, // void (*const generateJsonResult)(void* options, yyjson_mut_doc* doc, yyjson_mut_val* obj)
34-
void (*printHelpFormat)(void),
35-
void* generateJsonConfig // void (*const generateJsonConfig)(void* options, yyjson_mut_doc* doc, yyjson_mut_val* obj)
36-
)
37-
{
38-
baseInfo->name = name;
39-
baseInfo->description = description;
40-
baseInfo->parseCommandOptions = (__typeof__(baseInfo->parseCommandOptions)) parseCommandOptions;
41-
baseInfo->parseJsonObject = (__typeof__(baseInfo->parseJsonObject)) parseJsonObject;
42-
baseInfo->printModule = (__typeof__(baseInfo->printModule)) printModule;
43-
baseInfo->generateJsonResult = (__typeof__(baseInfo->generateJsonResult)) generateJsonResult;
44-
baseInfo->printHelpFormat = printHelpFormat;
45-
baseInfo->generateJsonConfig = (__typeof__(baseInfo->generateJsonConfig)) generateJsonConfig;
46-
}
47-
4840
typedef enum __attribute__((__packed__)) FFModuleKeyType
4941
{
5042
FF_MODULE_KEY_TYPE_NONE = 0,

src/common/printing.c

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void ffPrintLogoAndKey(const char* moduleName, uint8_t moduleIndex, const FFModu
5050
else
5151
{
5252
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
53-
FF_PARSE_FORMAT_STRING_CHECKED(&key, &moduleArgs->key, 2, ((FFformatarg[]){
53+
FF_PARSE_FORMAT_STRING_CHECKED(&key, &moduleArgs->key, ((FFformatarg[]) {
5454
FF_FORMAT_ARG(moduleIndex, "index"),
5555
FF_FORMAT_ARG(moduleArgs->keyIcon, "icon"),
5656
}));
@@ -148,18 +148,3 @@ void ffPrintCharTimes(char c, uint32_t times)
148148
if(remaining > 0)
149149
fwrite(str, 1, remaining, stdout);
150150
}
151-
152-
void ffPrintModuleFormatHelp(const char* name, const char* def, uint32_t numArgs, const char* args[])
153-
{
154-
FF_STRBUF_AUTO_DESTROY buffer = ffStrbufCreateS(name);
155-
ffStrbufLowerCase(&buffer);
156-
printf("--%s-format:\n", buffer.chars);
157-
printf("Sets the format string for %s output.\n", name);
158-
puts("To see how a format string is constructed, take a look at \"fastfetch --help format\".");
159-
puts("The following values are passed:");
160-
161-
for(unsigned i = 0; i < numArgs; i++)
162-
printf(" {%u}: %s\n", i + 1, args[i]);
163-
164-
printf("The default is something similar to \"%s\".\n", def);
165-
}

src/common/printing.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,8 @@ typedef enum __attribute__((__packed__)) FFPrintType {
1414

1515
void ffPrintLogoAndKey(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType);
1616
void ffPrintFormat(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType, uint32_t numArgs, const FFformatarg* arguments);
17-
#define FF_PRINT_FORMAT_CHECKED(moduleName, moduleIndex, moduleArgs, printType, numArgs, arguments) do {\
18-
static_assert(sizeof(arguments) / sizeof(*arguments) == (numArgs), "Invalid number of format arguments");\
19-
ffPrintFormat((moduleName), (moduleIndex), (moduleArgs), (printType), (numArgs), (arguments));\
20-
} while (0)
17+
#define FF_PRINT_FORMAT_CHECKED(moduleName, moduleIndex, moduleArgs, printType, arguments) \
18+
ffPrintFormat((moduleName), (moduleIndex), (moduleArgs), (printType), (sizeof(arguments) / sizeof(*arguments)), (arguments));
2119
FF_C_PRINTF(5, 6) void ffPrintError(const char* moduleName, uint8_t moduleIndex, const FFModuleArgs* moduleArgs, FFPrintType printType, const char* message, ...);
2220
void ffPrintColor(const FFstrbuf* colorValue);
2321
void ffPrintCharTimes(char c, uint32_t times);
24-
25-
void ffPrintModuleFormatHelp(const char* name, const char* def, uint32_t numArgs, const char* args[]);
26-
#define FF_PRINT_MODULE_FORMAT_HELP_CHECKED(moduleName, def, numArgs, args) do {\
27-
static_assert(sizeof(args) / sizeof(*args) == (numArgs), "Invalid number of format arguments");\
28-
ffPrintModuleFormatHelp((moduleName), (def), (numArgs), (args));\
29-
} while (0)

src/fastfetch.c

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "common/commandoption.h"
33
#include "common/io/io.h"
44
#include "common/jsonconfig.h"
5+
#include "common/printing.h"
56
#include "detection/version/version.h"
67
#include "util/stringUtils.h"
78
#include "util/mallocHelper.h"
@@ -18,13 +19,25 @@
1819
static void printCommandFormatHelp(const char* command)
1920
{
2021
FF_STRBUF_AUTO_DESTROY type = ffStrbufCreateNS((uint32_t) (strlen(command) - strlen("-format")), command);
22+
ffStrbufLowerCase(&type);
2123
for (FFModuleBaseInfo** modules = ffModuleInfos[toupper(command[0]) - 'A']; *modules; ++modules)
2224
{
2325
FFModuleBaseInfo* baseInfo = *modules;
2426
if (ffStrbufIgnCaseEqualS(&type, baseInfo->name))
2527
{
26-
if (baseInfo->printHelpFormat)
27-
baseInfo->printHelpFormat();
28+
if (baseInfo->formatArgs.count > 0)
29+
{
30+
printf("--%s-format:\n", type.chars);
31+
printf("Sets the format string for %s output.\n", baseInfo->name);
32+
puts("To see how a format string is constructed, take a look at \"fastfetch --help format\".");
33+
puts("The following values are passed:");
34+
35+
for (unsigned i = 0; i < baseInfo->formatArgs.count; i++)
36+
{
37+
const FFModuleFormatArg* arg = &baseInfo->formatArgs.args[i];
38+
printf("%16s {%u}: %s\n", arg->name, i + 1, arg->desc);
39+
}
40+
}
2841
else
2942
fprintf(stderr, "Error: Module '%s' doesn't support output formatting\n", baseInfo->name);
3043
return;

src/modules/battery/battery.c

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
#include "modules/battery/battery.h"
88
#include "util/stringUtils.h"
99

10-
#define FF_BATTERY_NUM_FORMAT_ARGS 14
11-
1210
static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uint8_t index)
1311
{
1412
FF_STRBUF_AUTO_DESTROY key = ffStrbufCreate();
@@ -22,7 +20,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
2220
else
2321
{
2422
ffStrbufClear(&key);
25-
FF_PARSE_FORMAT_STRING_CHECKED(&key, &options->moduleArgs.key, 2, ((FFformatarg[]){
23+
FF_PARSE_FORMAT_STRING_CHECKED(&key, &options->moduleArgs.key, ((FFformatarg[]) {
2624
FF_FORMAT_ARG(index, "index"),
2725
FF_FORMAT_ARG(result->modelName, "name"),
2826
}));
@@ -105,7 +103,7 @@ static void printBattery(FFBatteryOptions* options, FFBatteryResult* result, uin
105103
FF_STRBUF_AUTO_DESTROY tempStr = ffStrbufCreate();
106104
ffTempsAppendNum(result->temperature, &tempStr, options->tempConfig, &options->moduleArgs);
107105

108-
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_BATTERY_NUM_FORMAT_ARGS, ((FFformatarg[]) {
106+
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
109107
FF_FORMAT_ARG(result->manufacturer, "manufacturer"),
110108
FF_FORMAT_ARG(result->modelName, "model-name"),
111109
FF_FORMAT_ARG(result->technology, "technology"),
@@ -270,39 +268,35 @@ void ffGenerateBatteryJsonResult(FFBatteryOptions* options, yyjson_mut_doc* doc,
270268
}
271269
}
272270

273-
void ffPrintBatteryHelpFormat(void)
274-
{
275-
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_BATTERY_MODULE_NAME, "{4} ({12} hours {13} mins) [{5}]", FF_BATTERY_NUM_FORMAT_ARGS, ((const char* []) {
276-
"Battery manufacturer - manufacturer",
277-
"Battery model name - model-name",
278-
"Battery technology - technology",
279-
"Battery capacity (percentage num) - capacity",
280-
"Battery status - status",
281-
"Battery temperature (formatted) - temperature",
282-
"Battery cycle count - cycle-count",
283-
"Battery serial number - serial",
284-
"Battery manufactor date - manufacture-date",
285-
"Battery capacity (percentage bar) - capacity-bar",
286-
"Battery time remaining days - time-days",
287-
"Battery time remaining hours - time-hours",
288-
"Battery time remaining minutes - time-minutes",
289-
"Battery time remaining seconds - time-seconds",
290-
}));
291-
}
271+
static FFModuleBaseInfo ffModuleInfo = {
272+
.name = FF_BATTERY_MODULE_NAME,
273+
.description = "Print battery capacity, status, etc",
274+
.parseCommandOptions = (void*) ffParseBatteryCommandOptions,
275+
.parseJsonObject = (void*) ffParseBatteryJsonObject,
276+
.printModule = (void*) ffPrintBattery,
277+
.generateJsonResult = (void*) ffGenerateBatteryJsonResult,
278+
.generateJsonConfig = (void*) ffGenerateBatteryJsonConfig,
279+
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
280+
{"Battery manufacturer", "manufacturer"},
281+
{"Battery model name", "model-name"},
282+
{"Battery technology", "technology"},
283+
{"Battery capacity (percentage num)", "capacity"},
284+
{"Battery status", "status"},
285+
{"Battery temperature (formatted)", "temperature"},
286+
{"Battery cycle count", "cycle-count"},
287+
{"Battery serial number", "serial"},
288+
{"Battery manufactor date", "manufacture-date"},
289+
{"Battery capacity (percentage bar)", "capacity-bar"},
290+
{"Battery time remaining days", "time-days"},
291+
{"Battery time remaining hours", "time-hours"},
292+
{"Battery time remaining minutes", "time-minutes"},
293+
{"Battery time remaining seconds", "time-seconds"},
294+
}))
295+
};
292296

293297
void ffInitBatteryOptions(FFBatteryOptions* options)
294298
{
295-
ffOptionInitModuleBaseInfo(
296-
&options->moduleInfo,
297-
FF_BATTERY_MODULE_NAME,
298-
"Print battery capacity, status, etc",
299-
ffParseBatteryCommandOptions,
300-
ffParseBatteryJsonObject,
301-
ffPrintBattery,
302-
ffGenerateBatteryJsonResult,
303-
ffPrintBatteryHelpFormat,
304-
ffGenerateBatteryJsonConfig
305-
);
299+
options->moduleInfo = ffModuleInfo;
306300
ffOptionInitModuleArg(&options->moduleArgs, "");
307301
options->temp = false;
308302
options->tempConfig = (FFColorRangeConfig) { 60, 80 };

src/modules/bios/bios.c

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@
44
#include "modules/bios/bios.h"
55
#include "util/stringUtils.h"
66

7-
#define FF_BIOS_NUM_FORMAT_ARGS 5
8-
97
void ffPrintBios(FFBiosOptions* options)
108
{
119
FFBiosResult bios;
@@ -43,7 +41,7 @@ void ffPrintBios(FFBiosOptions* options)
4341
else
4442
{
4543
ffStrbufClear(&key);
46-
FF_PARSE_FORMAT_STRING_CHECKED(&key, &options->moduleArgs.key, 2, ((FFformatarg[]){
44+
FF_PARSE_FORMAT_STRING_CHECKED(&key, &options->moduleArgs.key, ((FFformatarg[]) {
4745
FF_FORMAT_ARG(bios.type, "type"),
4846
FF_FORMAT_ARG(options->moduleArgs.keyIcon, "icon"),
4947
}));
@@ -60,7 +58,7 @@ void ffPrintBios(FFBiosOptions* options)
6058
}
6159
else
6260
{
63-
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, FF_BIOS_NUM_FORMAT_ARGS, ((FFformatarg[]) {
61+
FF_PRINT_FORMAT_CHECKED(key.chars, 0, &options->moduleArgs, FF_PRINT_TYPE_NO_CUSTOM_KEY, ((FFformatarg[]) {
6462
FF_FORMAT_ARG(bios.date, "date"),
6563
FF_FORMAT_ARG(bios.release, "release"),
6664
FF_FORMAT_ARG(bios.vendor, "vendor"),
@@ -144,30 +142,26 @@ void ffGenerateBiosJsonResult(FF_MAYBE_UNUSED FFBiosOptions* options, yyjson_mut
144142
ffStrbufDestroy(&bios.type);
145143
}
146144

147-
void ffPrintBiosHelpFormat(void)
148-
{
149-
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_BIOS_MODULE_NAME, "{4} ({2})", FF_BIOS_NUM_FORMAT_ARGS, ((const char* []) {
150-
"bios date - date",
151-
"bios release - release",
152-
"bios vendor - vendor",
153-
"bios version - version",
154-
"firmware type - type",
155-
}));
156-
}
145+
static FFModuleBaseInfo ffModuleInfo = {
146+
.name = FF_BIOS_MODULE_NAME,
147+
.description = "Print information of 1st-stage bootloader (name, version, release date, etc)",
148+
.parseCommandOptions = (void*) ffParseBiosCommandOptions,
149+
.parseJsonObject = (void*) ffParseBiosJsonObject,
150+
.printModule = (void*) ffPrintBios,
151+
.generateJsonResult = (void*) ffGenerateBiosJsonResult,
152+
.generateJsonConfig = (void*) ffGenerateBiosJsonConfig,
153+
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
154+
{"Bios date", "date"},
155+
{"Bios release", "release"},
156+
{"Bios vendor", "vendor"},
157+
{"Bios version", "version"},
158+
{"Firmware type", "type"},
159+
}))
160+
};
157161

158162
void ffInitBiosOptions(FFBiosOptions* options)
159163
{
160-
ffOptionInitModuleBaseInfo(
161-
&options->moduleInfo,
162-
FF_BIOS_MODULE_NAME,
163-
"Print information of 1st-stage bootloader (name, version, release date, etc)",
164-
ffParseBiosCommandOptions,
165-
ffParseBiosJsonObject,
166-
ffPrintBios,
167-
ffGenerateBiosJsonResult,
168-
ffPrintBiosHelpFormat,
169-
ffGenerateBiosJsonConfig
170-
);
164+
options->moduleInfo = ffModuleInfo;
171165
ffOptionInitModuleArg(&options->moduleArgs, "");
172166
}
173167

src/modules/bluetooth/bluetooth.c

Lines changed: 19 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
#include "modules/bluetooth/bluetooth.h"
66
#include "util/stringUtils.h"
77

8-
#define FF_BLUETOOTH_NUM_FORMAT_ARGS 6
9-
108
static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* device, uint8_t index)
119
{
1210
FFPercentageTypeFlags percentType = options->percent.type == 0 ? instance.config.display.percentType : options->percent.type;
@@ -47,7 +45,7 @@ static void printDevice(FFBluetoothOptions* options, const FFBluetoothResult* de
4745
if(percentType & FF_PERCENTAGE_TYPE_BAR_BIT)
4846
ffPercentAppendBar(&percentageBar, device->battery, options->percent, &options->moduleArgs);
4947

50-
FF_PRINT_FORMAT_CHECKED(FF_BLUETOOTH_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, FF_BLUETOOTH_NUM_FORMAT_ARGS, ((FFformatarg[]) {
48+
FF_PRINT_FORMAT_CHECKED(FF_BLUETOOTH_MODULE_NAME, index, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, ((FFformatarg[]) {
5149
FF_FORMAT_ARG(device->name, "name"),
5250
FF_FORMAT_ARG(device->address, "address"),
5351
FF_FORMAT_ARG(device->type, "type"),
@@ -188,31 +186,27 @@ void ffGenerateBluetoothJsonResult(FF_MAYBE_UNUSED FFBluetoothOptions* options,
188186
}
189187
}
190188

191-
void ffPrintBluetoothHelpFormat(void)
192-
{
193-
FF_PRINT_MODULE_FORMAT_HELP_CHECKED(FF_BLUETOOTH_MODULE_NAME, "{1} ({4})", FF_BLUETOOTH_NUM_FORMAT_ARGS, ((const char* []) {
194-
"Name - device name",
195-
"Address - remote device address",
196-
"Type - type",
197-
"Battery percentage number - battery-percentage",
198-
"Is connected - connected",
199-
"Battery percentage bar - battery-percentage-bar",
200-
}));
201-
}
189+
static FFModuleBaseInfo ffModuleInfo = {
190+
.name = FF_BLUETOOTH_MODULE_NAME,
191+
.description = "List (connected) bluetooth devices",
192+
.parseCommandOptions = (void*) ffParseBluetoothCommandOptions,
193+
.parseJsonObject = (void*) ffParseBluetoothJsonObject,
194+
.printModule = (void*) ffPrintBluetooth,
195+
.generateJsonResult = (void*) ffGenerateBluetoothJsonResult,
196+
.generateJsonConfig = (void*) ffGenerateBluetoothJsonConfig,
197+
.formatArgs = FF_FORMAT_ARG_LIST(((FFModuleFormatArg[]) {
198+
{"Name", "name"},
199+
{"Address", "address"},
200+
{"Type", "type"},
201+
{"Battery percentage number", "battery-percentage"},
202+
{"Is connected", "connected"},
203+
{"Battery percentage bar", "battery-percentage-bar"},
204+
}))
205+
};
202206

203207
void ffInitBluetoothOptions(FFBluetoothOptions* options)
204208
{
205-
ffOptionInitModuleBaseInfo(
206-
&options->moduleInfo,
207-
FF_BLUETOOTH_MODULE_NAME,
208-
"List (connected) bluetooth devices",
209-
ffParseBluetoothCommandOptions,
210-
ffParseBluetoothJsonObject,
211-
ffPrintBluetooth,
212-
ffGenerateBluetoothJsonResult,
213-
ffPrintBluetoothHelpFormat,
214-
ffGenerateBluetoothJsonConfig
215-
);
209+
options->moduleInfo = ffModuleInfo;
216210
ffOptionInitModuleArg(&options->moduleArgs, "");
217211
options->showDisconnected = false;
218212
options->percent = (FFPercentageModuleConfig) { 50, 20, 0 };

0 commit comments

Comments
 (0)