我想检测系统控制台是否支持来自 Java 应用程序的 ANSI 颜色代码(以及哪些颜色代码),而不依赖于 native 代码(这意味着我无法与 ncurses 交互)。
是否有一种简单可靠的方法将 $TERM
映射到控制台支持的颜色代码列表?
我的应用程序真的需要支持 http://invisible-island.net/ncurses/terminfo.src.html 中找到的数百个 terminfo 代码吗?或者是否有一个我应该检查的最小列表(例如 xterm
、xterm-color
和 xterm-256color
)?
更新:我在 http://tldp.org/HOWTO/Text-Terminal-HOWTO-16.html 找到了 terminfo 的精彩概述。
最佳答案
这里有两种不同且互补的方法:
- 您希望您的系统能够在任何可能的终端上运行(包括旧的真实终端,例如 televideo-tvi950 的 dec-vt100)=> 使用 [n]curses 构建一些东西。它是为这种用途而开发的:从真实终端抽象程序
- 您希望您的系统适用于一种单一终端类型(例如 xterm-color)并依赖模拟器来实现它。您只需对
TERM
环境变量xterm-color
进行测试,或者将选项传递给程序以尝试使用颜色或不使用颜色。
这两种情况现在都是可以接受的,事实上您可以找到实现它们的 Java 库,例如 Java Curses对于前者或 Lanterna对于后者(更多引用here)
您应该在文档中简单地说明这一点。像这样的东西:
This application requires an xterm compatible terminal. If the
TERM
environment variable isxterm-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 theTERM
environment variable to the best possible emulation. Colors are supported by application if present inTERMINFO
database.
关于linux - 将 TERM 映射到支持的 ANSI 颜色代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39032411/