ddopson / underscore-cli

Command-line utility-belt for hacking JSON and Javascript.
Other
1.72k stars 83 forks source link

saving result of 'underscore pretty' to a file results in not valid JSON #39

Open tiurin opened 9 years ago

tiurin commented 9 years ago

Mac OS X 10.10 Forwarding underscore pretty output to a file (either with -o option or with > ) saves a file with color formatting signs being inserted, smth like: [32m, [33m, [39m along with some non-printables before each of them, which makes JSON not valid. Example:

{
  foo: [50193, 20500, 6648, 2],
  bar: "bar",
}

With underscore print everything works just perfect. I guess this behavior should be either documented or pretty should not be available for file forwarding as the only thing you can do with the file later is output it again to console with color highlighting.

blakemcbride commented 9 years ago

I am having the same problem and understand what is going on. "pretty" is a nicer format than "print" but "pretty" adds console color escape codes. What we need is the "pretty" format without the color codes. I suggest adding a "--nocolor" option. Alternatively, the following also works:

underscore pretty |sed -r 's/\x1B[([0-9]{1,2}(;[0-9]{1,2})?)?[mGK]//g'

tiurin commented 9 years ago

@blakemcbride Good point, but now I see there is already a standard --color option for colorising output. I would suggest that perhaps "no color" output should be default for pretty as being more universal and suitable for all output formats. I will investigate the code a more thoroughly later today and make a pull request.

themightychris commented 7 years ago

Even without color codes "pretty" won't be valid JSON because keys aren't quoted. The are other output formats for valid JSON that are indented