Skip to content

Commit 217f98a

Browse files
committed
Global: tidy json result formats
1 parent 1011e21 commit 217f98a

File tree

12 files changed

+52
-78
lines changed

12 files changed

+52
-78
lines changed

src/detection/battery/battery_linux.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,5 @@ const char* ffDetectBattery(FFBatteryOptions* options, FFlist* results)
170170
ffStrbufSubstrBefore(&baseDir, baseDirLength);
171171
}
172172

173-
if(results->length == 0)
174-
return "\"/sys/class/power_supply/\" doesn't contain any battery folder";
175-
176173
return NULL;
177174
}

src/modules/battery/battery.c

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,28 @@ void ffPrintBattery(FFBatteryOptions* options)
8484
if (error)
8585
{
8686
ffPrintError(FF_BATTERY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", error);
87+
return;
8788
}
88-
else
89+
if(results.length == 0)
8990
{
90-
for(uint8_t i = 0; i < (uint8_t) results.length; i++)
91-
{
92-
FFBatteryResult* result = ffListGet(&results, i);
93-
printBattery(options, result, i);
94-
95-
ffStrbufDestroy(&result->manufacturer);
96-
ffStrbufDestroy(&result->modelName);
97-
ffStrbufDestroy(&result->technology);
98-
ffStrbufDestroy(&result->status);
99-
ffStrbufDestroy(&result->serial);
100-
ffStrbufDestroy(&result->manufactureDate);
101-
}
102-
if(results.length == 0)
103-
ffPrintError(FF_BATTERY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "No batteries found");
91+
ffPrintError(FF_BATTERY_MODULE_NAME, 0, &options->moduleArgs, FF_PRINT_TYPE_DEFAULT, "%s", "No batteries found");
92+
return;
93+
}
94+
95+
for(uint32_t i = 0; i < results.length; i++)
96+
{
97+
FFBatteryResult* result = ffListGet(&results, i);
98+
printBattery(options, result, (uint8_t) i);
99+
}
100+
101+
FF_LIST_FOR_EACH(FFBatteryResult, result, results)
102+
{
103+
ffStrbufDestroy(&result->manufacturer);
104+
ffStrbufDestroy(&result->modelName);
105+
ffStrbufDestroy(&result->technology);
106+
ffStrbufDestroy(&result->status);
107+
ffStrbufDestroy(&result->serial);
108+
ffStrbufDestroy(&result->manufactureDate);
104109
}
105110
}
106111

