ymattw / ydiff

View colored, incremental diff in workspace or from stdin, side by side and auto paged.
Other
877 stars 62 forks source link

Customize colors #80

Closed net closed 5 months ago

net commented 6 years ago

Is there a way to customize the specific colors (foreground and background) ydiff uses?

ymattw commented 5 years ago

Unfortunately I do not have plan to support that. I intentionally want to keep the tool simple enough. But I can imagine there are different flavors of color schema, so I am open to accept contribution :).

ElectricRCAircraftGuy commented 3 years ago

@ymattw Can you point out in the code where the colors are set?

This could easily be done by having a config file that ydiff reads from, but for any configuration settings not set, or if the config file is missing entirely, it will just use some defaults. That's what I would do.

plgruener commented 3 years ago

I may be able to help out here:

@ymattw Can you point out in the code where the colors are set?

The color definition is not exactly hard to find if you try just a little bit: https://github.com/ymattw/ydiff/blob/ce8f7a11a4eb27558173a1a7620adef21784eadb/ydiff.py#L43-L60

These are ANSI escape codes, and are sort of the least common denominator for encoding colors across terminal applications.

ydiff uses the 3/4bit ones, which essentially mean there are 16 basic named colors (in order: black, red, green, yellow, blue, magenta, cyan, white; and their respective bright- variants), which exact RGB-value is set by your terminal. So if you just wanted a lighter/darker shade of red, adjust your terminal accordingly. If you wanted magenta/cyan instead of red/green, you would just change the ANSI escape code in that color-definition above.

There are two other modes: 8bit (256 colors) and 24bit (full RGB, 32M colors). Many terminal today do have "truecolor" support, but some don't and never will (basic xterm has not, for example).
And doing truecolor-support the right way is hard. For your "simple" suggestion, you'd have to have to do:

  1. a custom config file for ydiff
  2. a custom DSL for defining colors, either as 4bit, 8bit or 24bit ones (RGB,RGB-A,HSV,…?)
  3. parsing of this color-DSL and generating the appropriate ANSI escape codes. Sure there are probably libraries for that, but that's another dependency.
  4. check if the terminal you're outputting to does support 256/24bit colors or not, and add fallbacks. This is the hardest step, because there currently is just no reliable way to do that. You can find a great writeup here: https://gist.github.com/XVilka/8346728

All in all probably too much hassle for a tool that just displays two colors.