fusesource / jansi

Jansi is a small java library that allows you to use ANSI escape sequences to format your console output which works even on windows.
http://fusesource.github.io/jansi/
Apache License 2.0
1.11k stars 140 forks source link

Support for 256 color terminals #131

Closed dblevins closed 3 years ago

dblevins commented 6 years ago

The terminal on OSX supports 256 colors. It would be awesome if Jansi could know when to strip them out and when to leave them.

Try this on an OSX terminal

#!/bin/bash

cat > /tmp/termcolors.java <<EOF
public class termcolors {
    public static void main(String[] args) {
        for (int i = 0; i < 256; i++) {
            System.out.printf("\033[38;5;%sm#@%03d", i, i);
            System.out.print("  ");

            if (i % 12 == 0) System.out.println();
        }
        System.out.println();
    }
}
EOF
javac /tmp/termcolors.java &&
java -cp /tmp termcolors

cat <<EOF

#!/bin/bash
N=202
echo -e "\033[38;5;\${N}mTry Me\033[0m"
EOF
monitorjbl commented 5 years ago

IMO, this would be best served by allowing users to at least extend the existing colors. Right now, the Color enum is the roadblock to this; you can't extend an enum in Java and the fg()/bg() functions only accept the enum so the stock set of colors is all you get.

It would be pretty trivial to switch that over to an interface that Color can then implement. Users could then define custom colors for terminals that support it.

dialex commented 4 years ago

@dblevins Sorry it took 2 years, but if you're willing to switch from JAnsi to JColor, then you will be happy to know we support not only 256 colors (8-bit) but the whole RGB range. It was fixed by https://github.com/dialex/JColor/issues/27 and just released (v5.0.0).

The syntax looks like this:

import static com.diogonunes.jcolor.Ansi.colorize;
import static com.diogonunes.jcolor.Attribute.*;

// Use Case 6: we support 8-bit colors
System.out.print(colorize("We support 8-bit colors (0-255)", TEXT_COLOR(225));

// Use Case 7: we support TrueColors (RGB)
System.out.print(colorize("We support 8-bit colors (0-255)", TEXT_COLOR(15, 251, 23));
alexandrage commented 3 years ago

@dialex JColor doesn't work.

dialex commented 3 years ago

Can you be more precise regarding what doesn't work, ie. actual vs expected behaviour? Feel free to open an issue.

gnodet commented 3 years ago

This is implemented in JLine too using the AttributedCharSequence and related classes (AttributedString, AttributedStringBuilder, etc...)

gnodet commented 3 years ago

I'm going to back port the JLine classes to Jansi with additional support for TrueColors terminals.

gnodet commented 3 years ago

256-color and truecolor support has been added to Jansi 2.1.0. I haven't backported the AttributedCharSequence from JLine, as they are better located in that project, however the Ansi class has added methods for specifying 256 or 24-bit colors. They are rounded to the nearest color in the palette.

terheyden commented 1 year ago

Works great, here's a test method for folks:

public static void testJAnsi() {

    Ansi ansi = Ansi.ansi();

    for (int index = 0; index < 256; index++) {
        ansi.fg(index).a("FG %d ".formatted(index));
    }

    AnsiConsole.out().println(ansi);

    ansi = Ansi.ansi();

    for (int index = 0; index < 256; index++) {
        ansi.bg(index).a("BG %d ".formatted(index));
    }

    AnsiConsole.out().println(ansi);
}