fatih/color
是一个能使你的命令行输出变得花里胡哨的工具包。实现依赖于ANSI Escape Codes 。
可以看看github上给出的效果图。
快速入门
安装命令如下:
go get github.com/fatih/color
复制代码
基础样式
官方提供了大量的基础样式,基础样式有直接的打印函数可以调用。用一下github上的例子:
// Print with default helper functions
color.Cyan("Prints text in cyan.")
// A newline will be appended automatically
color.Blue("Prints %s in blue.", "text")
// These are using the default foreground colors
color.Red("We have red")
color.Magenta("And many others ..")
复制代码
效果如下:
看起来还不错吧。整理了一个表格。
颜色 | 函数 |
---|---|
青色 | Cyan |
蓝色 | Blue |
红色 | Red |
黑色 | Black |
绿色 | Green |
黄色 | Yellow |
洋红 | Magenta |
白色 | White |
函数名前加Hi
前缀,为加深颜色,效果不是很明显。。。如:
color.White("This is white")
color.HiWhite("This is hiwhite")
复制代码
函数名后加
String
后缀,返回格式化好的字符串,暂时不知道有啥用。如:
color.WhiteString("This is white string")
color.HiWhiteString("This is hiwhite string")
复制代码
其他样式
color
中还提供了一些格式,比如加粗、下划线、删除线等,但是他们不能直接使用。需要创建color object
,通过调用对象的方法进行打印。
fmt.Println("original")
b := color.New(color.Bold)
b.Println("bold")
复制代码
颜色样式:
包括前景色Fg
、背景色Bg
。只需在颜色函数之前再添加对应前缀即可。如:
color.New(color.BgBgHiBlack)
注意: 实验中发现,样式修改后可能会影响到其他打印信息。可以尝试使用下面的重置方法处理下。
特殊样式:
样式 | 对应常量 |
---|---|
加粗 | Bold |
斜体 | Italic |
下划线 | Underline |
CrossedOut | |
下面的效果不太方便演示,所以请自行尝试其效果 | |
隐藏 看不见,但是可以被复制 | Concealed |
负片 | ReverseVideo |
模糊 | Faint |
慢闪 实测window上无效 | BlinkSlow |
快闪 实测window上无效 | BlinkRapid |
重置 | Reset |
组合
样式可以组合再一起使用,并且创建的颜色对象也是可以重复使用的。继续使用github上的例子。
// Create a new color object
c := color.New(color.FgCyan).Add(color.Underline)
c.Println("Prints cyan text with an underline.")
// Or just add them to New()
d := color.New(color.FgCyan, color.Bold)
d.Printf("This prints bold cyan %s\n", "too!.")
// Mix up foreground and background colors, create new mixes!
red := color.New(color.FgRed)
boldRed := red.Add(color.Bold)
boldRed.Println("This will print text in bold red.")
whiteBackground := red.Add(color.BgWhite)
whiteBackground.Println("Red text with white background.")
复制代码
效果:
输出接口
可以使用io.Writer
做为输出的接口。如:
// Use your own io.Writer output
color.New(color.FgBlue).Fprintln(myWriter, "blue color!")
blue := color.New(color.FgBlue)
blue.Fprint(writer, "This will print text in blue.")
复制代码
自定义函数名
可以使用自定义函数名,简化打印过程。
red := color.New(color.FgRed).PrintfFunc()
red("Warning")
red("Error: %s", err)
复制代码
同时支持PrintfFunc
,FprintFunc
,SprintFunc
三类方法。
原有打印的改造
可以使用Set
,Unset
原来的打印进行改在。
// Use handy standard colors
color.Set(color.FgYellow)
fmt.Println("Existing text will now be in yellow")
fmt.Printf("This one %s\n", "too")
color.Unset() // Don't forget to unset
// You can mix up parameters
color.Set(color.FgMagenta, color.Bold)
defer color.Unset() // Use it in your function
fmt.Println("All text will now be bold magenta.")
复制代码
注意: 记得Unset
。
禁能、使能颜色
在一些特殊场景我们可能会不希望有颜色输出效果。
这时可以设置NO_COLOR
开关,禁用颜色。或者通过flag控制color.NoClor
来控制。
// 运行时添加`--no-color`参数即可
var flagNoColor = flag.Bool("no-color", false, "Disable color output")
if *flagNoColor {
color.NoColor = true // disables colorized output
}
复制代码
同时它还支持对单一颜色对象的使能、禁能控制。
c := color.New(color.FgCyan)
c.Println("Prints cyan text")
c.DisableColor()
c.Println("This is printed without any color")
c.EnableColor()
c.Println("This prints again cyan...")
复制代码
使用案例
- 彩色的日志输出,帮助快速找到需要的信息。
logErr := color.New(color.FgRed)
logWarn := color.New(color.FgHiYellow)
logInfo := color.New(color.FgGreen)
logErr.Println("我是Err信息", "我是Err信息", "我是Err信息", "我是Err信息")
logWarn.Println("我是Warn信息", "我是Warn信息", "我是Warn信息", "我是Warn信息")
logInfo.Println("我是Info信息", "我是Info信息", "我是Info信息", "我是Info信息", "我是Info信息", "我是Info信息")
复制代码