Closed dblevins closed 3 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.
@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));
@dialex JColor doesn't work.
Can you be more precise regarding what doesn't work, ie. actual vs expected behaviour? Feel free to open an issue.
This is implemented in JLine too using the AttributedCharSequence
and related classes (AttributedString
, AttributedStringBuilder
, etc...)
I'm going to back port the JLine classes to Jansi with additional support for TrueColors terminals.
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.
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);
}
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