Documentation ¶
Index ¶
- Constants
- Variables
- func AddStyle(name string, s Style)
- func Apply(str string, colors ...Color) string
- func ApplyTag(tag string, args ...interface{}) string
- func Black(args ...interface{})
- func Blue(args ...interface{})
- func Bold(args ...interface{})
- func Byte24Color(str string)
- func Byte8Color(str string, val Bt8Color)
- func ClearCode(str string) string
- func ClearTag(str string) string
- func Comment(args ...interface{})
- func Cyan(args ...interface{})
- func Danger(args ...interface{})
- func Disable()
- func Err(args ...interface{})
- func Error(args ...interface{})
- 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 GetStyleName(name string) string
- func Gray(args ...interface{})
- func Green(args ...interface{})
- func Info(args ...interface{})
- func IsBgColor(name string) bool
- func IsDefinedTag(name string) bool
- func IsDisabled() bool
- func IsFgColor(name string) bool
- func IsOption(name string) bool
- func Light(args ...interface{})
- func Magenta(args ...interface{})
- func Note(args ...interface{})
- func Notice(args ...interface{})
- func ParseCodeFromAttr(attr string) (code string)
- func Primary(args ...interface{})
- func Print(args ...interface{}) (int, error)
- func Printf(format string, args ...interface{}) (int, error)
- func Println(args ...interface{}) (int, error)
- func Question(args ...interface{})
- func Red(args ...interface{})
- 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 Secondary(args ...interface{})
- func Set(colors ...Color) (int, error)
- func Suc(args ...interface{})
- func Success(args ...interface{})
- func TrueColor(str string, rgb RgbColor)
- func Warn(args ...interface{})
- func Warning(args ...interface{})
- func White(args ...interface{})
- func WrapTag(str string, tag string) string
- func Yellow(args ...interface{})
- 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 Logger
- type RgbColor
- type RgbStyle
- type Style
- func (s Style) Apply(args ...interface{}) string
- 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)
- type Tag
- type Tips
Constants ¶
const ( // Regex to match color tags // golang 不支持反向引用. 即不支持使用 \1 引用第一个匹配 ([a-z=;]+) // TagExpr = `<([a-z=;]+)>(.*?)<\/\1>` // 所以调整一下 统一使用 `</>` 来结束标签,例如 "<info>some text</>" // 支持自定义颜色属性的tag "<fg=white;bg=blue;op=bold>content</>" // (?s:...) s - 让 "." 匹配换行 TagExpr = `<([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 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 BuiltinStyles = map[string]Style{ "info": {OpReset, FgGreen}, "note": {OpBold, FgLightCyan}, "light": {FgLightWhite}, "error": {FgLightWhite, BgRed}, "danger": {OpBold, FgRed}, "notice": {OpBold, FgCyan}, "success": {OpBold, FgGreen}, "comment": {OpReset, FgYellow}, "primary": {OpReset, FgBlue}, "warning": {OpBold, FgYellow}, "question": {OpReset, FgMagenta}, "secondary": {FgDarkGray}, }
Some defined styles, like bootstrap styles
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 LogLevel2tag = map[string]string{
"info": "info",
"warn": "warning",
"warning": "warning",
"debug": "cyan",
"notice": "notice",
"error": "error",
}
log level to cli color style
var Options = map[string]Color{ "reset": OpReset, "bold": OpBold, "fuzzy": OpFuzzy, "italic": OpItalic, "underscore": OpUnderscore, "blink": OpBlink, "reverse": OpReverse, "concealed": OpConcealed, }
color options map
Functions ¶
func Apply ¶
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 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 // extra Foreground color 90 - 97(非标准) FgDarkGray Color = iota + 90 // 亮黑(灰) FgLightRed FgLightGreen FgLightYellow FgLightBlue FgLightMagenta FgLightCyan FgLightWhite )
Foreground colors.
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 // extra Background color 100 - 107(非标准) BgDarkGray Color = iota + 100 BgLightRed BgLightGreen BgLightYellow BgLightBlue BgLightMagenta BgLightCyan BgLightWhite )
Background colors.
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
type LiteTips ¶
type LiteTips string
LiteTips will only add color for tag name value is a defined style name
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger console logger
type Style ¶
type Style []Color
Style a colored style can add: fg color, bg color, color options quick use:
color.Style(color.FgGreen).