Gili Gili - 1 year ago 66
Linux Question

Mapping TERM to supported ANSI color codes?

I'd like to detect whether the system console supports ANSI color codes (and which ones) from a Java application without relying on native code (meaning, I cannot interact with ncurses).

Is there an easy and reliable way to map

to the list of color codes that the console supports?

Does my application really need to support the hundreds of terminfo codes found at or is there a minimal list that I should be checking for instead (e.g.

UPDATE: I found an excellent overview of terminfo at

Answer Source

There are 2 different and complementary approaches here:

  • you want your system to work on any possible terminal (including old real terminals like dec-vt100 of televideo-tvi950) => build something using [n]curses. It has been developped for that usage: abstract programs from the real terminal
  • you want your system to work for one single terminal type (say xterm-color) and rely on emulators to implement it. You simply could have a test on TERM environment variable being xterm-color or an option passed to the program to try to use colors or not.

Both cases are nowadays acceptable, and in fact you can find Java libraries implementing them, like Java Curses for the former or Lanterna for the latter (more references here)

You should simply state it clearly in your documentation. Something like:

This application requires an xterm compatible terminal. If the TERM environment variable is xterm-color or if this parameter is passed of that system property is present it will use colors.

(substiture this and that with relevant options...)


This application is based on the terminfo system. Be sure to set the the TERM environment variable to the best possible emulation. Colors are supported by application if present in TERMINFO database.