gookit/color - Go语言命令行色彩使用库教程

gookit/color - Go语言命令行色彩使用库教程

1.安装

go 复制代码
go get github.com/gookit/color

2.基础颜色(16-color)

提供通用的API方法:Print Printf Println Sprint Sprintf

1、例如:

go 复制代码
color.Yellow.Println("bold message")
color.Blue.Println("bold message")
color.White.Println("bold message")

2、特例,仅仅设置前景和背景颜色:

go 复制代码
// 仅使用前景色
color.FgCyan.Printf("Simple to use %s\n", "color")
// 仅使用背景色
color.BgRed.Printf("Simple to use %s\n", "color")

3、构建风格

go 复制代码
// 完全自定义: 前景色 背景色 选项(字体)
style := color.New(color.FgRed, color.BgBlack, color.OpBold)
style.Println("custom color style")

4、直接设置控制台属性

go 复制代码
// 设置console颜色
_, err := color.Set(color.FgCyan)
if err != nil {
	return
}
// 输出信息
fmt.Print("message")

5、扩展风格方法

go 复制代码
color.Info.Println("Info message")
color.Note.Println("Note message")
color.Notice.Println("Notice message")

全部扩展风格:

go 复制代码
// Info color style
Info = &Theme{"info", Style{OpReset, FgGreen}}
// Note color style
Note = &Theme{"note", Style{OpBold, FgLightCyan}}
// Warn color style
Warn = &Theme{"warning", Style{OpBold, FgYellow}}
// Light color style
Light = &Theme{"light", Style{FgLightWhite, BgBlack}}
// Error color style
Error = &Theme{"error", Style{FgLightWhite, BgRed}}
// Danger color style
Danger = &Theme{"danger", Style{OpBold, FgRed}}
// Debug color style
Debug = &Theme{"debug", Style{OpReset, FgCyan}}
// Notice color style
Notice = &Theme{"notice", Style{OpBold, FgCyan}}
// Comment color style
Comment = &Theme{"comment", Style{OpReset, FgYellow}}
// Success color style
Success = &Theme{"success", Style{OpBold, FgGreen}}
// Primary color style
Primary = &Theme{"primary", Style{OpReset, FgBlue}}
// Question color style
Question = &Theme{"question", Style{OpReset, FgMagenta}}
// Secondary color style
Secondary = &Theme{"secondary", Style{FgDarkGray}}

6、简约提示风格

go 复制代码
color.Info.Tips("Info tips message")
color.Notice.Tips("Notice tips message")
color.Error.Tips("Error tips message")

7、着重提示风格

go 复制代码
color.Info.Prompt("Info prompt message")
color.Error.Prompt("Error prompt message")
color.Danger.Prompt("Danger prompt message")

8、强调提示风格

go 复制代码
color.Warn.Block("Warn block message")
color.Debug.Block("Debug block message")
color.Question.Block("Question block message")

3.256色彩/RGB风格

参考官方文档:

gookit/color

相关推荐
程序员cxuan5 分钟前
Anthropic:session 之间可以相互通信了
人工智能·后端·程序员
周GZ18 分钟前
简单讲解Java中静态方法与 实例方法
java·开发语言
卷无止境27 分钟前
FastAPI 前端托管全攻略:从静态文件到大型全栈项目架构
后端·python
webmote3329 分钟前
用 NVIDIA Nemotron 3 Super + .NET 构建有记忆的多轮对话
后端·算法
神奇小汤圆33 分钟前
JUC三大常用工具类CountDownLatch、CyclicBarrier、Semaphore
后端
卷无止境1 小时前
当FastAPI项目开始"膨胀",代码该往哪儿放
后端·python
fliter1 小时前
程序员每天能省 1 小时的 50 个 macOS/终端/Git/浏览器技巧
后端
神奇小汤圆1 小时前
Redis为什么使用哈希槽而不用一致性哈希
后端
用户8356290780511 小时前
Python设置PowerPoint幻灯片背景的方法
后端·python
站大爷IP1 小时前
Python 的 is 把我坑惨了,原来 == 和 is 在小整数池外完全是两码事
后端