# color **Repository Path**: opsfast/color ## Basic Information - **Project Name**: color - **Description**: Golang下的命令行色彩使用库,支持丰富的颜色输出,通用的API方法,支持html标签式的颜色渲染,兼容Windows系统环境 - **Primary Language**: Go - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 8 - **Created**: 2019-03-04 - **Last Updated**: 2020-12-19 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # CLI Color [![GoDoc](https://godoc.org/github.com/gookit/color?status.svg)](https://godoc.org/github.com/gookit/color) [![Build Status](https://travis-ci.org/gookit/color.svg?branch=master)](https://travis-ci.org/gookit/color) [![Coverage Status](https://coveralls.io/repos/github/gookit/color/badge.svg?branch=master)](https://coveralls.io/github/gookit/color?branch=master) [![Go Report Card](https://goreportcard.com/badge/github.com/gookit/color)](https://goreportcard.com/report/github.com/gookit/color) Golang下的命令行色彩使用库, 拥有丰富的色彩渲染输出,通用的API方法,兼容Windows系统 > **[EN README](README.md)** 基本颜色预览: ![basic-color](_examples/images/basic-color.png) ## 功能特色 - 使用简单方便,无其他依赖 - 支持丰富的颜色输出, 16色(4bit),256色(8bit),RGB色彩(24bit) - 16色(4bit)是最常用和支持最广的,支持Windows `cmd.exe` - 另外两种支持 `linux` `mac` 和 Windows下的 `ConEmu` `git-bash` `mintty` 等部分终端 - 通用的API方法:`Print` `Printf` `Println` `Sprint` `Sprintf` - 同时支持html标签式的颜色渲染. eg: `message` - 基础色彩: `Bold` `Black` `White` `Gray` `Red` `Green` `Yellow` `Blue` `Magenta` `Cyan` - 扩展风格: `Info` `Note` `Light` `Error` `Danger` `Notice` `Success` `Comment` `Primary` `Warning` `Question` `Secondary` - 兼容Windows系统环境 ## GoDoc - [godoc for gopkg](https://godoc.org/gopkg.in/gookit/color.v1) - [godoc for github](https://godoc.org/github.com/gookit/color) ## 快速开始 如下,引入当前包就可以快速的使用 ```bash import "gopkg.in/gookit/color.v1" // 推荐 // or import "github.com/gookit/color" ``` ### 如何使用 ```go package main import ( "fmt" "github.com/gookit/color" ) func main() { // simple usage color.Cyan.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("hello, welcome\n") // 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/app.go` ![colored-out](_examples/images/color-demo.jpg) ## 构建风格 ```go // 仅设置前景色 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") ``` ```go // 设置console颜色 color.Set(color.FgCyan) // 输出信息 fmt.Print("message") // 重置console颜色 color.Reset() ``` ## 使用内置风格 ### 基础颜色方法 > 支持在windows `cmd.exe` 使用 - `color.Bold` - `color.Black` - `color.White` - `color.Gray` - `color.Red` - `color.Green` - `color.Yellow` - `color.Blue` - `color.Magenta` - `color.Cyan` ```go color.Bold.Println("bold message") color.Yellow.Println("yellow message") ``` > 运行 demo: `go run ./_examples/basiccolor.go` ![basic-color](_examples/images/basic-color.png) ### 扩展风格方法 > 支持在windows `cmd.exe` 使用 - `color.Info` - `color.Note` - `color.Light` - `color.Error` - `color.Danger` - `color.Notice` - `color.Success` - `color.Comment` - `color.Primary` - `color.Warning` - `color.Question` - `color.Secondary` ```go color.Info.Println("Info message") color.Success.Println("Success message") ``` > 运行 demo: `go run ./_examples/theme_style.go` ![theme-style](_examples/images/theme-style.jpg) ### 使用颜色标签 > **不** 支持在windows `cmd.exe` 使用,但不影响使用,会自动去除颜色标签 使用内置的颜色标签,可以非常方便简单的构建自己需要的任何格式 ```go // 使用内置的 color tag color.Print("hello, welcome") color.Println("hello") color.Println("hello") color.Println("hello") // 自定义颜色属性 color.Print("hello, welcome\n") ``` - 使用 `color.Tag` 给后面输出的文本信息加上给定的颜色风格标签 ```go // 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/colortag.go` ![color-tags](_examples/images/color-tags.jpg) ## 256色使用 ### 使用前景或后景色 - `color.C256(val uint8, isBg ...bool) Color256` ```go 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") ``` ### 使用风格 > 可同时设置前景和背景色 - `color.S256(fgAndBg ...uint8) *Style256` ```go s := color.S256(32, 203) s.Println("message") s.Printf("format %s", "message") ``` > 运行 demo: `go run ./_examples/color256.go` ![color-tags](_examples/images/256-color.jpg) ## RGB色彩使用 ### 使用前景或后景色 - `color.RGB(r, g, b uint8, isBg ...bool) RGBColor` ```go 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进制颜色创建 ```go c := HEX("ccc") // 也可以写为: "cccccc" "#cccccc" c.Println("message") c.Printf("format %s", "message") c = HEX("aabbcc", true) // as bg color c.Println("message") c.Printf("format %s", "message") ``` ### 使用风格 > 可同时设置前景和背景色 - `color.NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyle` ```go s := 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进制颜色创建 ```go s := HEXStyle("11aa23", "eee") s.Println("message") s.Printf("format %s", "message") ``` ## CLI 应用包 **[gookit/gcli](https://github.com/gookit/gcli)** 快速的构建CLI命令应用 ## 参考项目 - `issue9/term` https://github.com/issue9/term - `beego/bee` https://github.com/beego/bee - `inhere/console` https://github/inhere/php-console - [ANSI转义序列](https://zh.wikipedia.org/wiki/ANSI转义序列) - [Standard ANSI color map](https://conemu.github.io/en/AnsiEscapeCodes.html#Standard_ANSI_color_map) ## License MIT