mfer/vendor/github.com/gookit/color/README.zh-CN.md

14 KiB
Raw Blame History

CLI Color

GitHub go.mod Go version Actions Status Codacy Badge GoDoc GitHub tag (latest SemVer) Build Status Coverage Status Go Report Card

Golang下的命令行色彩使用库, 拥有丰富的色彩渲染输出通用的API方法兼容Windows系统

EN README

基本颜色预览:

basic-color

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

color-on-cmd-pwsh

功能特色

  • 使用简单方便
  • 支持丰富的颜色输出, 16色(4bit)256色(8bit)RGB色彩(24bit, RGB)
    • 16色(4bit)是最常用和支持最广的支持Windows cmd.exe
    • v1.2.4256色(8bit)RGB色彩(24bit)均支持Windows CMD和PowerShell终端
    • 请查看 this gist 了解支持RGB色彩的终端
  • 提供通用的API方法Print Printf Println Sprint Sprintf
  • 同时支持html标签式的颜色渲染除了使用内置标签同时支持自定义颜色属性
    • 例如: this an <green>message</> 标签内部的文本将会渲染为绿色字体
    • 自定义颜色属性: 支持使用16色彩名称256色彩值rgb色彩值以及hex色彩值
  • 基础色彩: Bold Black White Gray Red Green Yellow Blue Magenta Cyan
  • 扩展风格: Info Note Light Error Danger Notice Success Comment Primary Warning Question Secondary
  • 支持通过设置环境变量 NO_COLOR 来禁用色彩,或者使用 FORCE_COLOR 来强制使用色彩渲染.
  • 支持 Rgb, 256, 16 色彩之间的互相转换
  • 支持Linux、Mac同时兼容Windows系统环境

GoDoc

安装

go get github.com/gookit/color

快速开始

如下,引入当前包就可以快速的使用

package main

import (
	"fmt"
	
	"github.com/gookit/color"
)

func main() {
	// 简单快速的使用,跟 fmt.Print* 类似
	color.Redp("Simple to use color")
	color.Redln("Simple to use color")
	color.Greenp("Simple to use color\n")
	color.Cyanln("Simple to use color")
	color.Yellowln("Simple to use color")

	// 简单快速的使用,跟 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")
	// 自定义标签: 支持使用16色彩名称256色彩值rgb色彩值以及hex色彩值
	color.Println("<fg=11aa23>he</><bg=120,35,156>llo</>, <fg=167;bg=232>wel</><fg=red>come</>")

	// 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

colored-out

基础颜色(16-color)

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

支持在windows cmd.exe powerShell 等终端使用

color.Bold.Println("bold message")
color.Black.Println("bold message")
color.White.Println("bold message")
color.Gray.Println("bold message")
color.Red.Println("yellow message")
color.Blue.Println("yellow message")
color.Cyan.Println("yellow message")
color.Yellow.Println("yellow message")
color.Magenta.Println("yellow message")

// Only use foreground color
color.FgCyan.Printf("Simple to use %s\n", "color")
// Only use background color
color.BgRed.Printf("Simple to use %s\n", "color")

运行demo: go run ./_examples/color_16.go

basic-color

构建风格

// 仅设置前景色
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已经内置丰富的色彩风格支持

扩展风格方法

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

支持在windows cmd.exe powerShell 等终端使用

基础使用:

// print message
color.Info.Println("Info message")
color.Note.Println("Note message")
color.Notice.Println("Notice message")
color.Error.Println("Error message")
color.Danger.Println("Danger message")
color.Warn.Println("Warn message")
color.Debug.Println("Debug message")
color.Primary.Println("Primary message")
color.Question.Println("Question message")
color.Secondary.Println("Secondary message")

Run demo: go run ./_examples/theme_basic.go

theme-basic

简约提示风格

color.Info.Tips("Info tips message")
color.Note.Tips("Note tips message")
color.Notice.Tips("Notice tips message")
color.Error.Tips("Error tips message")
color.Danger.Tips("Danger tips message")
color.Warn.Tips("Warn tips message")
color.Debug.Tips("Debug tips message")
color.Primary.Tips("Primary tips message")
color.Question.Tips("Question tips message")
color.Secondary.Tips("Secondary tips message")

Run demo: go run ./_examples/theme_tips.go

theme-tips

着重提示风格

color.Info.Prompt("Info prompt message")
color.Note.Prompt("Note prompt message")
color.Notice.Prompt("Notice prompt message")
color.Error.Prompt("Error prompt message")
color.Danger.Prompt("Danger prompt message")

Run demo: go run ./_examples/theme_prompt.go

theme-prompt

强调提示风格

color.Warn.Block("Warn block message")
color.Debug.Block("Debug block message")
color.Primary.Block("Primary block message")
color.Question.Block("Question block message")
color.Secondary.Block("Secondary block message")

Run demo: go run ./_examples/theme_block.go

theme-block

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")

使用256 色彩风格

可同时设置前景和背景色

  • 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/color_256.go

color-tags

RGB/True色彩使用

RGB色彩在 v1.2.4 后支持 Windows CMD, PowerShell 环境

效果预览:

运行 demo: Run demo: go run ./_examples/color_rgb.go

color-rgb

代码示例:

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")

使用RGB风格

可同时设置前景和背景色

  • 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")

使用颜色标签

支持 在windows cmd.exe PowerShell 使用

使用内置的颜色标签,可以非常方便简单的构建自己需要的任何格式

同时支持自定义颜色属性: 支持使用16色彩名称256色彩值rgb色彩值以及hex色彩值

// 使用内置的 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")

// 自定义颜色属性: 支持使用16色彩名称256色彩值rgb色彩值以及hex色彩值
color.Println("<fg=11aa23>he</><bg=120,35,156>llo</>, <fg=167;bg=232>wel</><fg=red>come</>")
  • 使用 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/color_tag.go

color-tags

颜色转换

支持 Rgb, 256, 16 色彩之间的互相转换 Rgb <=> 256 <=> 16

basic := color.Red
basic.Println("basic color")

c256 := color.Red.C256()
c256.Println("256 color")
c256.C16().Println("basic color")

rgb := color.Red.RGB()
rgb.Println("rgb color")
rgb.C256().Println("256 color")

方法参考

一些有用的工具方法参考

  • Disable() disable color render
  • SetOutput(io.Writer) custom set the colored text output writer
  • ForceOpenColor() force open color render
  • ClearCode(str string) string Use for clear color codes
  • Colors2code(colors ...Color) string Convert colors to code. return like "32;45;3"
  • ClearTag(s string) string clear all color html-tag for a string
  • IsConsole(w io.Writer) Determine whether w is one of stderr, stdout, stdin
  • HexToRgb(hex string) (rgb []int) Convert hex color string to RGB numbers
  • RgbToHex(rgb []int) string Convert RGB to hex code
  • 更多请查看文档 https://pkg.go.dev/github.com/gookit/color

使用color的项目

看看这些使用了 https://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 的一些工具函数,格式化,特殊处理,常用信息获取等
  • 更多请查看 https://github.com/gookit

参考项目

License

MIT