dariober dariober - 3 months ago 15
Java Question

Set terminal background color with Java

I have a Java program that uses JLine2 to handle input and output on the console terminal. For aesthetic reasons this program works best if the background color of the terminal is white.

So, Is it possible to set the background color of the terminal from within java? Ideally using JLine, but I don't mind other solutions as long as they portable and not overly complex.

This is what I would like to do:

ConsoleReader console= new ConsoleReader();
console.getTerminal().setBackgroundColor("WHITE") // <- Ideally!


EDIT

I don't think the question Highlighting text in commandline java is relevant. I don't want to set the background colour of the printed characters, rather I want to set the background colour of the terminal screen. I could print a bunch of blank spaces with white background and then print on top of that but it seems a very clumsy solution to me.

For example, from this (black background):

enter image description here

I want to this (white background):

enter image description here

Thank you!

Dario

Answer

I would try with escape codes.

White background is \e[107m, Black foreground \e[30m, i.e. something like the following leads to black on white:

echo -e "\e[107m\e[30mTest String"

In Java you would a System.out.println("\\e[107m\\e[30m") during startup of the application.

See List of ANSI color escape sequences and http://misc.flogisoft.com/bash/tip_colors_and_formatting for more details

Comments