golang终端文本样式美化插件库gommon/color的使用
golang终端文本样式美化插件库gommon/color的使用
简介
Color是一个用于美化终端文本样式的Golang库。
安装
go get github.com/labstack/gommon/color
Windows支持
如果你在Windows上使用,可以尝试cmder或者mattn/go-colorable。
使用示例
导入包
import github.com/labstack/gommon/color
彩色文本
// 打印不同颜色的文本
color.Println(color.Black("black"))
color.Println(color.Red("red"))
color.Println(color.Green("green"))
color.Println(color.Yellow("yellow"))
color.Println(color.Blue("blue"))
color.Println(color.Magenta("magenta"))
color.Println(color.Cyan("cyan"))
color.Println(color.White("white"))
color.Println(color.Grey("grey"))
彩色背景
// 打印带彩色背景的文本
color.Println(color.BlackBg("black background", color.Wht))
color.Println(color.RedBg("red background"))
color.Println(color.GreenBg("green background"))
color.Println(color.YellowBg("yellow background"))
color.Println(color.BlueBg("blue background"))
color.Println(color.MagentaBg("magenta background"))
color.Println(color.CyanBg("cyan background"))
color.Println(color.WhiteBg("white background"))
文本强调
// 打印不同强调样式的文本
color.Println(color.Bold("bold"))
color.Println(color.Dim("dim"))
color.Println(color.Italic("italic"))
color.Println(color.Underline("underline"))
color.Println(color.Inverse("inverse"))
color.Println(color.Hidden("hidden"))
color.Println(color.Strikeout("strikeout"))
混合使用
// 混合使用颜色和样式
color.Println(color.Green("bold green with white background", color.B, color.WhtBg))
color.Println(color.Red("underline red", color.U))
color.Println(color.Yellow("dim yellow", color.D))
color.Println(color.Cyan("inverse cyan", color.In))
color.Println(color.Blue("bold underline dim blue", color.B, color.U, color.D))
启用/禁用包
// 禁用颜色输出
color.Disable()
// 启用颜色输出
color.Enable()
创建新实例
// 创建新的color实例
c := color.New()
c.Green("green") // 使用新实例输出绿色文本
这个库提供了丰富的终端文本样式控制功能,可以轻松实现彩色文本、背景色和各种文本强调效果。
更多关于golang终端文本样式美化插件库gommon/color的使用的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于golang终端文本样式美化插件库gommon/color的使用的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
使用gommon/color美化Golang终端输出
gommon/color是一个简单易用的Golang终端文本样式库,它可以帮助你为终端输出添加颜色、背景色和各种文本样式。下面我将详细介绍它的使用方法。
安装
首先安装gommon/color库:
go get github.com/gommon/color
基本使用
1. 基础颜色设置
package main
import (
"fmt"
"github.com/gommon/color"
)
func main() {
// 创建颜色实例
c := color.New()
// 基本颜色
fmt.Println(c.Red("红色文本"))
fmt.Println(c.Green("绿色文本"))
fmt.Println(c.Blue("蓝色文本"))
fmt.Println(c.Yellow("黄色文本"))
fmt.Println(c.Magenta("品红色文本"))
fmt.Println(c.Cyan("青色文本"))
fmt.Println(c.White("白色文本"))
fmt.Println(c.Black("黑色文本"))
// 带背景色
fmt.Println(c.Red("红色文本").BgWhite())
}
2. 文本样式
func styles() {
c := color.New()
// 加粗
fmt.Println(c.Bold("加粗文本"))
// 下划线
fmt.Println(c.Underline("下划线文本"))
// 反转颜色
fmt.Println(c.Inverse("反转颜色文本"))
// 组合样式
fmt.Println(c.Bold(c.Red("红色加粗文本")))
fmt.Println(c.Underline(c.Green("绿色下划线文本")))
}
3. 自定义颜色
func customColors() {
c := color.New()
// 使用RGB值设置颜色
fmt.Println(c.Color("自定义颜色", color.RGB(255, 165, 0))) // 橙色
// 使用16进制颜色码
fmt.Println(c.Color("16进制颜色", color.Hex("#FF69B4"))) // 热粉色
// 自定义背景色
fmt.Println(c.Color("自定义背景", color.BgRGB(70, 130, 180))) // 钢蓝色背景
}
4. 禁用颜色
在某些情况下(如非TTY终端),你可能想禁用颜色输出:
func disableColor() {
c := color.New()
c.Disable()
// 即使设置了颜色也不会显示
fmt.Println(c.Red("这段文本不会有颜色"))
}
高级用法
1. 链式调用
gommon/color支持链式调用,可以组合多种样式:
func chainExample() {
c := color.New()
// 红色加粗带下划线的文本
fmt.Println(c.Red("警告信息").Bold().Underline())
// 青色背景的黄色文本
fmt.Println(c.Yellow("注意").BgCyan())
}
2. 格式化输出
func formatExample() {
c := color.New()
// 使用Printf风格的格式化
fmt.Printf("%s %s\n", c.Red("错误:"), "文件未找到")
// 使用Sprintf
msg := fmt.Sprintf("%s 代码: %d", c.Green("成功"), 200)
fmt.Println(msg)
}
3. 预定义样式
你可以创建并复用样式:
func predefinedStyles() {
c := color.New()
// 定义错误样式
errorStyle := func(text string) string {
return c.Red("ERROR: " + text).Bold().String()
}
// 定义成功样式
successStyle := func(text string) string {
return c.Green("SUCCESS: " + text).Underline().String()
}
fmt.Println(errorStyle("发生了一个错误"))
fmt.Println(successStyle("操作完成"))
}
实际应用示例
package main
import (
"fmt"
"github.com/gommon/color"
"os"
)
func main() {
c := color.New()
// 检查是否是终端输出
if fileInfo, _ := os.Stdout.Stat(); (fileInfo.Mode() & os.ModeCharDevice) == 0 {
c.Disable()
}
// 模拟日志输出
fmt.Printf("[%s] %s\n", c.Blue("INFO"), "应用程序启动")
fmt.Printf("[%s] %s\n", c.Yellow("WARN"), "配置项缺失,使用默认值")
fmt.Printf("[%s] %s\n", c.Red("ERROR"), "无法连接到数据库")
// 进度条模拟
fmt.Print(c.Green("处理中: "))
for i := 0; i <= 100; i += 10 {
fmt.Print(c.Cyan(fmt.Sprintf("[%-10s]", strings.Repeat("=", i/10))))
fmt.Printf(" %d%%\r", i)
time.Sleep(300 * time.Millisecond)
}
fmt.Println()
// 表格数据展示
fmt.Println(c.Bold("用户列表:"))
fmt.Printf("%-20s %-10s %s\n",
c.Underline("用户名"),
c.Underline("年龄"),
c.Underline("状态"))
fmt.Printf("%-20s %-10s %s\n",
"Alice",
c.Green("28"),
c.Green("活跃"))
fmt.Printf("%-20s %-10s %s\n",
"Bob",
c.Yellow("32"),
c.Yellow("待激活"))
fmt.Printf("%-20s %-10s %s\n",
"Charlie",
c.Red("45"),
c.Red("禁用"))
}
注意事项
- 不是所有终端都支持所有颜色和样式
- 在Windows上可能需要额外配置才能正常显示颜色
- 考虑添加颜色禁用选项,以便在重定向输出到文件时不会包含颜色代码
- 过度使用颜色可能会降低可读性而非提高
gommon/color是一个轻量级的解决方案,适合需要简单终端美化的场景。如果需要更复杂的功能(如进度条、表格等),可以考虑更全面的库如charmbracelet/lipgloss或gookit/color。