- Notifications
You must be signed in to change notification settings - Fork90
🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染
License
gookit/color
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
A command-line color library with 16/256/True color support, universal API methods and Windows support.
Basic color preview:
Now, 256 colors and RGB colors have also been supported to work in Windows CMD and PowerShell:
- Simple to use, zero dependencies
- Supports rich color output: 16-color (4-bit), 256-color (8-bit), true color (24-bit, RGB)
- 16-color output is the most commonly used and most widely supported, working on any Windows version
- Since
v1.2.4
the 256-color (8-bit), true color (24-bit) support windows CMD and PowerShell - Seethis gist for information on true color support
- Support converts
HEX
HSL
value to RGB color - Generic API methods:
Print
,Printf
,Println
,Sprint
,Sprintf
- Supports HTML tag-style color rendering, such as
<green>message</> <fg=red;bg=blue>text</>
.- In addition to using built-in tags, it also supports custom color attributes
- Custom color attributes support the use of 16 color names, 256 color values, rgb color values and hex color values
- Support working on Windows
cmd
andpowerShell
terminal
- Basic colors:
Bold
,Black
,White
,Gray
,Red
,Green
,Yellow
,Blue
,Magenta
,Cyan
- Additional styles:
Info
,Note
,Light
,Error
,Danger
,Notice
,Success
,Comment
,Primary
,Warning
,Question
,Secondary
- Support by set
NO_COLOR
for disable color or useFORCE_COLOR
for force open color render. - Support Rgb, 256, 16 color conversion
go get github.com/gookit/color
package mainimport ("fmt""github.com/gookit/color")funcmain() {// quick use package funccolor.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")// quick use like 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 funcred:=color.FgRed.Rendergreen:=color.FgGreen.Renderfmt.Printf("%s line %s library\n",red("Command"),green("color"))// custom colorcolor.New(color.FgWhite,color.BgBlack).Println("custom color style")// can also:color.Style{color.FgCyan,color.OpBold}.Println("custom color style")// internal theme/style:color.Info.Tips("message")color.Info.Prompt("message")color.Info.Println("message")color.Warn.Println("message")color.Error.Println("message")// use style tagcolor.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n")// Custom label attr: Supports the use of 16 color names, 256 color values, rgb color values and hex color valuescolor.Println("<fg=11aa23>he</><bg=120,35,156>llo</>, <fg=167;bg=232>wel</><fg=red>come</>")// apply a style tagcolor.Tag("info").Println("info style text")// prompt messagecolor.Info.Prompt("prompt style message")color.Warn.Prompt("prompt style message")// tips messagecolor.Info.Tips("tips style message")color.Warn.Tips("tips style message")}
Run demo:go run ./_examples/demo.go
Supported on any Windows version. Provide generic API methods:Print
,Printf
,Println
,Sprint
,Sprintf
color.Bold.Println("bold message")color.Cyan.Println("yellow message")color.Yellow.Println("yellow message")color.Magenta.Println("yellow message")// Only use foreground colorcolor.FgCyan.Printf("Simple to use %s\n","color")// Only use background colorcolor.BgRed.Printf("Simple to use %s\n","color")
Run demo:go run ./_examples/color_16.go
// Full custom: foreground, background, optionmyStyle:=color.New(color.FgWhite,color.BgBlack,color.OpBold)myStyle.Println("custom color style")// can also:color.Style{color.FgCyan,color.OpBold}.Println("custom color style")
custom set console settings:
// set console colorcolor.Set(color.FgCyan)// print messagefmt.Print("message")// reset console settingscolor.Reset()
provide generic API methods:Print
,Printf
,Println
,Sprint
,Sprintf
print message use defined style:
color.Info.Println("Info message")color.Notice.Println("Notice message")color.Error.Println("Error message")// ...
Run demo:go run ./_examples/theme_basic.go
Tips style
color.Info.Tips("Info tips message")color.Notice.Tips("Notice tips message")color.Error.Tips("Error tips message")color.Secondary.Tips("Secondary tips message")
Run demo:go run ./_examples/theme_tips.go
Prompt Style
color.Info.Prompt("Info prompt message")color.Notice.Prompt("Notice prompt message")color.Error.Prompt("Error prompt message")// ...
Run demo:go run ./_examples/theme_prompt.go
Block Style
color.Danger.Block("Danger block message")color.Warn.Block("Warn block message")// ...
Run demo:go run ./_examples/theme_block.go
256 colors support Windows CMD, PowerShell environment after
v1.2.4
color.C256(val uint8, isBg ...bool) Color256
c:=color.C256(132)// fg colorc.Println("message")c.Printf("format %s","message")c:=color.C256(132,true)// bg colorc.Println("message")c.Printf("format %s","message")
Can be used to set foreground and background colors at the same time.
S256(fgAndBg ...uint8) *Style256
s:=color.S256(32,203)s.Println("message")s.Printf("format %s","message")
with options:
s:=color.S256(32,203)s.SetOpts(color.Opts{color.OpBold})s.Println("style with options")s.Printf("style with %s\n","options")
Run demo:go run ./_examples/color_256.go
RGB colors support Windows
CMD
,PowerShell
environment afterv1.2.4
Preview:
Run demo:
Run demo: go run ./_examples/color_rgb.go
example:
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 colorc.Println("message")c.Printf("format %s","message")c:=color.RGB(30,144,255,true)// bg colorc.Println("message")c.Printf("format %s","message")
Create a style from an hexadecimal color string:
color.HEX(hex string, isBg ...bool) RGBColor
c:=color.HEX("ccc")// can also: "cccccc" "#cccccc"c.Println("message")c.Printf("format %s","message")c=color.HEX("aabbcc",true)// as bg colorc.Println("message")c.Printf("format %s","message")
Can be used to set the foreground and background colors at the same time.
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")
Create a style from an hexadecimal color string:
color.HEXStyle(fg string, bg ...string) *RGBStyle
s:=color.HEXStyle("11aa23","eee")s.Println("message")s.Printf("format %s","message")
with options:
s:=color.HEXStyle("11aa23","eee")s.SetOpts(color.Opts{color.OpBold})s.Println("style with options")s.Printf("style with %s\n","options")
Print,Printf,Println
functions support auto parse and render color tags.
text:=` <mga1>gookit/color:</> A <green>command-line</> <cyan>color library</> with <fg=167;bg=232>256-color</> and <fg=11aa23;op=bold>True-color</> support, <fg=mga;op=i>universal API</> methods and <cyan>Windows</> support.`color.Print(text)
Preview, code please see_examples/demo_tag.go:
Tag formats:
- Use built in tags:
<TAG_NAME>CONTENT</>
e.g:<info>message</>
- Custom tag attributes:
<fg=VALUE;bg=VALUE;op=VALUES>CONTENT</>
e.g:<fg=167;bg=232>wel</>
Supported on Windows
cmd.exe
PowerShell
.
Examples:
// use style tagcolor.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>")color.Println("<suc>hello</>")color.Println("<error>hello</>")color.Println("<warning>hello</>")// custom color attributescolor.Print("<fg=yellow;bg=black;op=underscore;>hello, welcome</>\n")// Custom label attr: Supports the use of 16 color names, 256 color values, rgb color values and hex color valuescolor.Println("<fg=11aa23>he</><bg=120,35,156>llo</>, <fg=167;bg=232>wel</><fg=red>come</>")
tag attributes format:
attr format: // VALUE please see var: FgColors, BgColors, AllOptions "fg=VALUE;bg=VALUE;op=VALUE"16 color: "fg=yellow" "bg=red" "op=bold,underscore" // option is allow multi value "fg=white;bg=blue;op=bold" "fg=white;op=bold,underscore"256 color: "fg=167" "fg=167;bg=23" "fg=167;bg=23;op=bold" True color: // hex "fg=fc1cac" "fg=fc1cac;bg=c2c3c4" // r,g,b "fg=23,45,214" "fg=23,45,214;bg=109,99,88"
tag attributes parse please see
func ParseCodeFromAttr()
Built-in tags please see varcolorTags
incolor_tag.go
// use style tagcolor.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>")color.Println("<suc>hello</>")color.Println("<error>hello</>")
Run demo:go run ./_examples/color_tag.go
Usecolor.Tag
build message:
// set a style tagcolor.Tag("info").Print("info style text")color.Tag("info").Printf("%s style text","info")color.Tag("info").Println("info style text")
Supports conversion between Rgb, 256, 16 colors,Rgb <=> 256 <=> 16
basic:=color.Redbasic.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")
color
has many built-in color conversion utility functions.
funcBasic2hex(valuint8)stringfuncBg2Fg(valuint8)uint8funcFg2Bg(valuint8)uint8funcC256ToRgb(valuint8) (rgb []uint8)funcC256ToRgbV1(valuint8) (rgb []uint8)funcHex2basic(hexstring,asBg...bool)uint8funcHex2rgb(hexstring) []intfuncHexToRGB(hexstring) []intfuncHexToRgb(hexstring) (rgb []int)funcHslIntToRgb(h,s,lint) (rgb []uint8)funcHslToRgb(h,s,lfloat64) (rgb []uint8)funcHsvToRgb(h,s,vint) (rgb []uint8)funcRgb2ansi(r,g,buint8,isBgbool)uint8funcRgb2basic(r,g,buint8,isBgbool)uint8funcRgb2hex(rgb []int)stringfuncRgb2short(r,g,buint8)uint8funcRgbTo256(r,g,buint8)uint8funcRgbTo256Table()map[string]uint8funcRgbToAnsi(r,g,buint8,isBgbool)uint8funcRgbToHex(rgb []int)stringfuncRgbToHsl(r,g,buint8) []float64funcRgbToHslInt(r,g,buint8) []int
Convert toRGBColor
:
func RGBFromSlice(rgb []uint8, isBg ...bool) RGBColor
func RGBFromString(rgb string, isBg ...bool) RGBColor
func HEX(hex string, isBg ...bool) RGBColor
func HSL(h, s, l float64, isBg ...bool) RGBColor
func HSLInt(h, s, l int, isBg ...bool) RGBColor
There are some useful functions reference
Disable()
disable color renderSetOutput(io.Writer)
custom set the colored text output writerForceOpenColor()
force open color renderColors2code(colors ...Color) string
Convert colors to code. return like "32;45;3"ClearCode(str string) string
Use for clear color codesClearTag(s string) string
clear all color html-tag for a stringIsConsole(w io.Writer)
Determine whether w is one of stderr, stdout, stdin
More useful func please seehttps://pkg.go.dev/github.com/gookit/color
color
automatically checks the color levels supported by the current environment.
// Level is the color level supported by a terminal.typeLevel= terminfo.ColorLevel// terminal color available level alias of the terminfo.ColorLevel*const (LevelNo=terminfo.ColorLevelNone// not support color.Level16=terminfo.ColorLevelBasic// basic - 3/4 bit color supportedLevel256=terminfo.ColorLevelHundreds// hundreds - 8-bit color supportedLevelRgb=terminfo.ColorLevelMillions// millions - (24 bit)true color supported)
func SupportColor() bool
Whether the current environment supports color outputfunc Support256Color() bool
Whether the current environment supports 256-color outputfunc SupportTrueColor() bool
Whether the current environment supports (RGB)True-color outputfunc TermColorLevel() Level
Get the currently supported color level
Check out these projects, which usehttps://github.com/gookit/color :
- https://github.com/Delta456/box-cli-maker Make Highly Customized Boxes for your CLI
- https://github.com/flipped-aurora/gin-vue-admin 基于gin+vue搭建的(中)后台系统框架
- https://github.com/JanDeDobbeleer/oh-my-posh A prompt theme engine for any shell.
- https://github.com/jesseduffield/lazygit Simple terminal UI for git commands
- https://github.com/olivia-ai/olivia 💁♀️Your new best friend powered by an artificial neural network
- https://github.com/pterm/pterm PTerm is a modern Go module to beautify console output. Featuring charts, progressbars, tables, trees, etc.
- https://github.com/securego/gosec Golang security checker
- https://github.com/TNK-Studio/lazykube ⎈ The lazier way to manage kubernetes.
- + See More
- gookit/ini Go config management, use INI files
- gookit/rux Simple and fast request router for golang HTTP
- gookit/gcli build CLI application, tool library, running CLI commands
- gookit/slog Concise and extensible go log library
- gookit/event Lightweight event manager and dispatcher implements by Go
- gookit/cache Generic cache use and cache manager for golang. support File, Memory, Redis, Memcached.
- gookit/config Go config management. support JSON, YAML, TOML, INI, HCL, ENV and Flags
- gookit/color A command-line color library with true color support, universal API methods and Windows support
- gookit/filter Provide filtering, sanitizing, and conversion of golang data
- gookit/validate Use for data validation and filtering. support Map, Struct, Form data
- gookit/goutil Some utils for the Go: string, array/slice, map, format, cli, env, filesystem, test and more
- More, please seehttps://github.com/gookit
About
🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染