如何讓shell終端和goland控制臺輸出彩色的文字
終端輸出彩色文字
開發(fā)工具:Mac,Goland,Mac自帶shell。這是基于Mac的測試結(jié)果,根據(jù)讀者留言,在Windows上不生效,標(biāo)識符不一樣。
在終端輸出這段命令,將的到一個紅色背景、綠色文字,并不停閃爍的輸出。
sszxr:~ sszxr$ echo -e "\033[5;32;41mI ♡ You \033[0m" I ♡ You sszxr:~ sszxr$
雙引號中的反斜杠\表示轉(zhuǎn)義,033是標(biāo)識符,表示用來設(shè)置顏色,[表示開始顏色設(shè)置,m為顏色設(shè)置結(jié)束。[后面的5表示閃爍,分號后面的32表示前景色,也就是文字的顏色,為綠色;再后面41表示背景色,為紅色,到m為設(shè)置結(jié)束,后面是輸出的內(nèi)容,最后為再一次設(shè)置顏色,0m表示取消顏色設(shè)置。
從括號[到m中間為顏色設(shè)置,以;號分隔。
樣式有【0,1,4,5,7,8】六種,分別是:
0 終端默認(rèn)設(shè)置 1 高亮顯示 4 使用下劃線 5 閃爍 7 反白顯示 8 不可見
顏色有7中,分別為
前景 背景 顏色 30 40 黑色 31 41 紅色 32 42 綠色 33 43 黃色 34 44 藍(lán)色 35 45 紫紅色 36 46 青藍(lán)色 37 47 白色
3開頭是前景色,也就是文字的顏色;4開頭是背景色。
Go語言中的彩色輸出
樣式和顏色與上面一樣,只是標(biāo)識符不一樣,
fmt.Printf("%c[0;41;36m%s%c[0m\n", 0x1B, "testPrintColor", 0x1B)
標(biāo)識符為0x1B,具體設(shè)置也是在[到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) }
運行結(jié)果
[]byte{}中那些數(shù)字是什么意思
他們是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("%#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) }
運行結(jié)果
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,表示設(shè)置字體顏色
52代表4,表示設(shè)置背景色
51代表3,表示設(shè)置前景色,也就是文字的顏色
90到97與30到37的效果一樣,一個是設(shè)置字體顏色,一個是設(shè)置前景色,所以57和51可以互換,效果完全一樣,
reset表示0x1B[0m,表示清除顏色設(shè)置。
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) }
補充:Golang終端彩色輸出
終端彩色輸出
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: 藍(lán) \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: 藍(lán) 33: 黃 \n", 44, 33) fmt.Printf("\x1b[%d;%dmhello world \x1b[0m 43: 黃 34: 藍(lán) \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) }
終端顯示
取值范圍
前景 背景 顏色 30 40 黑色 31 41 紅色 32 42 綠色 33 43 黃色 34 44 藍(lán)色 35 45 紫色 36 46 深綠 37 47 白色
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關(guān)文章
Golang的os標(biāo)準(zhǔn)庫中常用函數(shù)的整理介紹
這篇文章主要介紹了Go語言的os標(biāo)準(zhǔn)庫中常用函數(shù),主要用來實現(xiàn)與操作系統(tǒng)的交互功能,需要的朋友可以參考下2015-10-10Go語言中處理JSON數(shù)據(jù)的編碼和解碼的方法
在Go語言中,處理JSON數(shù)據(jù)的編碼和解碼主要依賴于標(biāo)準(zhǔn)庫中的encoding/json包,這個包提供了兩個核心的函數(shù):Marshal和Unmarshal,本文給大家介紹了Go語言中處理JSON數(shù)據(jù)的編碼和解碼的方法,需要的朋友可以參考下2024-04-04Golang標(biāo)準(zhǔn)庫unsafe源碼解讀
這篇文章主要為大家介紹了Golang標(biāo)準(zhǔn)庫unsafe源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08Go 使用Unmarshal將json賦給struct出錯的原因及解決
這篇文章主要介紹了Go 使用Unmarshal將json賦給struct出錯的原因及解決方案,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-03-03Go使用Gin+mysql實現(xiàn)增刪改查的詳細(xì)實例
golang本身沒有提供連接mysql的驅(qū)動,但是定義了標(biāo)準(zhǔn)接口供第三方開發(fā)驅(qū)動,下面這篇文章主要給大家介紹了關(guān)于Go使用Gin+mysql實現(xiàn)增刪改查的相關(guān)資料,需要的朋友可以參考下2022-12-12golang調(diào)用c實現(xiàn)的dll接口細(xì)節(jié)分享
這篇文章主要介紹了golang調(diào)用c實現(xiàn)的dll接口細(xì)節(jié)分享,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-05-05