給CLI一點顏色 (ANSI Colors)

紀錄一下如何在cli上呈現不同的顏色

在使用golang時,發現fmt以及log印出來的字除了多了時間以外還多了點顏色。

好奇之下才去搜尋了一下關於golang顏色字串的教學。

強力推薦這篇StackOverFlow的文章 

查了一下才發現原來顏色字串這件事情,其實要歸到ANSI-Colors這個功能。

1.首先你的文字介面器要可以支援ansi escape code

    Unix-like的系統應該都支援,windows 的 Powershell也支援。

 

2.規則

    溢出字元 "\033"

    指令規則:"\033[{code1};{code2};{code3}m"

 

3.範例

    紫色字串:"\033[35m"(code參考下列色碼表)

package main

import (
	"fmt"
)

func main() {
	fmt.Println()
	fmt.Println("\033[35m你好")
	fmt.Println("你好")
	fmt.Println()
}

輸出:

因為指令"\033[35m"所以之後的字串顏色都變成紫色的

 

    還原指令: "\033[0m"

package main

import (
	"fmt"
)

func main() {
	fmt.Println()
	fmt.Println("\033[35m你好\033[0m \033[31m我好\033[0m \033[32m大家好\033[0m")
	fmt.Println()
}

輸出:

 

    加上背景色:

package main

import (
	"fmt"
)

func main() {
	fmt.Println()
	fmt.Println("\033[37;101m危險\033[0m")
	fmt.Println()
}

輸出:

其他像是粗體字,下底線等等code請參考附件表格。

 

4.懶人包

如果懶得自己處理溢出字元指令,github上也有人分享包裝好的package方便使用。

詳細可自行參考github說明 

    安裝 :go get github.com/fatih/color

package main

import (
	"fmt"

	"github.com/fatih/color"
)

func main() {
	fmt.Println()
	color.Cyan("Prints text in cyan.")
	fmt.Println()
}

輸出:

 

參考附件

色碼表:

指令表:

╔══════════╦════════════════════════════════╦═════════════════════════════════════════════════════════════════════════╗
║  Code    ║             Effect             ║                                   Note                                  ║
╠══════════╬════════════════════════════════╬═════════════════════════════════════════════════════════════════════════╣
║ 0        ║  Reset / Normal                ║  all attributes off                                                     ║
║ 1        ║  Bold or increased intensity   ║                                                                         ║
║ 2        ║  Faint (decreased intensity)   ║  Not widely supported.                                                  ║
║ 3        ║  Italic                        ║  Not widely supported. Sometimes treated as inverse.                    ║
║ 4        ║  Underline                     ║                                                                         ║
║ 5        ║  Slow Blink                    ║  less than 150 per minute                                               ║
║ 6        ║  Rapid Blink                   ║  MS-DOS ANSI.SYS; 150+ per minute; not widely supported                 ║
║ 7        ║  [[reverse video]]             ║  swap foreground and background colors                                  ║
║ 8        ║  Conceal                       ║  Not widely supported.                                                  ║
║ 9        ║  Crossed-out                   ║  Characters legible, but marked for deletion.  Not widely supported.    ║
║ 10       ║  Primary(default) font         ║                                                                         ║
║ 11–19    ║  Alternate font                ║  Select alternate font `n-10`                                           ║
║ 20       ║  Fraktur                       ║  hardly ever supported                                                  ║
║ 21       ║  Bold off or Double Underline  ║  Bold off not widely supported; double underline hardly ever supported. ║
║ 22       ║  Normal color or intensity     ║  Neither bold nor faint                                                 ║
║ 23       ║  Not italic, not Fraktur       ║                                                                         ║
║ 24       ║  Underline off                 ║  Not singly or doubly underlined                                        ║
║ 25       ║  Blink off                     ║                                                                         ║
║ 27       ║  Inverse off                   ║                                                                         ║
║ 28       ║  Reveal                        ║  conceal off                                                            ║
║ 29       ║  Not crossed out               ║                                                                         ║
║ 30–37    ║  Set foreground color          ║  See color table below                                                  ║
║ 38       ║  Set foreground color          ║  Next arguments are `5;n` or `2;r;g;b`, see below                       ║
║ 39       ║  Default foreground color      ║  implementation defined (according to standard)                         ║
║ 40–47    ║  Set background color          ║  See color table below                                                  ║
║ 48       ║  Set background color          ║  Next arguments are `5;n` or `2;r;g;b`, see below                       ║
║ 49       ║  Default background color      ║  implementation defined (according to standard)                         ║
║ 51       ║  Framed                        ║                                                                         ║
║ 52       ║  Encircled                     ║                                                                         ║
║ 53       ║  Overlined                     ║                                                                         ║
║ 54       ║  Not framed or encircled       ║                                                                         ║
║ 55       ║  Not overlined                 ║                                                                         ║
║ 60       ║  ideogram underline            ║  hardly ever supported                                                  ║
║ 61       ║  ideogram double underline     ║  hardly ever supported                                                  ║
║ 62       ║  ideogram overline             ║  hardly ever supported                                                  ║
║ 63       ║  ideogram double overline      ║  hardly ever supported                                                  ║
║ 64       ║  ideogram stress marking       ║  hardly ever supported                                                  ║
║ 65       ║  ideogram attributes off       ║  reset the effects of all of 60-64                                      ║
║ 90–97    ║  Set bright foreground color   ║  aixterm (not in standard)                                              ║
║ 100–107  ║  Set bright background color   ║  aixterm (not in standard)                                              ║
╚══════════╩════════════════════════════════╩═════════════════════════════════════════════════════════════════════════╝

 

參考:

https://stackoverflow.com/questions/4842424/list-of-ansi-color-escape-sequences

https://en.wikipedia.org/wiki/ANSI_escape_code

https://github.com/fatih/color