脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Golang - 如何让shell终端和goland控制台输出彩色的文字

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

2021-06-15 01:23Elonjelinek Golang

这篇文章主要介绍了如何让shell终端和goland控制台输出彩色的文字的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

终端输出彩色文字

开发工具:Mac,Goland,Mac自带shell。这是基于Mac的测试结果,根据读者留言,在Windows上不生效,标识符不一样。

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

?
1
2
3
sszxr:~ sszxr$ echo  -e "\033[5;32;41mI ♡  You \033[0m"
I ♡  You
sszxr:~ sszxr$

双引号中的反斜杠\表示转义,033是标识符,表示用来设置颜色,[表示开始颜色设置,m为颜色设置结束。[后面的5表示闪烁,分号后面的32表示前景色,也就是文字的颜色,为绿色;再后面41表示背景色,为红色,到m为设置结束,后面是输出的内容,最后为再一次设置颜色,0m表示取消颜色设置。

从括号[到m中间为颜色设置,以;号分隔。

样式有【0,1,4,5,7,8】六种,分别是:

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

颜色有7中,分别为

?
1
2
3
4
5
6
7
8
9
前景 背景 颜色
30  40  黑色
31  41  红色
32  42  绿色
33  43  黄色
34  44  蓝色
35  45  紫红色
36  46  青蓝色
37  47  白色

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

Go语言中的彩色输出

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

?
1
fmt.Printf("%c[0;41;36m%s%c[0m\n", 0x1B, "testPrintColor", 0x1B)

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

另一种方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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)
}

运行结果

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

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

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

?
1
2
3
4
5
6
7
package main
import "fmt"
func main() {
 fmt.Print(0x1B, '[', ';', 'm', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', "\n")
 fmt.Printf("%#X\t%c\t%c\t%c\t", 27, 91, 59, 109)
 fmt.Printf("%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t%c\t", 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到97与30到37的效果一样,一个是设置字体颜色,一个是设置前景色,所以57和51可以互换,效果完全一样,

reset表示0x1B[0m,表示清除颜色设置。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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)
}

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

补充:Golang终端彩色输出

终端彩色输出
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
func main() {
 fmt.Printf("\x1b[%dmhello world 30: 黑 \x1b[0m\n", 30)
 fmt.Printf("\x1b[%dmhello world 31: 红 \x1b[0m\n", 31)
 fmt.Printf("\x1b[%dmhello world 32: 绿 \x1b[0m\n", 32)
 fmt.Printf("\x1b[%dmhello world 33: 黄 \x1b[0m\n", 33)
 fmt.Printf("\x1b[%dmhello world 34: 蓝 \x1b[0m\n", 34)
 fmt.Printf("\x1b[%dmhello world 35: 紫 \x1b[0m\n", 35)
 fmt.Printf("\x1b[%dmhello world 36: 深绿 \x1b[0m\n", 36)
 fmt.Printf("\x1b[%dmhello world 37: 白色 \x1b[0m\n", 37)
 fmt.Printf("\x1b[%d;%dmhello world \x1b[0m 47: 白色 30: 黑 \n", 47, 30)
 fmt.Printf("\x1b[%d;%dmhello world \x1b[0m 46: 深绿 31: 红 \n", 46, 31)
 fmt.Printf("\x1b[%d;%dmhello world \x1b[0m 45: 紫   32: 绿 \n", 45, 32)
 fmt.Printf("\x1b[%d;%dmhello world \x1b[0m 44: 蓝   33: 黄 \n", 44, 33)
 fmt.Printf("\x1b[%d;%dmhello world \x1b[0m 43: 黄   34: 蓝 \n", 43, 34)
 fmt.Printf("\x1b[%d;%dmhello world \x1b[0m 42: 绿   35: 紫 \n", 42, 35)
 fmt.Printf("\x1b[%d;%dmhello world \x1b[0m 41: 红   36: 深绿 \n", 41, 36)
 fmt.Printf("\x1b[%d;%dmhello world \x1b[0m 40: 黑   37: 白色 \n", 40, 37)
}
终端显示

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

取值范围
?
1
2
3
4
5
6
7
8
9
前景 背景 颜色
30 40 黑色
31 41 红色
32 42 绿色
33 43 黄色
34 44 蓝色
35 45 紫色
36 46 深绿
37 47 白色

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。如有错误或未考虑完全的地方,望不吝赐教。

原文链接:https://blog.csdn.net/Charliewolf/article/details/84771983

延伸 · 阅读

精彩推荐
  • Golanggolang json.Marshal 特殊html字符被转义的解决方法

    golang json.Marshal 特殊html字符被转义的解决方法

    今天小编就为大家分享一篇golang json.Marshal 特殊html字符被转义的解决方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧 ...

    李浩的life12792020-05-27
  • GolangGolang中Bit数组的实现方式

    Golang中Bit数组的实现方式

    这篇文章主要介绍了Golang中Bit数组的实现方式,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    天易独尊11682021-06-09
  • Golanggolang 通过ssh代理连接mysql的操作

    golang 通过ssh代理连接mysql的操作

    这篇文章主要介绍了golang 通过ssh代理连接mysql的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    a165861639710342021-03-08
  • Golanggo日志系统logrus显示文件和行号的操作

    go日志系统logrus显示文件和行号的操作

    这篇文章主要介绍了go日志系统logrus显示文件和行号的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...

    SmallQinYan12302021-02-02
  • Golanggolang如何使用struct的tag属性的详细介绍

    golang如何使用struct的tag属性的详细介绍

    这篇文章主要介绍了golang如何使用struct的tag属性的详细介绍,从例子说起,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看...

    Go语言中文网11352020-05-21
  • GolangGolang通脉之数据类型详情

    Golang通脉之数据类型详情

    这篇文章主要介绍了Golang通脉之数据类型,在编程语言中标识符就是定义的具有某种意义的词,比如变量名、常量名、函数名等等,Go语言中标识符允许由...

    4272021-11-24
  • Golanggolang的httpserver优雅重启方法详解

    golang的httpserver优雅重启方法详解

    这篇文章主要给大家介绍了关于golang的httpserver优雅重启的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,...

    helight2992020-05-14
  • Golanggo语言制作端口扫描器

    go语言制作端口扫描器

    本文给大家分享的是使用go语言编写的TCP端口扫描器,可以选择IP范围,扫描的端口,以及多线程,有需要的小伙伴可以参考下。 ...

    脚本之家3642020-04-25