Documentation ¶
Overview ¶
Package color is command line color library, written using golang
Source code and other details for the project are available at GitHub:
https://github.com/gookit/color
More usage please see README and tests.
Example ¶
// simple usage Cyan.Printf("Simple to use %s\n", "color") // use like func red := FgRed.Render green := FgGreen.Render fmt.Printf("%s line %s library\n", red("Command"), green("color")) // custom color New(FgWhite, BgBlack).Println("custom color style") // can also: Style{FgCyan, OpBold}.Println("custom color style") // internal theme/style: Info.Tips("message") Info.Prompt("message") Info.Println("info message") Warn.Println("warning message") Error.Println("error message") Danger.Println("danger message") // use style tag Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n") // set a style tag Tag("info").Println("info style text") // use info style tips Tips("info").Print("tips style text") // use info style blocked tips LiteTips("info").Print("blocked tips style text")
Output:
Index ¶
- Constants
- Variables
- func AddStyle(name string, s Style)
- func AddTheme(name string, style Style)
- func ApplyTag(tag string, a ...interface{}) string
- func Bit24Color(str string)
- func ClearCode(str string) string
- func ClearTag(str string) string
- func Color256Table()
- func Disable()
- func Fprint(w io.Writer, a ...interface{})
- func Fprintf(w io.Writer, format string, a ...interface{}) (int, error)
- func Fprintln(w io.Writer, a ...interface{}) (int, error)
- func GetColorTags() map[string]string
- func GetTagCode(name string) string
- func HexToRGB(hex string) (rgb []int)
- func IsConsole(out io.Writer) bool
- func IsDefinedTag(name string) bool
- func IsLinux() bool
- func IsMSys() bool
- func IsMac() bool
- func IsSupport256Color() bool
- func IsSupportColor() bool
- func IsWin() bool
- func ParseCodeFromAttr(attr string) (code string)
- func Print(a ...interface{})
- func Printf(format string, a ...interface{})
- func Println(a ...interface{})
- func RGBto216(n int) int
- func RGBto256(r, g, b uint8)
- func Render(a ...interface{}) string
- func RenderCode(code string, args ...interface{}) string
- func RenderString(code string, str string) string
- func ReplaceTag(str string, dumpIt ...bool) string
- func Reset() (int, error)
- func Set(colors ...Color) (int, error)
- func Sprint(args ...interface{}) string
- func Sprintf(format string, a ...interface{}) string
- func String(str string) string
- func Text(str string) string
- func TrueColor(str string, rgb RGBColor)
- func WrapTag(str string, tag string) string
- type Color
- func (c Color) IsValid() bool
- func (c Color) Print(args ...interface{})
- func (c Color) Printf(format string, a ...interface{})
- func (c Color) Println(a ...interface{})
- func (c Color) Render(a ...interface{}) string
- func (c Color) Sprint(a ...interface{}) string
- func (c Color) Sprintf(format string, args ...interface{}) string
- func (c Color) String() string
- func (c Color) Text(message string) string
- type Color256
- func (c Color256) IsEmpty() bool
- func (c Color256) Print(a ...interface{})
- func (c Color256) Printf(format string, a ...interface{})
- func (c Color256) Println(a ...interface{})
- func (c Color256) Sprint(a ...interface{}) string
- func (c Color256) Sprintf(format string, a ...interface{}) string
- func (c Color256) String() string
- func (c Color256) Value() uint8
- type LiteTips
- type RGBColor
- func (c RGBColor) IsEmpty() bool
- func (c RGBColor) Print(a ...interface{})
- func (c RGBColor) Printf(format string, a ...interface{})
- func (c RGBColor) Println(a ...interface{})
- func (c RGBColor) Sprint(a ...interface{}) string
- func (c RGBColor) Sprintf(format string, a ...interface{}) string
- func (c RGBColor) String() string
- func (c RGBColor) Values() []int
- type RGBStyle
- type Style
- func (s Style) IsEmpty() bool
- func (s Style) Print(a ...interface{})
- func (s Style) Printf(format string, args ...interface{})
- func (s Style) Println(a ...interface{})
- func (s Style) Render(a ...interface{}) string
- func (s Style) Save(name string)
- func (s Style) Sprint(a ...interface{}) string
- func (s Style) String() string
- type Style256
- func (s *Style256) Print(a ...interface{})
- func (s *Style256) Printf(format string, a ...interface{})
- func (s *Style256) Println(a ...interface{})
- func (s *Style256) Set(fgVal, bgVal uint8) *Style256
- func (s *Style256) SetBg(bgVal uint8) *Style256
- func (s *Style256) SetFg(fgVal uint8) *Style256
- func (s *Style256) Sprint(a ...interface{}) string
- func (s *Style256) Sprintf(format string, a ...interface{}) string
- func (s *Style256) String() string
- type Tag
- type Theme
- type Tips
Examples ¶
Constants ¶
const ( ModeNormal = iota Mode256 // 8 bite ModeRGB // 24 bite ModeGrayscale )
console color mode
const ( SettingTpl = "\x1b[%sm" FullColorTpl = "\x1b[%sm%s\x1b[0m" )
color render templates
const ( Red = FgRed Cyan = FgCyan Gray = FgDarkGray Blue = FgBlue Black = FgBlack Green = FgGreen White = FgWhite Yellow = FgYellow Magenta = FgMagenta Bold = OpBold Normal = FgDefault )
There are basic foreground color alias
const ( TplFg256 = "38;5;%d" TplBg256 = "48;5;%d" )
tpl for 8 bit 256 color(`2^8`)
format:
ESC[ … 38;5;<n> … m // 选择前景色 ESC[ … 48;5;<n> … m // 选择背景色
example:
fg "\x1b[38;5;242m" bg "\x1b[48;5;208m" both "\x1b[38;5;242;48;5;208m"
links:
https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97#8位
const ( TplFgRGB = "38;2;%d;%d;%d" TplBgRGB = "48;2;%d;%d;%d" )
TplFgRGB 24 bit RGB color RGB:
R 0-255 G 0-255 B 0-255 R 00-FF G 00-FF B 00-FF (16进制)
format:
ESC[ … 38;2;<r>;<g>;<b> … m // 选择RGB前景色 ESC[ … 48;2;<r>;<g>;<b> … m // 选择RGB背景色
links:
https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97#24位
example:
fg: \x1b[38;2;30;144;255mMESSAGE\x1b[0m bg: \x1b[48;2;30;144;255mMESSAGE\x1b[0m both: \x1b[38;2;233;90;203;48;2;30;144;255mMESSAGE\x1b[0m
const ( AsFg uint8 = iota AsBg )
mark color is fg or bg
const ( // Regex to match color tags // golang 不支持反向引用. 即不支持使用 \1 引用第一个匹配 ([a-z=;]+) // MatchExpr = `<([a-z=;]+)>(.*?)<\/\1>` // 所以调整一下 统一使用 `</>` 来结束标签,例如 "<info>some text</>" // 支持自定义颜色属性的tag "<fg=white;bg=blue;op=bold>content</>" // (?s:...) s - 让 "." 匹配换行 MatchExpr = `<([a-zA-Z_=,;]+)>(?s:(.*?))<\/>` // Regex to match color attributes AttrExpr = `(fg|bg|op)=([a-z,]+);?` // Regex used for removing color tags // StripExpr = `<[\/]?[a-zA-Z=;]+>` // 随着上面的做一些调整 StripExpr = `<[\/]?[a-zA-Z_=,;]*>` )
output colored text like use html tag. (not support windows cmd)
const CodeExpr = `\033\[[\d;?]+m`
CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m"
const ResetCode = "0"
ResetCode value
const ResetSet = "\x1b[0m"
ResetSet 重置/正常 关闭所有属性。
Variables ¶
var ( // Info color style Info = &Theme{"info", Style{OpReset, FgGreen}} // Note color style Note = &Theme{"note", Style{OpBold, FgLightCyan}} // Warn color style Warn = &Theme{"warning", Style{OpBold, FgYellow}} // Light color style Light = &Theme{"light", Style{FgLightWhite, BgBlack}} // Error color style Error = &Theme{"error", Style{FgLightWhite, BgRed}} // Danger color style Danger = &Theme{"danger", Style{OpBold, FgRed}} // Notice color style Notice = &Theme{"notice", Style{OpBold, FgCyan}} // Comment color style Comment = &Theme{"comment", Style{OpReset, FgLightYellow}} // Success color style Success = &Theme{"success", Style{OpBold, FgGreen}} // Primary color style Primary = &Theme{"primary", Style{OpReset, FgBlue}} // Question color style Question = &Theme{"question", Style{OpReset, FgMagenta}} // Secondary color style Secondary = &Theme{"secondary", Style{FgDarkGray}} )
internal themes(like bootstrap style) Usage:
color.Info.Print("message") color.Info.Printf("a %s message", "test") color.Warn.Println("message") color.Error.Println("message")
var BgColors = map[string]Color{ "black": BgBlack, "red": BgRed, "green": BgGreen, "yellow": BgYellow, "blue": BgBlue, "magenta": BgMagenta, "cyan": BgCyan, "white": BgWhite, "default": BgDefault, }
BgColors background colors map
var Enable = true
Enable switch color display
var ExBgColors = map[string]Color{ "darkGray": BgDarkGray, "lightRed": BgLightRed, "lightGreen": BgLightGreen, "lightYellow": BgLightYellow, "lightBlue": BgLightBlue, "lightMagenta": BgLightMagenta, "lightCyan": BgLightCyan, "lightWhite": BgLightWhite, }
ExBgColors extra background colors map
var ExFgColors = map[string]Color{ "darkGray": FgDarkGray, "lightRed": FgLightRed, "lightGreen": FgLightGreen, "lightYellow": FgLightYellow, "lightBlue": FgLightBlue, "lightMagenta": FgLightMagenta, "lightCyan": FgLightCyan, "lightWhite": FgLightWhite, }
ExFgColors extra foreground colors map
var FgColors = map[string]Color{ "black": FgBlack, "red": FgRed, "green": FgGreen, "yellow": FgYellow, "blue": FgBlue, "magenta": FgMagenta, "cyan": FgCyan, "white": FgWhite, "default": FgDefault, }
FgColors foreground colors map
var Options = map[string]Color{ "reset": OpReset, "bold": OpBold, "fuzzy": OpFuzzy, "italic": OpItalic, "underscore": OpUnderscore, "blink": OpBlink, "reverse": OpReverse, "concealed": OpConcealed, }
Options color options map
var Styles = map[string]Style{ "info": {OpReset, FgGreen}, "note": {OpBold, FgLightCyan}, "light": {FgLightWhite, BgRed}, "error": {FgLightWhite, BgRed}, "danger": {OpBold, FgRed}, "notice": {OpBold, FgCyan}, "success": {OpBold, FgGreen}, "comment": {OpReset, FgMagenta}, "primary": {OpReset, FgBlue}, "warning": {OpBold, FgYellow}, "question": {OpReset, FgMagenta}, "secondary": {FgDarkGray}, }
Styles internal defined styles, like bootstrap styles. usage:
color.Styles["info"].Println("message")
var Themes = map[string]*Theme{ "info": Info, "note": Note, "light": Light, "error": Error, "danger": Danger, "notice": Notice, "success": Success, "comment": Comment, "primary": Primary, "warning": Warn, "question": Question, "secondary": Secondary, }
Themes internal defined themes. usage:
color.Themes["info"].Println("message")
Functions ¶
func GetTagCode ¶ added in v1.1.2
GetTagCode get color code by tag name
func HexToRGB ¶ added in v1.1.2
HexToRGB hex color string to RGB numbers Usage:
rgb := HexToRGB("ccc") // rgb: [204 204 204] rgb := HexToRGB("aabbcc") // rgb: [170 187 204] rgb := HexToRGB("0xad99c0") // rgb: [170 187 204]
func IsSupportColor ¶ added in v1.1.2
func IsSupportColor() bool
IsSupportColor check console is support color. supported: linux, mac, or windows's ConEmu, Cmder, putty, git-bash.exe not support: windows cmd, powerShell
func ParseCodeFromAttr ¶
ParseCodeFromAttr parse color attributes. attr like:
"fg=VALUE;bg=VALUE;op=VALUE", VALUE please see var: FgColors, BgColors, Options
eg:
"fg=yellow" "bg=red" "op=bold,underscore" option is allow multi value "fg=white;bg=blue;op=bold" "fg=white;op=bold,underscore"
func RGBto216 ¶ added in v1.1.2
RGBto216 16-231:6 × 6 × 6 立方(216色): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5)
func RenderCode ¶ added in v1.1.2
RenderCode render message by color code. Usage:
msg := RenderCode("3;32;45", "some", "message")
func RenderString ¶ added in v1.1.2
RenderString render a string with color code. Usage:
msg := RenderString("3;32;45", "a message")
func ReplaceTag ¶
ReplaceTag parse string, replace tag and return rendered string
Types ¶
type Color ¶
type Color uint8
Color value type 3(2^3=8) OR 4(2^4=16) bite color. ESC 操作的表示:
"\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)
const ( FgBlack Color = iota + 30 FgRed FgGreen FgYellow FgBlue FgMagenta // 品红 FgCyan // 青色 FgWhite // FgDefault revert default FG FgDefault Color = 39 )
Foreground colors. basic foreground colors 30 - 37
const ( FgDarkGray Color = iota + 90 // 亮黑(灰) FgLightRed FgLightGreen FgLightYellow FgLightBlue FgLightMagenta FgLightCyan FgLightWhite // FgGray is alias of FgDarkGray FgGray Color = 90 // 亮黑(灰) )
Extra foreground color 90 - 97(非标准)
const ( BgBlack Color = iota + 40 BgRed BgGreen BgYellow // BgBrown like yellow BgBlue BgMagenta BgCyan BgWhite // BgDefault revert default BG BgDefault Color = 49 )
Background colors. basic background colors 40 - 47
const ( BgDarkGray Color = iota + 99 BgLightRed BgLightGreen BgLightYellow BgLightBlue BgLightMagenta BgLightCyan BgLightWhite // BgGray is alias of BgDarkGray BgGray Color = 100 )
Extra background color 100 - 107(非标准)
const ( OpReset Color = iota // 0 重置所有设置 OpBold // 1 加粗 OpFuzzy // 2 模糊(不是所有的终端仿真器都支持) OpItalic // 3 斜体(不是所有的终端仿真器都支持) OpUnderscore // 4 下划线 OpBlink // 5 闪烁 OpFastBlink // 5 快速闪烁(未广泛支持) OpReverse // 7 颠倒的 交换背景色与前景色 OpConcealed // 8 隐匿的 OpStrikethrough // 9 删除的,删除线(未广泛支持) )
Option settings
func (Color) Print ¶
func (c Color) Print(args ...interface{})
Print messages. Usage:
color.Green.Print("message")
OR:
green := color.FgGreen.Print green("message")
func (Color) Printf ¶
Printf format and print messages. usage:
color.Cyan.Printf("string %s", "arg0")
func (Color) Render ¶
Render messages by color setting usage:
green := color.FgGreen.Render fmt.Println(green("message"))
func (Color) Sprint ¶ added in v1.1.2
Sprint render messages by color setting. is alias of the Render()
type Color256 ¶ added in v1.1.2
type Color256 [2]uint8
Color256 256 (8 bit) color, uint8 range at 0 - 255
颜色值使用10进制和16进制都可 0x98 = 152
颜色有两位uint8组成,
0: color value 1: color type, Fg=0 Bg=1 >1: unset value fg color: [152, 0] bg color: [152, 1]
func (Color256) Print ¶ added in v1.1.2
func (c Color256) Print(a ...interface{})
Print print message
func (Color256) Println ¶ added in v1.1.2
func (c Color256) Println(a ...interface{})
Println print message with newline
type LiteTips ¶
type LiteTips string
LiteTips will only add color for tag name value is a defined style name
type RGBColor ¶ added in v1.1.2
type RGBColor [4]uint8
RGBColor definition.
The first to third digits represent the color value. The last digit represents the foreground(0), background(1), >1 is unset value
Usage:
// 0, 1, 2 is R,G,B. // 3th: Fg=0, Bg=1, >1: unset value RGBColor{30,144,255, 0} RGBColor{30,144,255, 1}
func (RGBColor) Print ¶ added in v1.1.2
func (c RGBColor) Print(a ...interface{})
Print print message
func (RGBColor) Println ¶ added in v1.1.2
func (c RGBColor) Println(a ...interface{})
Println print message with newline
type RGBStyle ¶ added in v1.1.2
type RGBStyle struct { Name string // contains filtered or unexported fields }
RGBStyle definition.
前/背景色 都是由4位uint8组成, 前三位是色彩值; 最后一位与RGBColor不一样的是,在这里表示是否设置了值
type Style ¶
type Style []Color
Style a 16 color style can add: fg color, bg color, color options Example:
color.Style(color.FgGreen).Print("message")
func (Style) Render ¶
Render render text usage:
color.New(color.FgGreen).Render("text") color.New(color.FgGreen, color.BgBlack, color.OpBold).Render("text")
type Style256 ¶ added in v1.1.2
type Style256 struct { Name string // contains filtered or unexported fields }
Style256 definition
前/背景色 都是由两位uint8组成, 第一位是色彩值; 第二位与Bit8Color不一样的是,在这里表示是否设置了值 0 未设置 ^0 已设置
func S256 ¶ added in v1.1.2
S256 create a color256 style Usage:
s := color.S256() s := color.S256(132) // fg s := color.S256(132, 203) // fg and bg
func (*Style256) Print ¶ added in v1.1.2
func (s *Style256) Print(a ...interface{})
Print print message
func (*Style256) Println ¶ added in v1.1.2
func (s *Style256) Println(a ...interface{})
Println print message with newline
type Tag ¶
type Tag string
Tag value is a defined style name
type Theme ¶ added in v1.1.1
Theme definition. extends from Style