ANSI color style and value comparison table, java, shell color printout

1. Color and style comparison table

1.1. Color comparison table

serial number color color foreground color Foreground color (light) background color background color (bright)
0 black BLACK 30 90 40 100
1 red RED 31 91 41 101
2 green GREEN 32 92 42 102
3 yellow YELLOW 33 93 43 103
4 blue BLUE 34 94 44 104
5 magenta MAGENTA 35 95 45 105
6 green CYAN 36 96 46 106
7 white WHITE 37 97 47 107
8
9 default DEFAULT 39 39 49 49

1.2. Style comparison table

serial number style style style value
0 normal (reset) NORMAL 0
1 bold (enhanced) BOLD 1
2 weaken FAINT 2
3 italics ITALIC 3
4 underline UNDERLINE 4
5 slow blinking (not widely supported, shell works) SLOW_BLINK 5
6 Fast blinking (not widely supported) FAST_BLINK 6
7 reverse color REVERSE_COLOR 7
8 Foreground hidden (not widely supported, shell works) HIDDEN 8
9 strikethrough STRIKETHROUGH 9

1.3. More styles

insert image description here

2. Java uses color output

  • java code
public class AnsiEncoderTest {
    
    
    // 0b、0、、0x
    // \033即八进制的33,对应十进制的27、16进制的0x1b、二进制0b1111,对应ASCⅡ的ESC。https://baike.baidu.com/item/ASCII/309296
    // 源码:  https://gitee.com/lishuoboy/lishuoboy-hutool/tree/master/src/main/java/top/lishuoboy/hutool/lang
    private static final String START = "\033[";
    private static final String END = "m";

    @Test
    public void test() {
    
    
        System.out.println("==========test()==========");
        // 使用1个样式、颜色
        System.out.println(START + 31 + END + "我是红色前景");
        System.out.println("你猜我啥颜色?");
        System.out.println(START + 91 + END + "我是亮红色前景" + START + 0 + END);
        System.out.println("你猜我啥颜色?");
        System.out.println(START + 41 + END + "我是红色背景" + START + 0 + END);
        System.out.println(START + 3 + END + "我是斜体" + START + 0 + END);

        // 同时使用多个样式、颜色
        System.out.println(START + "34;103" + END + "我是蓝色前景、亮黄色背景" + START + 0 + END);
        System.out.println(START + 34 + END + START + 103 + END + "我是蓝色前景、亮黄色背景" + START + 0 + END);
    }

    @Test
    public void testHuTool() {
    
    
        System.out.println("==========testHuTool()==========");
        // 使用1个样式、颜色
        System.out.println(AnsiEncoder.encode(AnsiColor.RED, "我是红色前景"));
        System.out.println("你猜我啥颜色?");
        System.out.println(AnsiEncoder.encode(AnsiColor.BRIGHT_RED, "我是亮红色前景"));
        System.out.println("你猜我啥颜色?");
        System.out.println(AnsiEncoder.encode(AnsiBackground.RED, "我是红色背景"));
        System.out.println(AnsiEncoder.encode(AnsiStyle.ITALIC, "我是斜体"));

        // 同时使用多个样式、颜色
        System.out.println(AnsiEncoder.encode(AnsiColor.BLUE, AnsiBackground.BRIGHT_YELLOW, "我是蓝色前景、亮黄色背景"));
    }
}
  • idea log
    insert image description here
  • linux shellmiddle java -jaroutput
    insert image description here
  • windows cmdmiddle java -jaroutput
    insert image description here

3. The shell uses colored output

  • sh script
START='\e[';
END=m;

#使用1个样式、颜色
echo -e "${START}31${END}我是红色前景"
echo -e "你猜我啥颜色?"
echo -e "${START}91${END}我是亮红色前景${START}0${END}"
echo -e "你猜我啥颜色?"
echo -e "${START}41${END}我是红色背景${START}0${END}"
echo -e "${START}3${END}我是斜体${START}0${END}"

#同时使用多个样式、颜色
echo -e "${START}34;103${END}我是蓝色前景、亮黄色背景${START}0${END}"
echo -e "${START}34${END}${START}103${END}我是蓝色前景、亮黄色背景${START}0${END}"
  • sh log
    insert image description here

Guess you like

Origin blog.csdn.net/lishuoboy/article/details/130676206