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

相关推荐
customer086 分钟前
【开源免费】基于SpringBoot+Vue.JS医疗报销系统(JAVA毕业设计)
java·vue.js·spring boot·后端·开源
B站计算机毕业设计超人10 分钟前
计算机毕业设计SpringBoot+Vue.jst房屋租赁系统(源码+LW文档+PPT+讲解)
vue.js·spring boot·后端·eclipse·intellij-idea·mybatis·课程设计
thinkMoreAndDoMore35 分钟前
深度学习(3)-TensorFlow入门(常数张量和变量)
开发语言·人工智能·python
蓝桉8021 小时前
图片爬取案例
开发语言·数据库·python
逸狼1 小时前
【JavaEE进阶】Spring DI
java·开发语言
m0_748248651 小时前
SpringBoot整合easy-es
spring boot·后端·elasticsearch
一个热爱生活的普通人2 小时前
golang的切片(Slice)底层实现解析
后端·go
红目香薰2 小时前
Trae——慧码速造——完整项目开发体验
后端
my_styles2 小时前
2025-alibaba-Sentinel组件
java·开发语言·sentinel
禁默2 小时前
C++之旅-C++11的深度剖析(1)
开发语言·c++