在命令行中突出显示文本 java

Highlighting text in commandline java

我有一项任务是重新创建 unix cal 程序,除了一部分之外相当简单。在当天,它会突出显示该数字。我不知道该怎么做。知道如何在 Java 中做到这一点吗?

图片:

ANSI color codes

The color of the prompt is set by expanding the escape sequence “\e[sm”, where s is a semicolon-delimited list of ANSI color codes: “\e[31;44;1m” would set the foreground color to red, the background to blue, and the font in bold face; (The “\e” is the ASCII Escape character. Don’t forget to terminate the sequence with the “m” character.)

Binary sequences in the environment variables need to be set off by indicators that they have zero width, or else the shell won’t calculate correctly the width of the prompt. Bash encases such things with slash-brackets “[ .. ]”, whereas Tcsh uses percent-brace “%{ .. %}”.

The codes:
0   restore default color
1   brighter
2   dimmer
4   underlined text
5   flashing text
7   reverse video

            black   red     green   yellow  blue    purple  cyan    white
foreground  30      31      32      33      34      35      36      37
background  40      41      42      43      44      45      46      47 

来自 http://zipcon.net/~swhite/docs/computers/linux/shell_prompts.html

因此,为了通过 Java 执行此操作,您需要设置

System.out.println(characterCode + character);

其中 String characterCode = "3[31;44;1m";char character = 'A';

你会得到一个 A,前景色设置为红色,背景色设置为蓝色,字体为粗体...


编辑:Xubuntu 中的测试结果

public static void main(String[] args) {
    char character = 'A';
    String characterCode;
    for (int foreground = 30; foreground < 38; foreground++) {
        for (int background = 40; background < 48; background++) {
            characterCode = "3[" + foreground + ";" + background + ";1m";
            System.out.print(characterCode + character);
        }
        System.out.println();
    }

}