同步操作将从 gookit/color 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
Golang下的命令行色彩使用库, 拥有丰富的色彩渲染输出,通用的API方法,兼容Windows系统
基本颜色预览:
现在,256色和RGB色彩也已经支持windows CMD和PowerShell中工作:
cmd.exe
v1.2.4
起 256色(8bit),RGB色彩(24bit)均支持windows CMD和PowerShell终端
Print
Printf
Println
Sprint
Sprintf
<green>message</>
Bold
Black
White
Gray
Red
Green
Yellow
Blue
Magenta
Cyan
Info
Note
Light
Error
Danger
Notice
Success
Comment
Primary
Warning
Question
Secondary
go get github.com/gookit/color
如果要在Windows上打印颜色消息,应使用 color.PrintX
而不是 fmt.PrintX
str := color.Red.Sprint("an colored message string")
// Windows 下将不会输出颜色
fmt.Println(str)
// Windows 也可以输出色彩
color.Println(str)
color.PrintX
系列方法是通用的,您可以直接使用它们替代fmt.PrintX
方法
如下,引入当前包就可以快速的使用
package main
import (
"fmt"
"github.com/gookit/color"
)
func main() {
// 简单快速的使用,跟 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")
// 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
// 仅设置前景色
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已经内置丰富的色彩风格支持
支持在windows
cmd.exe
使用
color.Bold
color.Black
color.White
color.Gray
color.Red
color.Green
color.Yellow
color.Blue
color.Magenta
color.Cyan
color.Bold.Println("bold message")
color.Yellow.Println("yellow message")
运行demo:
go run ./_examples/basiccolor.go
支持在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
// print message
color.Info.Println("Info message")
color.Success.Println("Success message")
Run demo: go run ./_examples/theme_basic.go
color.Info.Tips("tips style message")
color.Warn.Tips("tips style message")
Run demo: go run ./_examples/theme_tips.go
color.Info.Prompt("prompt style message")
color.Warn.Prompt("prompt style message")
Run demo: go run ./_examples/theme_prompt.go
color.Info.Block("prompt style message")
color.Warn.Block("prompt style message")
Run demo: go run ./_examples/theme_block.go
支持 在windows
cmd.exe
PowerShell
使用
使用内置的颜色标签,可以非常方便简单的构建自己需要的任何格式
// 使用内置的 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")
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/colortag.go
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")
可同时设置前景和背景色
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/color256.go
RGB色彩在
v1.2.4
后支持 WindowsCMD
,PowerShell
环境
效果预览:
运行 demo:
Run demo: go run ./_examples/color_rgb.go
代码示例:
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")
可同时设置前景和背景色
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")
一些有用的工具方法参考
Disable()
disable color renderSetOutput(io.Writer)
custom set the colored text output writerForceOpenColor()
force open color renderClearCode(str string) string
Use for clear color codesColors2code(colors ...Color) string
Convert colors to code. return like "32;45;3"ClearTag(s string) string
clear all color html-tag for a stringIsConsole(w io.Writer)
Determine whether w is one of stderr, stdout, stdinHexToRgb(hex string) (rgb []int)
Convert hex color string to RGB numbersRgbToHex(rgb []int) string
Convert RGB to hex codeMIT
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。