underscore-cli icon indicating copy to clipboard operation
underscore-cli copied to clipboard

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

Open tiurin opened this issue 10 years ago • 3 comments

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: [[33m50193[39m, [33m20500[39m, [33m6648[39m, [33m2[39m],
  bar: [32m"bar"[39m,
}

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.

tiurin avatar Jan 24 '15 12:01 tiurin

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'

blakemcbride avatar Apr 10 '15 21:04 blakemcbride

@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.

tiurin avatar Apr 11 '15 10:04 tiurin

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

themightychris avatar Jan 06 '17 14:01 themightychris