Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parameterize color schemes #41

Closed
2 tasks
alysbrooks opened this issue Mar 24, 2023 · 7 comments
Closed
2 tasks

Parameterize color schemes #41

alysbrooks opened this issue Mar 24, 2023 · 7 comments
Assignees

Comments

@alysbrooks
Copy link
Member

Currently, colors are hard-coded, but there are various situations where you might want to set different colors:

  • To ensure adequate contrast on a different background
  • To ensure readability by people who are colorblind
  • To match your editor or main diff tool's color scheme.

In addition to general usefulness, parameterizing color schemes may address #14 and would be required for lambdaisland/kaocha#403.

  • Allow for multiple built-in schemes: at least dark and light, and possibly a colorblind friendly scheme (if we can't make the dark and light schemes adequately colorblind) or a "classic" scheme if we end up tweaking colors.
  • Allow for customization of the scheme
@humorless
Copy link
Contributor

Hi @alysbrooks ,

From what I know about deep-diff2, the color is mostly controlled by here.
For example, when green is chosen for a string "hello", it outputs \u001b[32m hello \u001b[0m. Here, green color is replaced to color digit 32 through the sgr-code hashmap.

How you do think about the following implementation?

  • I implement a new namespace under color directory, like eight-bits-color.clj
  • Similar to ansi.clj, but support 256 colors.
  • Similar to ansi.clj, but it accept the user to inject a new map to replace sgr-code.

@humorless
Copy link
Contributor

@alysbrooks @plexus

I made some experiments. Actually, I can change the color to even 256 color without changing any single line of code.

For example, if I change the :lambdaisland.deep-diff2.printer-impl/deletion from [:red] to [:bg-256 5 13], the color code it outputs will change from \u001b[31m to \u001b[48;5;13m

Screen shot
截圖 2023-04-06 下午5 25 45

user=> (use 'lambdaisland.deep-diff2)
nil
user=> (def color-printer (printer {:color-scheme {:lambdaisland.deep-diff2.printer-impl/deletion [:bg-256 5 13]}}))
#'user/color-printer
user=> (pretty-print (diff {:a 1} {:b 2}) color-printer)
{+:b 2, -:a 1}

@humorless
Copy link
Contributor

Oh, after reading related issues more carefully, I find out that there are ansi.clj can controll more than I thought.

@plexus
Copy link
Member

plexus commented Apr 6, 2023

So it seems that Puget already allows customization through a custom printer? In that case I think there's not much more that needs to happen here, and it's more a matter of exposing this configuration in Kaocha's tests.edn.

Note that not all terminals support full RGB colors, although it's relatively widely supported these days. The downside of RGB colors is that, because they are absolute, you basically have to also set the background, because you can't predict what contrast you'll get. Whereas with the basic 16(?) colors it's up to the terminal how they render that exactly, so the terminal's color scheme should ensure that there's a sensible amount of contrast.

Which kind of goes back to the original issue, since we use these basic built-in colors ("red", "green"), rather than fully specified RGB colors, it's really up to the terminal color scheme to make sure they render ok on the default background.

@alysbrooks
Copy link
Member Author

alysbrooks commented Apr 7, 2023

Reading through the code and this discussion, I think we should take the following steps:

  1. Document how to set up a custom printer
  2. Optionally, create a helper function to make it easier to set custom named colors or RGB colors. lambdaisland.deep-diff2.printer-impl/puget-printer isn't that complicated, but there's quite a bit of set up that's orthogonal. It would be nice if you could do (deep-diff/custom-printer {:deleted :red :inserted [100 250 0]}), for example.
  3. Expose configuration in Kaocha.

I'm not sure my idea for color schemes really makes sense for deep-diff anymore. Perhaps a library or community extension would be more appropriate for that sort of thing. I think it would make sense in Kaocha (perhaps as a plugin). The one feature that might make sense in deep-diff2 is a fallback option, where we set the foreground colors and background colors to ensure good contrast so people can have something readable if their terminal color scheme isn't ideal or has gotten messed up somehow.

I think @humorless and I could split these among ourselves, unless you have time/interest, @plexus?

@plexus
Copy link
Member

plexus commented Apr 10, 2023

Sounds good, you can go for it.

@lread
Copy link
Contributor

lread commented May 30, 2023

This is a nice compromise for #14, thanks @alysbrooks and @humorless! I just used the updated docs to change deletion and insertion highlighting which made it much easier for me to see diffs in a large data structure. ❤️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

4 participants