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

相关推荐
源代码•宸18 小时前
goframe框架签到系统项目开发(每日签到添加积分和积分记录、获取当月最大连续签到天数、发放连续签到奖励积分、实现签到日历详情接口)
数据库·经验分享·redis·中间件·golang·dao·goframe
梦想的旅途218 小时前
企业微信外部群消息推送实战指南
java·golang·企业微信
古城小栈18 小时前
go-zero 从入门到实战 全指南(包的)
开发语言·后端·golang
lsx20240618 小时前
Python 3 函数
开发语言
-To be number.wan18 小时前
C++ 进阶技巧:如何让 cout << 自定义对象 正常输出?
开发语言·c++
独自破碎E18 小时前
怎么实现一个滑动验证码功能?又如何防止被机器识别破解
java·spring boot·后端
2501_9444460018 小时前
Flutter&OpenHarmony状态管理方案详解
开发语言·javascript·flutter
一路往蓝-Anbo18 小时前
C语言从句柄到对象 (三) —— 抛弃 Malloc:静态对象池与索引句柄的终极形态
c语言·开发语言·数据结构·stm32·单片机·算法
lbb 小魔仙18 小时前
【Java】Spring Data JPA 详解:ORM 映射、查询方法与复杂 SQL 处理
java·开发语言·sql·spring cloud
Fighting_p19 小时前
【预览word文档】使用插件 docx-preview 预览线上 word 文档
开发语言·c#·word