JTerminal documentation Help

Text-Styling

In the terminal there are certain text attributes for each cell that can be set. These include the foreground/background color and text fonts.

All functions in this topic support non-native based terminal instances!

TerminalColor

The TerminalColor class reflects a color for the foreground und background. There is also a return method to call the respective Ansi-code that is responsible for changing the text attributes.

TerminalColor.BLACK
TerminalColor.DARK_RED
TerminalColor.DARK_GREEN
TerminalColor.DARK_YELLOW
TerminalColor.DARK_BLUE
TerminalColor.DARK_PURPLE
TerminalColor.DARK_AQUA
TerminalColor.GRAY
TerminalColor.DARK_GRAY
TerminalColor.RED
TerminalColor.GREEN
TerminalColor.YELLOW
TerminalColor.BLUE
TerminalColor.PURPLE
TerminalColor.AQUA
TerminalColor.WHITE

TerminalColor.DEFAULT represents the default color for the foreground and background, which is set as the default color in the terminal application.

A color can also be generated by the RGB color model but may not be displayed in many terminals. The colors from xterm-256color can be used for safe resolutions. This color palette offers 256 possible colors that can be displayed in most terminals.

Create rgb color: TerminalColor.from(r,g,b) TerminalColor.from(hex)

Convert a color to xterm 256color: TerminalColor#asXtermColor()

256color map

Xterm-256color generating:
XtermColor.getNearestTo(AWTColor)
XtermColor.getColor(id:byte)

Text-Font

The TextFont enum list all font types:

fonts

With TextFont.getDefaultFont() an Ansi-Code is returned that resets the font attributes.

TextStyle class

The TextStyle class contains all attributes to describe a certain text style.

TextStyle textStyle = TextStyle.create(foreground_color, background_color, fonts);

Example code using a mixture of several styles:

Terminal terminal = Terminal.auto(); //foreground: green; background: null; font: underline TextStyle textStyle = TextStyle.create(TerminalColor.GREEN, null, TextFont.UNDERLINE); //foreground: null; background: yellow; font: ignored TextStyle textStyleExt = TextStyle.create(null, TerminalColor.YELLOW); TextStyle combinedStyle = Combiner.combine(textStyleExt, textStyle); String styleAnsiCode = AnsiCodeSerializer.DEFAULT.serialize(combinedStyle); String resetAnsiCode = AnsiCodeSerializer.DEFAULT.serialize(TextStyle.getDefault()); terminal.writeLine("\nResult: "); terminal.writeLine(styleAnsiCode + "This is a combined text style" + resetAnsiCode);
console output

When using the combiner class, all attributes can be overwritten if the attributes of the higher prioritized style object do not equal to null.

//foreground: green; background: yellow; font: underline TextStyle textStyle = TextStyle.create(TerminalColor.GREEN, TerminalColor.YELLOW, TextFont.UNDERLINE); //foreground: null; background: default; font: all unset TextStyle textStyleExt = TextStyle.create(null, TerminalColor.DEFAULT, FontMap.mapOfAll(FontOption.UNSET)); TextStyle combinedStyle = Combiner.combine(textStyleExt, textStyle); TerminalBuffer buffer = new TerminalBuffer() .setStyle(combinedStyle) .append("This is a combined text style") .resetStyle(); terminal.writeLine("\nResult:"); terminal.writeLine(buffer);
console output

TermString class

The TermString class contains a string with the associated style data and simplifies the use of terminal-based strings.

TermString termString = TermString.builder() .append("No Style String\n") .foregroundColor(TerminalColor.RED) .backgroundColor(TerminalColor.YELLOW) .append("First String\n") .backgroundColor(null) .append("Second String\n") .fontsSet(TextFont.UNDERLINE, TextFont.ITALIC) .append("Third String\n") .fontsUnset(TextFont.UNDERLINE) .append("Fourth String\n") .resetStyle() .build(); terminal.writeLine("\nResult:"); terminal.writeLine(termString);
console output
TermString replacement = TermString.builder() .foregroundColor(TerminalColor.GREEN) .append("Text") .build(); termString = termString.replace("String", replacement); terminal.writeLine("\nResult:"); terminal.writeLine(termString);
console output
Last modified: 01 Juni 2025