Fastfetch: adds support for reading JSON config from stdin

This commit is contained in:
李通洲
2025-08-14 10:50:39 +08:00
parent 9ebff9fd22
commit d4773212b7
2 changed files with 23 additions and 6 deletions
+2
View File
@@ -4,6 +4,7 @@ Changes:
* Keys in JSON configuration files are now case-sensitive, as stated in v2.49.0
* This is a breaking change, but it should not affect most users as long as your config file passes JSON schema validation.
* All module config flags have been removed, as stated in v2.49.0
* To configure modules at command line, `echo '{"modules": [{"type":"custom","format":"Hello Fastfetch!"}]}' | fastfetch -c -` should be used instead.
* Percent bar config `display.bar.*` options have been replaced with a more organized, nested object structure.
* `display.bar.charElapsed` has been renamed to `display.bar.char.elapsed`.
* `display.bar.charTotal` has been renamed to `display.bar.char.total`.
@@ -13,6 +14,7 @@ Changes:
* `--config` or `-c` should be used instead.
Features:
* Added support for reading JSON config from stdin using `--config -` or `-c -`
* Added `display.bar.border.{leftElapsed,rightElapsed}` for using border as parts of bar content. (#1875)
* `display.bar.border: null` has been added as a shorthand to disable bar borders.
* Added `display.bar.color.{elapsed,total,border}` to customize the color of the elapsed, total and border sections of the percent bar.
+21 -6
View File
@@ -382,16 +382,25 @@ static bool parseJsoncFile(const char* path, bool strictJson)
{
yyjson_read_err error;
instance.state.configDoc = yyjson_read_file(path, strictJson ? 0 : YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS, NULL, &error);
yyjson_read_flag flg = strictJson ? 0 : YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
instance.state.configDoc = path
? yyjson_read_file(path, flg, NULL, &error)
: yyjson_read_fp(stdin, flg, NULL, &error);
if (!instance.state.configDoc)
{
if (error.code != YYJSON_READ_ERROR_FILE_OPEN)
{
size_t row = 0, col = error.pos;
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
if (ffAppendFileBuffer(path, &content))
yyjson_locate_pos(content.chars, content.length, error.pos, &row, &col, NULL);
fprintf(stderr, "Error: failed to parse JSON config file `%s` at (%zu, %zu): %s\n", path, row, col, error.msg);
if (path)
{
size_t row = 0, col = error.pos;
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
if (ffAppendFileBuffer(path, &content))
yyjson_locate_pos(content.chars, content.length, error.pos, &row, &col, NULL);
fprintf(stderr, "Error: failed to parse JSON config file `%s` at (%zu, %zu): %s\n", path, row, col, error.msg);
}
else
fprintf(stderr, "Error: failed to parse JSON from stdin at %zu: %s\n", error.pos, error.msg);
exit(477);
}
return false;
@@ -461,6 +470,12 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
if (value[0] == '\0' || ffStrEqualsIgnCase(value, "none"))
return;
if (value[0] == '-' && value[1] == '\0')
{
parseJsoncFile(NULL, false);
return;
}
//Try to load as an absolute path
FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateS(value);