Documentation
¶
Overview ¶
Package ccolor is a simple color render library for terminal. Its main code is the code that is extracted and simplified from gookit/color,
TIP:
If you want to render with richer colors, use the https://github.com/gookit/color package.
Index ¶
- Constants
- Variables
- func ApplyTag(tag string, a ...any) string
- func Bg2Fg(val uint8) uint8
- func Bluef(format string, a ...any)
- func Blueln(a ...any)
- func Bluep(a ...any)
- func ClearCode(str string) string
- func ClearTag(s string) string
- func ColorTags() map[string]string
- func ColorsToCode(colors ...Color) string
- func Cyanf(format string, a ...any)
- func Cyanln(a ...any)
- func Cyanp(a ...any)
- func Disable()
- func Errorf(format string, a ...any)
- func Errorln(a ...any)
- func Errorp(a ...any)
- func Fg2Bg(val uint8) uint8
- func ForceEnableColor()
- func Fprint(w io.Writer, v ...any)
- func Fprintf(w io.Writer, format string, v ...any)
- func Fprintln(w io.Writer, v ...any)
- func GetTagCode(name string) string
- func Grayf(format string, a ...any)
- func Grayln(a ...any)
- func Grayp(a ...any)
- func Greenf(format string, a ...any)
- func Greenln(a ...any)
- func Greenp(a ...any)
- func Infof(format string, a ...any)
- func Infoln(a ...any)
- func Infop(a ...any)
- func IsDefinedTag(name string) bool
- func IsSupport256Color() bool
- func IsSupportColor() bool
- func IsSupportTrueColor() bool
- func LastErr() error
- func Level() termenv.ColorLevel
- func Lprint(l *log.Logger, v ...any)
- func Magentaf(format string, a ...any)
- func Magentaln(a ...any)
- func Magentap(a ...any)
- func ParseTag(str string) string
- func ParseTagByEnv(str string) string
- func Print(v ...any)
- func Printf(format string, v ...any)
- func Println(v ...any)
- func Redf(format string, a ...any)
- func Redln(a ...any)
- func Redp(a ...any)
- func Render(a ...any) string
- func RenderCode(code string, args ...any) string
- func RenderString(code string, str string) string
- func RenderWithSpaces(code string, args ...any) string
- func ReplaceTag(str string) string
- func RevertColorSupport()
- func SetOutput(w io.Writer)
- func Sprint(v ...any) string
- func Sprintf(format string, a ...any) string
- func Successf(format string, a ...any)
- func Successln(a ...any)
- func Successp(a ...any)
- func Warnf(format string, a ...any)
- func Warnln(a ...any)
- func Warnp(a ...any)
- func WrapTag(s string, tag string) string
- func Yellowf(format string, a ...any)
- func Yellowln(a ...any)
- func Yellowp(a ...any)
- type Color
- func (c Color) Code() string
- func (c Color) Darken() Color
- func (c Color) IsBg() bool
- func (c Color) IsFg() bool
- func (c Color) IsOption() bool
- func (c Color) IsValid() bool
- func (c Color) Light() Color
- func (c Color) Print(args ...any)
- func (c Color) Printf(format string, a ...any)
- func (c Color) Println(a ...any)
- func (c Color) Render(a ...any) string
- func (c Color) Renderln(a ...any) string
- func (c Color) Sprint(a ...any) string
- func (c Color) Sprintf(format string, args ...any) string
- func (c Color) String() string
- func (c Color) Text(message string) string
- func (c Color) ToBg() Color
- func (c Color) ToFg() Color
- type String
- type Style
- func (s *Style) Fprint(w io.Writer, v ...any)
- func (s *Style) Print(v ...any)
- func (s *Style) Printf(format string, v ...any)
- func (s *Style) Println(v ...any)
- func (s *Style) Sprint(v ...any) string
- func (s *Style) Sprintf(format string, v ...any) string
- func (s *Style) Sprintln(v ...any) string
- func (s *Style) String() string
Constants ¶
const ( // StartSet chars StartSet = "\x1b[" // ResetSet close all properties. ResetSet = "\x1b[0m" // SettingTpl string. SettingTpl = "\x1b[%sm" // FullColorTpl for build color code FullColorTpl = "\x1b[%sm%s\x1b[0m" // CodeSuffix string for color code. CodeSuffix = "[0m" )
color render templates
ESC 操作的表示:
"\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)
const ( Red = FgRed Cyan = FgCyan Gray = FgDarkGray // is light Black Blue = FgBlue Black = FgBlack Green = FgGreen White = FgWhite Yellow = FgYellow Magenta = FgMagenta Bold = OpBold Normal = FgDefault LightRed = FgLightRed LightCyan = FgLightCyan LightBlue = FgLightBlue LightGreen = FgLightGreen LightWhite = FgLightWhite LightYellow = FgLightYellow LightMagenta = FgLightMagenta HiRed = FgLightRed HiCyan = FgLightCyan HiBlue = FgLightBlue HiGreen = FgLightGreen HiWhite = FgLightWhite HiYellow = FgLightYellow HiMagenta = FgLightMagenta BgHiRed = BgLightRed BgHiCyan = BgLightCyan BgHiBlue = BgLightBlue BgHiGreen = BgLightGreen BgHiWhite = BgLightWhite BgHiYellow = BgLightYellow BgHiMagenta = BgLightMagenta )
There are basic and light foreground color aliases
const ( // MatchExpr regex to match color tags // // Notice: golang 不支持反向引用. 即不支持使用 \1 引用第一个匹配 ([a-z=;]+) // MatchExpr = `<([a-z=;]+)>(.*?)<\/\1>` // 所以调整一下 统一使用 `</>` 来结束标签,例如 "<info>some text</>" // // - NOTE: 不支持自定义属性。如有需要请使用 gookit/color 包 // // (?s:...) s - 让 "." 匹配换行 MatchExpr = `<([0-9a-zA-Z_]+)>(?s:(.*?))<\/>` // StripExpr regex used for removing color tags // StripExpr = `<[\/]?[a-zA-Z=;]+>` // 随着上面的做一些调整 StripExpr = `<[\/]?[0-9a-zA-Z_=,;]*>` )
output colored text like uses custom tag.
const CodeExpr = `\033\[[\d;?]+m`
CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m"
Variables ¶
var ( // Info color style Info = &Style{Fg: FgGreen} // Warn color style Warn = &Style{Fg: FgYellow} // Error color style Error = NewStyle(FgLightWhite, BgRed) // Debug color style Debug = &Style{Fg: FgCyan} // Success color style Success = &Style{Fg: FgGreen, Opts: []Color{OpBold}} )
var AllOptions = map[string]Color{ "reset": OpReset, "bold": OpBold, "fuzzy": OpFuzzy, "italic": OpItalic, "underscore": OpUnderscore, "blink": OpBlink, "reverse": OpReverse, "concealed": OpConcealed, }
AllOptions color options map
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 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
Functions ¶
func ColorsToCode ¶
ColorsToCode convert colors to code. return like "32;45;3"
func ForceEnableColor ¶
func ForceEnableColor()
ForceEnableColor setting value. TIP: use for unit testing.
Usage:
ccolor.ForceEnableColor() defer ccolor.RevertColorSupport()
func IsSupport256Color ¶
func IsSupport256Color() bool
IsSupport256Color returns true if the terminal supports 256 colors.
func IsSupportColor ¶
func IsSupportColor() bool
IsSupportColor returns true if the terminal supports color.
func IsSupportTrueColor ¶
func IsSupportTrueColor() bool
IsSupportTrueColor returns true if the terminal supports true color.
func ParseTag ¶
ParseTag parse given string, replace color tag and return rendered string
Use built in tags:
<TAG_NAME>CONTENT</> // e.g: `<info>message</>`
TIP: code is from gookit/color package
- Not support custom attributes
- Not support c256 or rgb color
func ParseTagByEnv ¶
ParseTagByEnv parse given string. will check package setting.
func Render ¶
Render parse color tags, return rendered string.
Usage:
text := Render("<info>hello</> <cyan>world</>!")
fmt.Println(text)
func RenderCode ¶
RenderCode render message by color code.
Usage:
msg := RenderCode("3;32;45", "some", "message")
func RenderString ¶
RenderString render a string with color code.
Usage:
msg := RenderString("3;32;45", "a message")
func RenderWithSpaces ¶
RenderWithSpaces Render code with spaces. If the number of args is > 1, a space will be added between the args
func ReplaceTag ¶
ReplaceTag parse string, replace color tag and return rendered string
Types ¶
type Color ¶
type Color uint8
Color Color16, 16 color value type 3(2^3=8) OR 4(2^4=16) bite color.
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 + 100 BgLightRed BgLightGreen BgLightYellow BgLightBlue BgLightMagenta BgLightCyan BgLightWhite // BgGray is alias of BgDarkGray BgGray Color = 100 )
Extra background color 100 - 107 (non-standard)
const ( OpReset Color = iota // 0 重置所有设置 OpBold // 1 加粗 OpFuzzy // 2 模糊(不是所有的终端仿真器都支持) OpItalic // 3 斜体(不是所有的终端仿真器都支持) OpUnderscore // 4 下划线 OpBlink // 5 闪烁 OpFastBlink // 6 快速闪烁(未广泛支持) OpReverse // 7 颠倒的 交换背景色与前景色 OpConcealed // 8 隐匿的 OpStrikethrough // 9 删除的,删除线(未广泛支持) )
Option settings. range: 0 - 9
func (Color) Darken ¶
Darken current color. eg. 96(FgLightCyan) -> 36(FgCyan)
Usage:
cyan := LightCyan.Darken()
cyan.Print("message")
func (Color) Light ¶
Light current color. eg: 36(FgCyan) -> 96(FgLightCyan).
Usage:
lightCyan := Cyan.Light()
lightCyan.Print("message")
func (Color) Print ¶
Print messages.
Usage:
ccolor.Green.Print("message")
OR:
green := ccolor.FgGreen.Print
green("message")
func (Color) Printf ¶
Printf format and print messages.
Usage:
ccolor.Cyan.Printf("string %s", "arg0")
func (Color) Render ¶
Render messages by color setting
Usage:
green := ccolor.FgGreen.Render
fmt.Println(green("message"))
func (Color) Renderln ¶
Renderln messages by color setting. like fmt.Println, will add spaces for each argument
Usage:
green := ccolor.FgGreen.Renderln
fmt.Println(green("message"))
type String ¶
type String string
String color style string. TODO eg:
s := String("red,bold")
s.Println("some text message")