Skip to content

Commit d477321

Browse files
committed
Fastfetch: adds support for reading JSON config from stdin
1 parent 9ebff9f commit d477321

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ Changes:
44
* Keys in JSON configuration files are now case-sensitive, as stated in v2.49.0
55
* This is a breaking change, but it should not affect most users as long as your config file passes JSON schema validation.
66
* All module config flags have been removed, as stated in v2.49.0
7+
* To configure modules at command line, `echo '{"modules": [{"type":"custom","format":"Hello Fastfetch!"}]}' | fastfetch -c -` should be used instead.
78
* Percent bar config `display.bar.*` options have been replaced with a more organized, nested object structure.
89
* `display.bar.charElapsed` has been renamed to `display.bar.char.elapsed`.
910
* `display.bar.charTotal` has been renamed to `display.bar.char.total`.
@@ -13,6 +14,7 @@ Changes:
1314
* `--config` or `-c` should be used instead.
1415

1516
Features:
17+
* Added support for reading JSON config from stdin using `--config -` or `-c -`
1618
* Added `display.bar.border.{leftElapsed,rightElapsed}` for using border as parts of bar content. (#1875)
1719
* `display.bar.border: null` has been added as a shorthand to disable bar borders.
1820
* Added `display.bar.color.{elapsed,total,border}` to customize the color of the elapsed, total and border sections of the percent bar.

src/fastfetch.c

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -382,16 +382,25 @@ static bool parseJsoncFile(const char* path, bool strictJson)
382382

383383
{
384384
yyjson_read_err error;
385-
instance.state.configDoc = yyjson_read_file(path, strictJson ? 0 : YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS, NULL, &error);
385+
yyjson_read_flag flg = strictJson ? 0 : YYJSON_READ_ALLOW_COMMENTS | YYJSON_READ_ALLOW_TRAILING_COMMAS;
386+
instance.state.configDoc = path
387+
? yyjson_read_file(path, flg, NULL, &error)
388+
: yyjson_read_fp(stdin, flg, NULL, &error);
386389
if (!instance.state.configDoc)
387390
{
388391
if (error.code != YYJSON_READ_ERROR_FILE_OPEN)
389392
{
390-
size_t row = 0, col = error.pos;
391-
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
392-
if (ffAppendFileBuffer(path, &content))
393-
yyjson_locate_pos(content.chars, content.length, error.pos, &row, &col, NULL);
394-
fprintf(stderr, "Error: failed to parse JSON config file `%s` at (%zu, %zu): %s\n", path, row, col, error.msg);
393+
if (path)
394+
{
395+
size_t row = 0, col = error.pos;
396+
FF_STRBUF_AUTO_DESTROY content = ffStrbufCreate();
397+
if (ffAppendFileBuffer(path, &content))
398+
yyjson_locate_pos(content.chars, content.length, error.pos, &row, &col, NULL);
399+
fprintf(stderr, "Error: failed to parse JSON config file `%s` at (%zu, %zu): %s\n", path, row, col, error.msg);
400+
}
401+
else
402+
fprintf(stderr, "Error: failed to parse JSON from stdin at %zu: %s\n", error.pos, error.msg);
403+
395404
exit(477);
396405
}
397406
return false;
@@ -461,6 +470,12 @@ static void optionParseConfigFile(FFdata* data, const char* key, const char* val
461470
if (value[0] == '\0' || ffStrEqualsIgnCase(value, "none"))
462471
return;
463472

473+
if (value[0] == '-' && value[1] == '\0')
474+
{
475+
parseJsoncFile(NULL, false);
476+
return;
477+
}
478+
464479
//Try to load as an absolute path
465480

466481
FF_STRBUF_AUTO_DESTROY absolutePath = ffStrbufCreateS(value);

0 commit comments

Comments
 (0)