package ccolor
Import Path
github.com/gookit/goutil/x/ccolor (on go.dev)
Dependency Relation
imports 8 packages, and imported by one package
Involved Source Files
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.
color16.go
color_tag.go
quickuse.go
style.go
util.go
Package-Level Type Names (total 3)
Color Color16, 16 color value type
3(2^3=8) OR 4(2^4=16) bite color.
Code convert to code string. eg "35"
Darken current color. eg. 96(FgLightCyan) -> 36(FgCyan)
Usage:
cyan := LightCyan.Darken()
cyan.Print("message")
Fprint writes to w with color setting.
Usage:
ccolor.Green.Fprint(os.Stdout, "message")
Fprintf formats and writes to w with color setting.
Usage:
ccolor.Cyan.Fprintf(os.Stdout, "string %s", "arg0")
Fprintln writes to w with color setting and adds a new line.
Usage:
ccolor.Green.Fprintln(os.Stdout, "message")
IsBg check is background color
IsFg check is foreground color
IsOption check is option code: 0-9
IsValid color value
Light current color. eg: 36(FgCyan) -> 96(FgLightCyan).
Usage:
lightCyan := Cyan.Light()
lightCyan.Print("message")
Print messages.
Usage:
ccolor.Green.Print("message")
OR:
green := ccolor.FgGreen.Print
green("message")
Printf format and print messages.
Usage:
ccolor.Cyan.Printf("string %s", "arg0")
Println messages with new line
Render messages by color setting
Usage:
green := ccolor.FgGreen.Render
fmt.Println(green("message"))
Renderln messages by color setting.
like fmt.Println, will add spaces for each argument
Usage:
green := ccolor.FgGreen.Renderln
fmt.Println(green("message"))
Sprint render messages by color setting. is alias of the Render()
Sprintf format and render message.
Usage:
green := ccolor.Green.Sprintf
colored := green("message")
String convert to code string. eg "35"
Text render a text message
ToBg always convert bg
ToFg always convert fg
Color : github.com/go-sql-driver/mysql.Logger
Color : database/sql/driver.Validator
Color : expvar.Var
Color : fmt.Stringer
Color : go.uber.org/fx.Printer
Color : gorm.io/gorm/logger.Writer
func Color.Darken() Color
func Color.Light() Color
func Color.ToBg() Color
func Color.ToFg() Color
func ColorsToCode(colors ...Color) string
func NewStyle(fg Color, bg Color, opts ...Color) *Style
func NewStyle(fg Color, bg Color, opts ...Color) *Style
func NewStyle(fg Color, bg Color, opts ...Color) *Style
const BgBlack
const BgBlue
const BgCyan
const BgDarkGray
const BgDefault
const BgGray
const BgGreen
const BgHiBlue
const BgHiCyan
const BgHiGreen
const BgHiMagenta
const BgHiRed
const BgHiWhite
const BgHiYellow
const BgLightBlue
const BgLightCyan
const BgLightGreen
const BgLightMagenta
const BgLightRed
const BgLightWhite
const BgLightYellow
const BgMagenta
const BgRed
const BgWhite
const BgYellow
const Black
const Blue
const Bold
const Cyan
const FgBlack
const FgBlue
const FgCyan
const FgDarkGray
const FgDefault
const FgGray
const FgGreen
const FgLightBlue
const FgLightCyan
const FgLightGreen
const FgLightMagenta
const FgLightRed
const FgLightWhite
const FgLightYellow
const FgMagenta
const FgRed
const FgWhite
const FgYellow
const Gray
const Green
const HiBlue
const HiCyan
const HiGreen
const HiMagenta
const HiRed
const HiWhite
const HiYellow
const LightBlue
const LightCyan
const LightGreen
const LightMagenta
const LightRed
const LightWhite
const LightYellow
const Magenta
const Normal
const OpBlink
const OpBold
const OpConcealed
const OpFastBlink
const OpFuzzy
const OpItalic
const OpReset
const OpReverse
const OpStrikethrough
const OpUnderscore
const Red
const White
const Yellow
String color style string. TODO
eg:
s := String("red,bold")
s.Println("some text message")
Style for color render.
Bg Color
Fg Color
Opts []Color
Fprint like fmt.Fprint, but with color
Fprintf like fmt.Fprintf, but with color
Fprintln like fmt.Fprintln, but with color
Print like fmt.Print, but with color
Printf render and print text
Println like fmt.Println, but with color
Sprint like fmt.Sprint, but with color
Sprintf format and render message.
Sprintln like fmt.Sprintln, but with color
String convert style setting to color code string.
*Style : github.com/go-sql-driver/mysql.Logger
*Style : expvar.Var
*Style : fmt.Stringer
*Style : go.uber.org/fx.Printer
*Style : gorm.io/gorm/logger.Writer
func NewStyle(fg Color, bg Color, opts ...Color) *Style
var Debug *Style
var Error *Style
var Info *Style
var Success *Style
var Warn *Style
Package-Level Functions (total 69)
ApplyTag for messages
Bg2Fg bg color value to fg value
Bluef print message with Blue color
Blueln print message line with Blue color
Bluep print message with Blue color
ClearCode clear color codes.
eg:
"\033[36;1mText\x1b[0m" -> "Text"
ClearTag clear-all tag for a string
ColorsToCode convert colors to code. return like "32;45;3"
ColorTags get all internal color tags
Cyanf print message with Cyan color
Cyanln print message line with Cyan color
Cyanp print message with Cyan color
Disable color of current terminal.
Usage:
ccolor.Disable()
defer ccolor.RevertColorSupport()
Errorf print message with error style
Errorln print message with error style
Errorp print message with error color
Fg2Bg fg color value to bg value
ForceEnableColor setting value. TIP: use for unit testing.
Usage:
ccolor.ForceEnableColor()
defer ccolor.RevertColorSupport()
Fprint auto parse color-tag, print rendered messages to the writer
Fprintf auto parse color-tag, print rendered messages to the writer.
Fprintln auto parse color-tag, print rendered messages to the writer
GetTagCode get color code by tag name
Grayf print message with gray color
Grayln print message line with gray color
Grayp print message with gray color
Greenf print message with green color
Greenln print message line with green color
Greenp print message with green color
Infof print message with info style
Infoln print message with info style
Infop print message with info color
IsDefinedTag is defined tag name
IsSupport256Color returns true if the terminal supports 256 colors.
IsSupportColor returns true if the terminal supports color.
IsSupportTrueColor returns true if the terminal supports true color.
LastErr info
Level value of current terminal.
Lprint passes colored messages to a log.Logger for printing.
Magentaf print message with magenta color
Magentaln print message line with magenta color
Magentap print message with magenta color
NewStyle fg, bg and options
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
ParseTagByEnv parse given string. will check package setting.
Print parse color tag and print messages
Printf format and print messages
Println messages with new line
Redf print message with Red color
Redln print message line with Red color
Redp print message with Red color
Render parse color tags, return rendered string.
Usage:
text := Render("<info>hello</> <cyan>world</>!")
fmt.Println(text)
RenderCode render message by color code.
Usage:
msg := RenderCode("3;32;45", "some", "message")
RenderString render a string with color code.
Usage:
msg := RenderString("3;32;45", "a message")
RenderWithSpaces Render code with spaces.
If the number of args is > 1, a space will be added between the args
ReplaceTag parse string, replace color tag and return rendered string
RevertColorSupport value
SetOutput set output writer
Sprint parse color tags, return rendered string
Sprintf format and return rendered string
Successf print message with success style
Successln print message with success style
Successp print message with success color
Warnf print message with warn style
Warnln print message with warn style
Warnp print message with warn color
WrapTag wrap a tag for a string "<tag>content</>"
Yellowf print message with yellow color
Yellowln print message line with yellow color
Yellowp print message with yellow color
Package-Level Variables (total 10)
AllOptions color options map
BgColors background colors map
Debug color style
Error color style
ExBgColors extra background colors map
ExFgColors extra foreground colors map
FgColors foreground colors map
Info color style
Success color style
Warn color style
Package-Level Constants (total 86)
Background colors. basic background colors 40 - 47
Background colors. basic background colors 40 - 47
Background colors. basic background colors 40 - 47
Extra background color 100 - 107 (non-standard)
BgDefault revert default BG
BgGray is alias of BgDarkGray
Background colors. basic background colors 40 - 47
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
Extra background color 100 - 107 (non-standard)
Extra background color 100 - 107 (non-standard)
Extra background color 100 - 107 (non-standard)
Extra background color 100 - 107 (non-standard)
Extra background color 100 - 107 (non-standard)
Extra background color 100 - 107 (non-standard)
Extra background color 100 - 107 (non-standard)
Background colors. basic background colors 40 - 47
Background colors. basic background colors 40 - 47
Background colors. basic background colors 40 - 47
Background colors. basic background colors 40 - 47
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m"
CodeSuffix string for color code.
There are basic and light foreground color aliases
Foreground colors. basic foreground colors 30 - 37
Foreground colors. basic foreground colors 30 - 37
Foreground colors. basic foreground colors 30 - 37
Extra foreground color 90 - 97(非标准)
FgDefault revert default FG
FgGray is alias of FgDarkGray
Foreground colors. basic foreground colors 30 - 37
Extra foreground color 90 - 97(非标准)
Extra foreground color 90 - 97(非标准)
Extra foreground color 90 - 97(非标准)
Extra foreground color 90 - 97(非标准)
Extra foreground color 90 - 97(非标准)
Extra foreground color 90 - 97(非标准)
Extra foreground color 90 - 97(非标准)
Foreground colors. basic foreground colors 30 - 37
Foreground colors. basic foreground colors 30 - 37
Foreground colors. basic foreground colors 30 - 37
Foreground colors. basic foreground colors 30 - 37
FullColorTpl for build color code
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
There are basic and light foreground color aliases
MatchExpr regex to match color tags
Notice: golang 不支持反向引用. 即不支持使用 \1 引用第一个匹配 ([a-z=;]+)
MatchExpr = `<([a-z=;]+)>(.*?)<\/\1>`
所以调整一下 统一使用 `</>` 来结束标签,例如 "<info>some text</>"
- NOTE: 不支持自定义属性。如有需要请使用 gookit/color 包
(?s:...) s - 让 "." 匹配换行
There are basic and light foreground color aliases
Option settings. range: 0 - 9
Option settings. range: 0 - 9
Option settings. range: 0 - 9
Option settings. range: 0 - 9
Option settings. range: 0 - 9
Option settings. range: 0 - 9
Option settings. range: 0 - 9
Option settings. range: 0 - 9
Option settings. range: 0 - 9
Option settings. range: 0 - 9
There are basic and light foreground color aliases
ResetSet close all properties.
SettingTpl string.
StartSet chars
StripExpr regex used for removing color tags
StripExpr = `<[\/]?[a-zA-Z=;]+>`
随着上面的做一些调整
There are basic and light foreground color aliases
There are basic and light foreground color aliases
![]() |
The pages are generated with Golds v0.8.4. (GOOS=linux GOARCH=amd64) Golds is a Go 101 project developed by Tapir Liu. PR and bug reports are welcome and can be submitted to the issue list. Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds. |