-1

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 $TERM to the list of color codes that the console supports?

Does my application really need to support the hundreds of terminfo codes found at http://invisible-island.net/ncurses/terminfo.src.html or is there a minimal list that I should be checking for instead (e.g. xterm, xterm-color and xterm-256color)?

UPDATE: I found an excellent overview of terminfo at http://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html

Gili
  • 86,244
  • 97
  • 390
  • 689

1 Answers1

1

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...)

Alternatively:

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.

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • Very helpful answer. So, if I understand you correctly, for the emulation approach users would run 3rd-party shells such as ConEmu on Windows to gain access to the particular terminal type(s) I require. 1. Can you please give specific examples of terminal emulators on Linux and OSX to make sure I understand you correctly? 2. If I wanted to go for the first approach, how would I find out what terminal types are supported by modern systems (e.g. Windows, OSX, and mainstream Linux distributions)? – Gili Aug 19 '16 at 14:46
  • @Gili: well xterm natively supports xterm emulations on any XWindow enabled system, including Linux. On Windows, the excellent [putty](http://www.chiark.greenend.org.uk/~sgtatham/putty/) allows to connect to a ssh server and offers (among others) xterm emulation. I have never used osx, but I think xterm exists there too. – Serge Ballesta Aug 19 '16 at 14:59
  • 1. Are there alternatives to xterm on Linux? 2. What about my second question? :) – Gili Aug 19 '16 at 15:04
  • @Gili:1/ I'm not a specialist for alternatives to xterm, but major XWindow desktops come with their own, konsole for kde, gconsole (I think) for gnome, and so on... 2/ AFAIK there is no terminfo native support on Windows, but I think you can find console emulators in msys or cygwin. No idea for OSX (never used), but all major Linux or BSD distrib support the full TERMINFO database. The hard part would be to find a working televideo console except in museums... – Serge Ballesta Aug 19 '16 at 15:13