skuzzle / snapshot-tests

Convenient snapshot testing for Java
https://skuzzle.github.io/snapshot-tests/reference/latest/
MIT License
12 stars 0 forks source link

Use ansi color codes when rendering unified text diff #13

Open skuzzle opened 2 years ago

skuzzle commented 2 years ago

We could use ANSI color codes to improve dispaly of the unified diff in console windows

skuzzle commented 2 years ago

First test did not prove so effective.

  1. Most environments (CI, IDE) do not render the text colorized but render the control chars instead
  2. We'd need to decide on which environments ANSI code should be rendered and we'd need to determine on which environment we're running
  3. Even when colors are rendered in a console, the diff mostly looks shitty

Usage of

<dependency>
    <groupId>com.diogonunes</groupId>
    <artifactId>JColor</artifactId>
    <version>5.2.0</version>
</dependency>

Is as simple as:

    public String getDisplayDiff(Diff diff) {
        if (isFailureDifference(diff)) {
            switch (diff.operation) {
            case DELETE:
                return Ansi.colorize("-" + einklammern(diff.text), Attribute.BOLD(), Attribute.BLACK_TEXT(),
                        Attribute.YELLOW_BACK());
            case INSERT:
                return Ansi.colorize("+" + einklammern(diff.text), Attribute.BOLD(), Attribute.BLACK_TEXT(),
                        Attribute.YELLOW_BACK());
            default:
                throw new IllegalStateException();
            }
        } else {
            return diff.text;
        }
    }
skuzzle commented 1 year ago

Coming back to this after a while, this might still be a nice feature to have. Now that diff creation and rendering has been extracted into its own module we could provide some configuration options for colorized diff rendering