gookit/color - Golang下的命令行色彩使用库
Gookit Color
Golang下的命令行色彩使用库, 拥有丰富的色彩渲染输出,通用的API方法,兼容Windows系统
基本颜色预览:

现在,256色和RGB色彩也已经支持windows CMD和PowerShell中工作:

功能特色
- 使用简单方便,无其他依赖
- 支持丰富的颜色输出, 16色(4bit),256色(8bit),RGB色彩(24bit, RGB)
- 16色(4bit)是最常用和支持最广的,支持Windows
cmd.exe - 自
v1.2.4起 256色(8bit),RGB色彩(24bit)均支持windows CMD和PowerShell终端 - 请查看 this gist 了解支持RGB色彩的终端
- 16色(4bit)是最常用和支持最广的,支持Windows
- 通用的API方法:
PrintPrintfPrintlnSprintSprintf - 同时支持html标签式的颜色渲染. eg:
<green>message</> - 基础色彩:
BoldBlackWhiteGrayRedGreenYellowBlueMagentaCyan - 扩展风格:
InfoNoteLightErrorDangerNoticeSuccessCommentPrimaryWarningQuestionSecondary - 支持Linux、Mac同时兼容Windows系统环境
GoDoc
安装
go get github.com/gookit/color
提示
如果要在Windows上打印自定义的颜色消息,应使用 color.PrintX 而不是 fmt.PrintX
str := color.Red.Sprint("an colored message string")
// Windows 下将不会输出颜色
fmt.Println(str)
// Windows 也可以输出色彩
color.Println(str)
color.PrintX系列方法是通用的,您可以直接使用它们替代fmt.PrintX方法
快速开始
如下,引入当前包就可以快速的使用
package main
import (
"fmt"
"github.com/gookit/color"
)
func main() {
// 简单快速的使用,跟 fmt.Print* 类似
color.Red.Println("Simple to use color")
color.Green.Print("Simple to use color\n")
color.Cyan.Printf("Simple to use %s\n", "color")
color.Yellow.Printf("Simple to use %s\n", "color")
// use like func
red := color.FgRed.Render
green := color.FgGreen.Render
fmt.Printf("%s line %s library\n", red("Command"), green("color"))
// 自定义颜色
color.New(color.FgWhite, color.BgBlack).Println("custom color style")
// 也可以:
color.Style{color.FgCyan, color.OpBold}.Println("custom color style")
// internal style:
color.Info.Println("message")
color.Warn.Println("message")
color.Error.Println("message")
// 使用颜色标签
color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n")
// apply a style tag
color.Tag("info").Println("info style text")
// prompt message
color.Info.Prompt("prompt style message")
color.Warn.Prompt("prompt style message")
// tips message
color.Info.Tips("tips style message")
color.Warn.Tips("tips style message")
}
运行 demo:
go run ./_examples/demo.go

构建风格
// 仅设置前景色
color.FgCyan.Printf("Simple to use %s\n", "color")
// 仅设置背景色
color.BgRed.Printf("Simple to use %s\n", "color")
// 完全自定义: 前景色 背景色 选项
style := color.New(color.FgWhite, color.BgBlack, color.OpBold)
style.Println("custom color style")
// 也可以:
color.Style{color.FgCyan, color.OpBold}.Println("custom color style")
直接设置控制台属性:
// 设置console颜色
color.Set(color.FgCyan)
// 输出信息
fmt.Print("message")
// 重置console颜色
color.Reset()
当然,color已经内置丰富的色彩风格支持
基础颜色方法
支持在windows
cmd.exepowerShell等终端使用
color.Boldcolor.Blackcolor.Whitecolor.Graycolor.Redcolor.Greencolor.Yellowcolor.Bluecolor.Magentacolor.Cyan
color.Bold.Println("bold message")
color.Yellow.Println("yellow message")
运行demo:
go run ./_examples/basiccolor.go

扩展风格方法
支持在windows
cmd.exepowerShell等终端使用
color.Infocolor.Notecolor.Lightcolor.Errorcolor.Dangercolor.Noticecolor.Successcolor.Commentcolor.Primarycolor.Warningcolor.Questioncolor.Secondary
基础风格
// print message
color.Info.Println("Info message")
color.Success.Println("Success message")
Run demo: go run ./_examples/theme_basic.go

简约提示风格
color.Info.Tips("tips style message")
color.Warn.Tips("tips style message")
Run demo: go run ./_examples/theme_tips.go

着重提示风格
color.Info.Prompt("prompt style message")
color.Warn.Prompt("prompt style message")
Run demo: go run ./_examples/theme_prompt.go

强调提示风格
color.Info.Block("prompt style message")
color.Warn.Block("prompt style message")
Run demo: go run ./_examples/theme_block.go

