From a4fababb71dfd01843f6157294d3a070fa1b0aed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Thu, 31 Aug 2023 10:38:11 +0800 Subject: [PATCH] Cursor: add JSON format support --- src/modules/cursor/cursor.c | 23 ++++++++++++++++++++++- src/modules/cursor/cursor.h | 1 + 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/modules/cursor/cursor.c b/src/modules/cursor/cursor.c index 5a0143df7..945f44b72 100644 --- a/src/modules/cursor/cursor.c +++ b/src/modules/cursor/cursor.c @@ -52,7 +52,7 @@ void ffPrintCursor(FFCursorOptions* options) void ffInitCursorOptions(FFCursorOptions* options) { - ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_CURSOR_MODULE_NAME, ffParseCursorCommandOptions, ffParseCursorJsonObject, ffPrintCursor, NULL); + ffOptionInitModuleBaseInfo(&options->moduleInfo, FF_CURSOR_MODULE_NAME, ffParseCursorCommandOptions, ffParseCursorJsonObject, ffPrintCursor, ffGenerateCursorJson); ffOptionInitModuleArg(&options->moduleArgs); } @@ -87,3 +87,24 @@ void ffParseCursorJsonObject(FFCursorOptions* options, yyjson_val* module) ffPrintError(FF_CURSOR_MODULE_NAME, 0, &options->moduleArgs, "Unknown JSON key %s", key); } } + +void ffGenerateCursorJson(FF_MAYBE_UNUSED FFCursorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module) +{ + FFCursorResult result; + ffStrbufInit(&result.error); + ffStrbufInit(&result.theme); + ffStrbufInit(&result.size); + + ffDetectCursor(&result); + + if (result.error.length) + { + yyjson_mut_obj_add_str(doc, module, "error", result.error.chars); + return; + } + + yyjson_mut_val* obj = yyjson_mut_obj(doc); + yyjson_mut_obj_add_val(doc, module, "result", obj); + yyjson_mut_obj_add_strbuf(doc, obj, "theme", &result.theme); + yyjson_mut_obj_add_strbuf(doc, obj, "size", &result.size); +} diff --git a/src/modules/cursor/cursor.h b/src/modules/cursor/cursor.h index ff849b547..02ed47e8f 100644 --- a/src/modules/cursor/cursor.h +++ b/src/modules/cursor/cursor.h @@ -9,3 +9,4 @@ void ffInitCursorOptions(FFCursorOptions* options); bool ffParseCursorCommandOptions(FFCursorOptions* options, const char* key, const char* value); void ffDestroyCursorOptions(FFCursorOptions* options); void ffParseCursorJsonObject(FFCursorOptions* options, yyjson_val* module); +void ffGenerateCursorJson(FFCursorOptions* options, yyjson_mut_doc* doc, yyjson_mut_val* module);