josephburnett / jd

JSON diff and patch
MIT License
826 stars 38 forks source link

add color to output diff #47

Closed neutralino closed 2 years ago

neutralino commented 2 years ago

Basic implementation of #3 . Excuse my poor Go skills.

Example: Screen Shot 2022-06-15 at 9 16 50 AM

josephburnett commented 2 years ago

Thanks for the contribution @neutralino! I'll take this as-is and afterward move it into the render function with a flag.

josephburnett commented 2 years ago

Here I put the color rendering behind a flag: https://github.com/josephburnett/jd/commit/6ee04750b5a38b317948d92ee028cb0bae94fc36

A few things to note here:

  1. Default behavior doesn't change now. This is important in case someone is parsing the output of jd.
  2. By emitting color in the Render function we don't need to parse the diff to add color.
  3. This ^^^ keeps the main function more clean and provides the same functionality to library users. E.g. those importing jd/lib instead of using main.go.
  4. The flag is added to Render as a list of options which can be emitted (the ... thing). This preserves backward compatibility because previously written code with no options still works. And forwards compatibility because more options can be added later without breaking anything.