使用颜色标签
支持 在windows
cmd.exePowerShell使用
使用内置的颜色标签,可以非常方便简单的构建自己需要的任何格式
// 使用内置的 color tag
color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>")
color.Println("<suc>hello</>")
color.Println("<error>hello</>")
color.Println("<warning>hello</>")
// 自定义颜色属性
color.Print("<fg=yellow;bg=black;op=underscore;>hello, welcome</>\n")
- 使用
color.Tag
给后面输出的文本信息加上给定的颜色风格标签
// set a style tag
color.Tag("info").Print("info style text")
color.Tag("info").Printf("%s style text", "info")
color.Tag("info").Println("info style text")
运行 demo:
go run ./_examples/colortag.go

256色使用
256色彩在
v1.2.4后支持Windows CMD,PowerShell 环境
使用前景或后景色
color.C256(val uint8, isBg ...bool) Color256
c := color.C256(132) // fg color
c.Println("message")
c.Printf("format %s", "message")
c := color.C256(132, true) // bg color
c.Println("message")
c.Printf("format %s", "message")
使用风格
可同时设置前景和背景色
color.S256(fgAndBg ...uint8) *Style256
s := color.S256(32, 203)
s.Println("message")
s.Printf("format %s", "message")
可以同时添加选项设置:
s := color.S256(32, 203)
s.SetOpts(color.Opts{color.OpBold})
s.Println("style with options")
s.Printf("style with %s\n", "options")
运行 demo:
go run ./_examples/color256.go

RGB色彩使用
RGB色彩在
v1.2.4后支持 WindowsCMD,PowerShell环境
效果预览:
运行 demo:
Run demo: go run ./_examples/color_rgb.go

代码示例:
color.RGB(30, 144, 255).Println("message. use RGB number")
color.HEX("#1976D2").Println("blue-darken")
color.HEX("#D50000", true).Println("red-accent. use HEX style")
color.RGBStyleFromString("213,0,0").Println("red-accent. use RGB number")
color.HEXStyle("eee", "D50000").Println("deep-purple color")
使用前景或后景色
color.RGB(r, g, b uint8, isBg ...bool) RGBColor
c := color.RGB(30,144,255) // fg color
c.Println("message")
c.Printf("format %s", "message")
c := color.RGB(30,144,255, true) // bg color
c.Println("message")
c.Printf("format %s", "message")
color.HEX(hex string, isBg ...bool) RGBColor从16进制颜色创建
c := color.HEX("ccc") // 也可以写为: "cccccc" "#cccccc"
c.Println("message")
c.Printf("format %s", "message")
c = color.HEX("aabbcc", true) // as bg color
c.Println("message")
c.Printf("format %s", "message")
使用风格
可同时设置前景和背景色
color.NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyle
s := color.NewRGBStyle(RGB(20, 144, 234), RGB(234, 78, 23))
s.Println("message")
s.Printf("format %s", "message")
color.HEXStyle(fg string, bg ...string) *RGBStyle从16进制颜色创建
s := color.HEXStyle("11aa23", "eee")
s.Println("message")
s.Printf("format %s", "message")
- 可以同时添加选项设置:
s := color.HEXStyle("11aa23", "eee")
s.SetOpts(color.Opts{color.OpBold})
s.Println("style with options")
s.Printf("style with %s\n", "options")
方法参考
一些有用的工具方法参考
Disable()disable color renderSetOutput(io.Writer)custom set the colored text output writerForceOpenColor()force open color renderClearCode(str string) stringUse for clear color codesColors2code(colors ...Color) stringConvert colors to code. return like “32;45;3”ClearTag(s string) stringclear all color html-tag for a stringIsConsole(w io.Writer)Determine whether w is one of stderr, stdout, stdinHexToRgb(hex string) (rgb []int)Convert hex color string to RGB numbersRgbToHex(rgb []int) stringConvert RGB to hex code- 更多请查看文档 pkg.go.dev/github.com/gookit/color
Gookit 工具包
- gookit/ini INI配置读取管理,支持多文件加载,数据覆盖合并, 解析ENV变量, 解析变量引用
- gookit/rux Simple and fast request router for golang HTTP
- gookit/gcli Go的命令行应用,工具库,运行CLI命令,支持命令行色彩,用户交互,进度显示,数据格式化显示
- gookit/slog 简洁易扩展的go日志库
- gookit/event Go实现的轻量级的事件管理、调度程序库, 支持设置监听器的优先级, 支持对一组事件进行监听
- gookit/cache 通用的缓存使用包装库,通过包装各种常用的驱动,来提供统一的使用API
- gookit/config Go应用配置管理,支持多种格式(JSON, YAML, TOML, INI, HCL, ENV, Flags),多文件加载,远程文件加载,数据合并
- gookit/color CLI 控制台颜色渲染工具库, 拥有简洁的使用API,支持16色,256色,RGB色彩渲染输出
- gookit/filter 提供对Golang数据的过滤,净化,转换
- gookit/validate Go通用的数据验证与过滤库,使用简单,内置大部分常用验证、过滤器
- gookit/goutil Go 的一些工具函数,格式化,特殊处理,常用信息获取等
- 更多请查看 github.com/gookit
参考项目
本作品采用《CC 协议》,转载必须注明作者和本文链接
关于 LearnKu
推荐文章: