From aa45e70910b180235eb4cbdde5103a6459e22913 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E9=80=9A=E6=B4=B2?= Date: Sun, 7 Apr 2024 20:41:01 +0800 Subject: [PATCH] Revert "fastfetch: add the missing new-line in config files (#772)" Fix #778 This reverts commit 4430b7145ebca48a6607133ebf58f3d8a7285211. --- src/fastfetch.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/src/fastfetch.c b/src/fastfetch.c index 20f316d0e..0109c2717 100644 --- a/src/fastfetch.c +++ b/src/fastfetch.c @@ -821,7 +821,6 @@ static void run(FFdata* data) if (instance.state.resultDoc) { yyjson_mut_write_fp(stdout, instance.state.resultDoc, YYJSON_WRITE_INF_AND_NAN_AS_NULL | YYJSON_WRITE_PRETTY_TWO_SPACES, NULL, NULL); - //TODO should use YYJSON_WRITE_NEWLINE_AT_END when it is available putchar('\n'); } else @@ -841,23 +840,24 @@ static void writeConfigFile(FFdata* data, const FFstrbuf* filename) ffOptionsGenerateLibraryJsonConfig(&instance.config.library, doc); ffMigrateCommandOptionToJsonc(data, doc); - FILE *fp = stdout; - bool writeToStdout = ffStrbufEqualS(filename, "-"); - if (!writeToStdout) - fp = fopen(filename->chars, "w"); - - bool ok = yyjson_mut_write_fp(fp, doc, YYJSON_WRITE_INF_AND_NAN_AS_NULL | YYJSON_WRITE_PRETTY_TWO_SPACES, NULL, NULL); - //TODO should use YYJSON_WRITE_NEWLINE_AT_END when it is available - fputc('\n', fp); - if (!ok) + if (ffStrbufEqualS(filename, "-")) + yyjson_mut_write_fp(stdout, doc, YYJSON_WRITE_INF_AND_NAN_AS_NULL | YYJSON_WRITE_PRETTY_TWO_SPACES, NULL, NULL); + else { - fprintf(stderr, "Error: failed to generate config in `%s`\n", writeToStdout ? "stdout" : filename->chars); - exit(1); - } - if (ok && !writeToStdout) - { - fclose(fp); - printf("The generated config file has been written in `%s`\n", filename->chars); + size_t len; + FF_AUTO_FREE const char* str = yyjson_mut_write(doc, YYJSON_WRITE_INF_AND_NAN_AS_NULL | YYJSON_WRITE_PRETTY_TWO_SPACES, &len); + if (!str) + { + printf("Error: failed to generate config file\n"); + exit(1); + } + if (ffWriteFileData(filename->chars, len, str)) + printf("The generated config file has been written in `%s`\n", filename->chars); + else + { + printf("Error: failed to write file in `%s`\n", filename->chars); + exit(1); + } } yyjson_mut_doc_free(doc);