README
¶
cli color
Command line color library, written using golang
Features
- Easy to use
- supports rich color output
- supports html tab-style color rendering
- compatible with Windows
Install
- use dep
dep ensure -add gopkg.in/gookit/color.v1 // is recommended
// OR
dep ensure -add github.com/gookit/color
- go get
go get gopkg.in/gookit/color.v1 // is recommended
// OR
go get -u github.com/gookit/color
- git clone
git clone https://github.com/gookit/color
Godoc
Quick start
import "gopkg.in/gookit/color.v1" // is recommended
// or
import "github.com/gookit/color"
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"))
// custom color
color.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 tag
color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n")
// set a style tag
color.Tag("info").Println("info style text")
// use info style tips
color.Tips("info").Print("tips style text")
// use info style blocked tips
color.LiteTips("info").Print("blocked tips style text")
}
run demo: go run ./_examples/app.go
Color output display
More usage
Basic color usage
support on 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")
Extra styles
support on 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
color.Info.Print("Info message")
color.Success.Print("Success message")
Use like html tag
not support on windows
cmd.exe
// use style tag
color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>")
color.Println("<suc>hello</>")
color.Println("<error>hello</>")
color.Println("<warning>hello</>")
// custom color attributes
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")
Internal color tags
// Some internal defined style tags
// usage: <tag>content text</>
// basic tags
- red
- blue
- cyan
- black
- green
- brown
- white
- default // no color
- normal// no color
- yellow
- magenta
// alert tags like bootstrap's alert
- suc // same "green" and "bold"
- success
- info // same "green"
- comment // same "brown"
- note
- notice
- warn
- warning
- primary
- danger // same "red"
- err
- error
// more tags
- lightRed
- light_red
- lightGreen
- light_green
- lightBlue
- light_blue
- lightCyan
- light_cyan
- lightDray
- light_gray
- gray
- darkGray
- dark_gray
- lightYellow
- light_yellow
- lightMagenta
- light_magenta
// extra
- lightRedEx
- light_red_ex
- lightGreenEx
- light_green_ex
- lightBlueEx
- light_blue_ex
- lightCyanEx
- light_cyan_ex
- whiteEx
- white_ex
// option
- bold
- underscore
- reverse
Ref
issue9/term
https://github.com/issue9/termbeego/bee
https://github.com/beego/beeinhere/console
https://github/inhere/php-console- ANSI escape code
License
MIT
Documentation
¶
Overview ¶
Example ¶
Output:
Index ¶
- Constants
- Variables
- func AddStyle(name string, s Style)
- func AddTheme(name string, style Style)
- func Apply(str string, colors ...Color) string
- func ApplyTag(tag string, args ...interface{}) string
- func Byte24Color(str string)
- func Byte8Color(str string, val Bt8Color)
- func ClearCode(str string) string
- func ClearTag(str string) string
- func Disable()
- func Fprint(w io.Writer, args ...interface{}) (int, error)
- func Fprintf(w io.Writer, format string, args ...interface{}) (int, error)
- func Fprintln(w io.Writer, args ...interface{}) (int, error)
- func GetColorTags() map[string]string
- func GetStyleCode(name string) string
- func IsBgColor(name string) bool
- func IsDefinedTag(name string) bool
- func IsDisabled() bool
- func IsFgColor(name string) bool
- func IsOption(name string) bool
- func ParseCodeFromAttr(attr string) (code string)
- func Print(args ...interface{}) (int, error)
- func Printf(format string, args ...interface{}) (int, error)
- func Println(args ...interface{}) (int, error)
- func Render(args ...interface{}) string
- func RenderCodes(code string, str string) string
- func RenderStr(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, args ...interface{}) string
- func String(str string) string
- func TrueColor(str string, rgb RgbColor)
- func WrapTag(str string, tag string) string
- type Bt8Color
- type Bt8Style
- type Color
- func (c Color) IsValid() bool
- func (c Color) Print(args ...interface{}) (int, error)
- func (c Color) Printf(format string, args ...interface{}) (int, error)
- func (c Color) Println(args ...interface{}) (int, error)
- func (c Color) Render(args ...interface{}) string
- func (c Color) Renderf(format string, args ...interface{}) string
- func (c Color) String() string
- type LiteTips
- type RgbColor
- type RgbStyle
- type Style
- func (s Style) IsEmpty() bool
- func (s Style) Print(args ...interface{}) (int, error)
- func (s Style) Printf(format string, args ...interface{}) (int, error)
- func (s Style) Println(args ...interface{}) (int, error)
- func (s Style) Render(args ...interface{}) string
- func (s Style) Save(name string)
- func (s Style) Sprint(args ...interface{}) string
- type Tag
- type Theme
- type Tips
Examples ¶
Constants ¶
const ( // Bold use bold for message Bold = OpBold // Red color for message Red = FgRed // Cyan color for message Cyan = FgCyan // Gray color for message Gray = FgDarkGray // Blue color for message Blue = FgBlue // Black color for message Black = FgBlack // Green color for message Green = FgGreen // White color for message White = FgWhite // Normal color for message Normal = FgDefault // Yellow color for message Yellow = FgYellow // Magenta color for message Magenta = FgMagenta )
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`
Regex to clear color codes eg "\033[1;36mText\x1b[0m"
const FullColorTpl = "\x1b[%sm%s\x1b[0m"
const ResetCode = "\x1b[0m"
ESC 操作的表示 "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)
const SetRgbBg = "\x1b[48;2;%d;%d;%dm"
const SetRgbFg = "\x1b[38;2;%d;%d;%dm"
24 byte 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转义序列#24位
const SettingTpl = "\x1b[%sm"
CLI color template
const SingleColorTpl = "\x1b[%dm%s\x1b[0m"
const Tpl256Bg = "48;5;%d"
const Tpl256Fg = "38;5;%d"
8 byte 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转义序列#8位
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}} // 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, FgMagenta}} // 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.Println("new line") 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, }
Background colors map
var Enable = true
switch color display
var FgColors = map[string]Color{ "black": FgBlack, "red": FgRed, "green": FgGreen, "yellow": FgYellow, "blue": FgBlue, "magenta": FgMagenta, "cyan": FgCyan, "white": FgWhite, "default": FgDefault, }
Foreground colors map
var Options = map[string]Color{ "reset": OpReset, "bold": OpBold, "fuzzy": OpFuzzy, "italic": OpItalic, "underscore": OpUnderscore, "blink": OpBlink, "reverse": OpReverse, "concealed": OpConcealed, }
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 Apply ¶
Apply custom colors. usage:
// (string, fg-color,bg-color, options...) color.Apply("text", color.FgGreen) color.Apply("text", color.FgGreen, color.BgBlack, color.OpBold)
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 RenderCodes ¶
RenderCodes render by color code "3;32;45"
func ReplaceTag ¶
ReplaceTag parse string, replace tag and return rendered string
Types ¶
type Bt8Color ¶
type Bt8Color uint8
uint8 at 0 - 255 10进制和16进制都可 0x98 = 152 use 8 byte, 0 - 255 color
type Color ¶
type Color uint8
Color represents a text color.
const ( // basic Foreground colors 30 - 37 FgBlack Color = iota + 30 FgRed FgGreen FgYellow FgBlue FgMagenta // 品红 FgCyan // 青色 FgWhite // revert default FG FgDefault Color = 39 )
Foreground colors.
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 ( // basic Background colors 40 - 47 BgBlack Color = iota + 40 BgRed BgGreen BgYellow // BgBrown like yellow BgBlue BgMagenta BgCyan BgWhite // revert default BG BgDefault Color = 49 )
Background colors.
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 ¶
Print messages usage:
color.FgGreen.Print("message")
or:
green := color.FgGreen.Print green("message")
func (Color) Printf ¶
Printf format and print messages usage:
color.FgCyan.Printf("string %s", "arg0")
func (Color) Render ¶
Render messages by color setting usage:
green := color.FgGreen.Render fmt.Println(green("message"))
type LiteTips ¶
type LiteTips string
LiteTips will only add color for tag name value is a defined style name
type Style ¶
type Style []Color
Style a colored style can add: fg color, bg color, color options quick use:
color.Style(color.FgGreen).
type Tag ¶
type Tag string
Tag value is a defined style name
type Theme ¶ added in v1.1.1
Theme definition. extends from Style