将 TERM 映射到支持的 ANSI 颜色代码?

Mapping TERM to supported ANSI color codes?

我想检测系统控制台是否支持来自 Java 应用程序的 ANSI 颜色代码(以及哪些)而不依赖本机代码(意思是,我无法与 ncurses 交互)。

是否有简单可靠的方法将 $TERM 映射到控制台支持的颜色代码列表?

我的应用程序是否真的需要支持 http://invisible-island.net/ncurses/terminfo.src.html 中的数百个 terminfo 代码,或者是否有一个我应该检查的最小列表(例如 xtermxterm-colorxterm-256color)?

更新:我在 http://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html

找到了关于术语信息的精彩概述

这里有两种不同且互补的方法:

  • 您希望您的系统在 任何 可能的终端上工作(包括旧的真实终端,如 televideo-tvi950 的 dec-vt100)=> 使用 [n]curses 构建一些东西。它是为这种用途开发的:来自真实终端的抽象程序
  • 您希望您的系统适用于一种终端类型(比如 xterm-color)并依赖模拟器来实现它。您可以简单地对 TERM 环境变量进行测试 xterm-color 或传递给程序的选项以尝试使用颜色。

这两种情况现在都可以接受,事实上你可以找到 Java 实现它们的库,比如 Java Curses for the former or Lanterna for the latter (more references here)

您应该在文档中清楚地说明它。类似于:

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.

(用相关选项替换这个和那个...)

或者:

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.