src/modules/bluetooth/bluetooth.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -150,19 +150,17 @@ void ffGenerateBluetoothJsonResult(FF_MAYBE_UNUSED FFBluetoothOptions* options,
150150
yyjson_mut_obj_add_str(doc, module, "error", error);
151151
return;
152152
}
153-
else
154-
{
155-
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
156153

157-
FF_LIST_FOR_EACH(FFBluetoothResult, item, results)
158-
{
159-
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
160-
yyjson_mut_obj_add_strbuf(doc, obj, "address", &item->address);
161-
yyjson_mut_obj_add_uint(doc, obj, "battery", item->battery);
162-
yyjson_mut_obj_add_bool(doc, obj, "connected", item->connected);
163-
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
164-
yyjson_mut_obj_add_strbuf(doc, obj, "type", &item->type);
165-
}
154+
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
155+
156+
FF_LIST_FOR_EACH(FFBluetoothResult, item, results)
157+
{
158+
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
159+
yyjson_mut_obj_add_strbuf(doc, obj, "address", &item->address);
160+
yyjson_mut_obj_add_uint(doc, obj, "battery", item->battery);
161+
yyjson_mut_obj_add_bool(doc, obj, "connected", item->connected);
162+
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
163+
yyjson_mut_obj_add_strbuf(doc, obj, "type", &item->type);
166164
}
167165

168166
FF_LIST_FOR_EACH(FFBluetoothResult, device, results)

src/modules/brightness/brightness.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,6 @@ void ffGenerateBrightnessJsonResult(FF_MAYBE_UNUSED FFBrightnessOptions* options
154154
return;
155155
}
156156

157-
if(result.length == 0)
158-
{
159-
yyjson_mut_obj_add_str(doc, module, "error", "No result is detected.");
160-
return;
161-
}
162-
163157
yyjson_mut_val* arr = yyjson_mut_arr(doc);
164158
yyjson_mut_obj_add_val(doc, module, "result", arr);
165159

src/modules/datetime/datetime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ void ffGenerateDateTimeJsonConfig(FFDateTimeOptions* options, yyjson_mut_doc* do
152152

153153
void ffGenerateDateTimeJsonResult(FF_MAYBE_UNUSED FFDateTimeOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module)
154154
{
155-
yyjson_mut_obj_add_uint(doc, module, "result", ffTimeGetNow());
155+
yyjson_mut_obj_add_strcpy(doc, module, "result", ffTimeToFullStr(ffTimeGetNow()));
156156
}
157157

158158
void ffPrintDateTimeHelpFormat(void)

src/modules/display/display.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ void ffGenerateDisplayJsonResult(FF_MAYBE_UNUSED FFDisplayOptions* options, yyjs
279279
yyjson_mut_obj_add_str(doc, module, "error", "Couldn't detect display");
280280
return;
281281
}
282+
282283
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
283284
FF_LIST_FOR_EACH(FFDisplayResult, item, dsResult->displays)
284285
{

src/modules/gamepad/gamepad.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,12 +118,6 @@ void ffGenerateGamepadJsonResult(FF_MAYBE_UNUSED FFGamepadOptions* options, yyjs
118118
return;
119119
}
120120

121-
if(!result.length)
122-
{
123-
yyjson_mut_obj_add_str(doc, module, "error", "No devices detected");
124-
return;
125-
}
126-
127121
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
128122
FF_LIST_FOR_EACH(FFGamepadDevice, device, result)
129123
{

src/modules/localip/localip.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,13 +334,7 @@ void ffGenerateLocalIpJsonResult(FF_MAYBE_UNUSED FFLocalIpOptions* options, yyjs
334334
if(error)
335335
{
336336
yyjson_mut_obj_add_str(doc, module, "error", error);
337-
goto exit;
338-
}
339-
340-
if(results.length == 0)
341-
{
342-
yyjson_mut_obj_add_str(doc, module, "error", "Failed to detect any IPs");
343-
goto exit;
337+
return;
344338
}
345339

346340
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
@@ -354,7 +348,6 @@ void ffGenerateLocalIpJsonResult(FF_MAYBE_UNUSED FFLocalIpOptions* options, yyjs
354348
yyjson_mut_obj_add_strbuf(doc, obj, "name", &ip->name);
355349
}
356350

357-
exit:
358351
FF_LIST_FOR_EACH(FFLocalIpResult, ip, results)
359352
{
360353
ffStrbufDestroy(&ip->name);

src/modules/poweradapter/poweradapter.c

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -101,24 +101,28 @@ void ffGeneratePowerAdapterJsonResult(FF_MAYBE_UNUSED FFPowerAdapterOptions* opt
101101
if (error)
102102
{
103103
yyjson_mut_obj_add_str(doc, module, "error", error);
104+
return;
104105
}
105-
else if(results.length == 0)
106+
107+
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
108+
FF_LIST_FOR_EACH(FFPowerAdapterResult, item, results)
106109
{
107-
yyjson_mut_obj_add_str(doc, module, "error", "No power adapters found");
110+
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
111+
yyjson_mut_obj_add_strbuf(doc, obj, "description", &item->description);
112+
yyjson_mut_obj_add_strbuf(doc, obj, "manufacturer", &item->manufacturer);
113+
yyjson_mut_obj_add_strbuf(doc, obj, "modelName", &item->modelName);
114+
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
115+
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &item->serial);
116+
yyjson_mut_obj_add_int(doc, obj, "watts", item->watts);
108117
}
109-
else
118+
119+
FF_LIST_FOR_EACH(FFPowerAdapterResult, item, results)
110120
{
111-
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
112-
FF_LIST_FOR_EACH(FFPowerAdapterResult, item, results)
113-
{
114-
yyjson_mut_val* obj = yyjson_mut_arr_add_obj(doc, arr);
115-
yyjson_mut_obj_add_strbuf(doc, obj, "description", &item->description);
116-
yyjson_mut_obj_add_strbuf(doc, obj, "manufacturer", &item->manufacturer);
117-
yyjson_mut_obj_add_strbuf(doc, obj, "modelName", &item->modelName);
118-
yyjson_mut_obj_add_strbuf(doc, obj, "name", &item->name);
119-
yyjson_mut_obj_add_strbuf(doc, obj, "serial", &item->serial);
120-
yyjson_mut_obj_add_int(doc, obj, "watts", item->watts);
121-
}
121+
ffStrbufDestroy(&item->manufacturer);
122+
ffStrbufDestroy(&item->description);
123+
ffStrbufDestroy(&item->modelName);
124+
ffStrbufDestroy(&item->name);
125+
ffStrbufDestroy(&item->serial);
122126
}
123127
}
124128

src/modules/sound/sound.c

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,6 @@ void ffGenerateSoundJsonResult(FF_MAYBE_UNUSED FFSoundOptions* options, yyjson_m
200200
return;
201201
}
202202

203-
if(result.length == 0)
204-
{
205-
yyjson_mut_obj_add_str(doc, module, "error", "No active sound devices found");
206-
return;
207-
}
208-
209203
yyjson_mut_val* arr = yyjson_mut_obj_add_arr(doc, module, "result");
210204
FF_LIST_FOR_EACH(FFSoundDevice, item, result)
211205
{

0 commit comments

Comments
 (0)