libfyaml icon indicating copy to clipboard operation
libfyaml copied to clipboard

Preferred defaults for `fy-tool -m dejson`

Open ingydotnet opened this issue 2 years ago • 3 comments

First off dejson is a bad name for this. How about yamlfmt, yfmt,, tidy or something? JSON has nothing to do with this.

So here are some changes to the default emitter format I'd like to see...

$ echo $'foo:\n- bar' | fy-tool -m yamlfmt -
foo:
- bar
$ echo $'foo:\n- "bar\\nbaz\\nboom\\n"' | fy-tool -m yamlfmt -
---
foo:
- |
  bar
  baz
  boom

Do those for starters.

In general @perlpunk's yamlpp-load-dump --preserve 0 does a good job for this kind of formatting. You can install with cpanm YAML::PP.

ingydotnet avatar Dec 22 '21 20:12 ingydotnet

Here's some general heuristics:

  • Use plain if possible
  • For multiline with printable chars, use literal.
  • For things that need quoting but don't need any escapes, use single quotes
  • For thing that need escapes use double quotes
  • I can't think of a situation where you can chose folded as the best choice, but maybe there is.

fy-tool already seems to fold plain scalars. yaml-pp-load doesn't. I think that folding is nice, so keep that.

$ echo $'foo:\n- " x "' | fy-tool -m dejson -
foo:
  - " x "

I'd like to see single quotes not double quotes. also you don't seem to fold quoted scalars like you do for plain. That's a shame. :)

ingydotnet avatar Dec 22 '21 20:12 ingydotnet

Also yamlpp-load-dump --preserve 0 emits --- at start. This is preferred to state (not too loudly) that this is YAML.

Also start top level scalars on the --- line

$ echo $'foo\nbar' | yamlpp-load-dump --preserve 0
--- foo bar

ingydotnet avatar Dec 22 '21 20:12 ingydotnet

Addressed most of these with the 21747066c6684f3b09c60c72fe5dda537706dc82 and de49581ad0cf52c103f9c21f0bffe7e76ff03d05 commits.

fy-tool now accepts -mpretty (or -myamlfmt as an alias).

I'm not so gung-ho to make single quotes as the default for scalars that can't be plain. I'm an old C person and when I see single quotes my lizard brain goes to char mode...

pantoniou avatar Jan 07 '22 10:01 pantoniou