让shell终端和goland控制台输出彩色的文字 - Go语言中文社区

让shell终端和goland控制台输出彩色的文字


终端输出彩色文字

在终端输出这段命令,将的到一个红色背景、绿色文字,并不停闪烁的输出。

sszxr:~ sszxr$ echo  -e "33[5;32;41mI ♡  You 33[0m"
I ♡  You 
sszxr:~ sszxr$ 

双引号中的反斜杠表示转义,033是标识符,表示用来设置颜色,[表示开始颜色设置,m为颜色设置结束。[后面的5表示闪烁,分号后面的32表示前景色,也就是文字的颜色,为绿色;再后面41表示背景色,为红色,到m为设置结束,后面是输出的内容,最后为再一次设置颜色,0m表示取消颜色设置。
从括号[m中间为颜色设置,以;号分隔。
样式有【0,1,4,5,7,8】六种,分别是:

0  终端默认设置
1  高亮显示
4  使用下划线
5  闪烁
7  反白显示
8  不可见

颜色有7中,分别为

前景 背景 颜色
30  40  黑色
31  41  红色
32  42  绿色
33  43  黄色
34  44  蓝色
35  45  紫红色
36  46  青蓝色
37  47  白色

3位前景色,也就是文字的颜色;4位背景色。

Go语言中的彩色输出

样式和颜色与上面一样,只是标识符不一样,

fmt.Printf("%c[0;41;36m%s%c[0mn", 0x1B, "testPrintColor", 0x1B)

标识符为0x1B,具体设置也是在[m之间,以分号;分隔。

另一种方式

package main

import (
	"fmt"
)

var (
	greenBg      = string([]byte{27, 91, 57, 55, 59, 52, 50, 109})
	whiteBg      = string([]byte{27, 91, 57, 48, 59, 52, 55, 109})
	yellowBg     = string([]byte{27, 91, 57, 48, 59, 52, 51, 109})
	redBg        = string([]byte{27, 91, 57, 55, 59, 52, 49, 109})
	blueBg       = string([]byte{27, 91, 57, 55, 59, 52, 52, 109})
	magentaBg    = string([]byte{27, 91, 57, 55, 59, 52, 53, 109})
	cyanBg       = string([]byte{27, 91, 57, 55, 59, 52, 54, 109})
	green        = string([]byte{27, 91, 51, 50, 109})
	white        = string([]byte{27, 91, 51, 55, 109})
	yellow       = string([]byte{27, 91, 51, 51, 109})
	red          = string([]byte{27, 91, 51, 49, 109})
	blue         = string([]byte{27, 91, 51, 52, 109})
	magenta      = string([]byte{27, 91, 51, 53, 109})
	cyan         = string([]byte{27, 91, 51, 54, 109})
	reset        = string([]byte{27, 91, 48, 109})
	disableColor = false
)

func main() {
	str := "hello world"
	fmt.Println(greenBg, str, reset)
	fmt.Println(whiteBg, str, reset)
	fmt.Println(yellowBg, str, reset)
	fmt.Println(redBg, str, reset)
	fmt.Println(blueBg, str, reset)
	fmt.Println(magentaBg, str, reset)
	fmt.Println(cyanBg, str, reset)
	word := "I love you"
	fmt.Println(green, word, reset)
	fmt.Println(white, word, reset)
	fmt.Println(yellow, word, reset)
	fmt.Println(red, word, reset)
	fmt.Println(blue, word, reset)
	fmt.Println(magenta, word, reset)
	fmt.Println(cyan, word, reset)
}

运行结果
在这里插入图片描述

[]byte{}中那些数字是什么意思

他们是0x1B [ ; m以及0-9的ASCII编码

package main

import "fmt"

func main() {
	fmt.Print(0x1B, '[', ';', 'm', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', "n")
	fmt.Printf("%#Xt%ct%ct%ct", 27, 91, 59, 109)
	fmt.Printf("%ct%ct%ct%ct%ct%ct%ct%ct%ct%ct", 48, 49, 50, 51, 52, 53, 54, 55, 56, 57)
}

运行结果

27 91 59 109 48 49 50 51 52 53 54 55 56 57
0X1B	[	;	m	0	1	2	3	4	5	6	7	8	9

27代表0x1B
91代表[
59代表;
109代表m
57代表9,表示设置字体颜色
52代表4,表示设置背景色
51代表3,表示设置前景色,也就是文字的颜色
90到9730到37的效果一样,一个是设置字体颜色,一个是设置前景色,所以57和51可以互换,效果完全一样,
reset表示0x1B[0m,表示清除颜色设置。

package main

import (
	"fmt"
)

var (
	black        = string([]byte{27, 91, 57, 48, 109})
	red          = string([]byte{27, 91, 57, 49, 109})
	green        = string([]byte{27, 91, 57, 50, 109})
	yellow       = string([]byte{27, 91, 57, 51, 109})
	blue         = string([]byte{27, 91, 57, 52, 109})
	magenta      = string([]byte{27, 91, 57, 53, 109})
	cyan         = string([]byte{27, 91, 57, 54, 109})
	white        = string([]byte{27, 91, 57, 55, 59, 52, 48, 109})
	reset        = string([]byte{27, 91, 48, 109})
	disableColor = false
)

func main() {
	word := "I love you"
	fmt.Println(black, word, reset)
	fmt.Println(red, word, reset)
	fmt.Println(green, word, reset)
	fmt.Println(yellow, word, reset)
	fmt.Println(blue, word, reset)
	fmt.Println(magenta, word, reset)
	fmt.Println(cyan, word, reset)
	fmt.Println(white, word, reset)
}

在这里插入图片描述

版权声明:本文来源CSDN,感谢博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/charliewolf/article/details/84771983
站方申明:本站部分内容来自社区用户分享,若涉及侵权,请联系站方删除。

0 条评论

请先 登录 后评论

官方社群

GO教程

猜你喜欢