// 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.
package ccolorimport ()// color render templates//// ESC 操作的表示://// "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)const (// StartSet charsStartSet = "\x1b["// ResetSet close all properties.ResetSet = "\x1b[0m"// SettingTpl string.SettingTpl = "\x1b[%sm"// FullColorTpl for build color codeFullColorTpl = "\x1b[%sm%s\x1b[0m"// CodeSuffix string for color code.CodeSuffix = "[0m")// CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m"constCodeExpr = `\033\[[\d;?]+m`var (// last error lastErr error// output the default io.Writer message print output io.Writer = os.Stdout// match color codes codeRegex = regexp.MustCompile(CodeExpr))// SetOutput set output writerfunc ( io.Writer) { output = }// LastErr infofunc () error {deferfunc() {lastErr = nil }()returnlastErr}//// ---------------- support detect from termenv ----------------//// Disable color of current terminal.//// Usage://// ccolor.Disable()// defer ccolor.RevertColorSupport()func () { termenv.DisableColor() }// Level value of current terminal.func () termenv.ColorLevel { returntermenv.TermColorLevel() }// IsSupportColor returns true if the terminal supports color.func () bool { returntermenv.IsSupportColor() }// IsSupport256Color returns true if the terminal supports 256 colors.func () bool { returntermenv.IsSupport256Color() }// IsSupportTrueColor returns true if the terminal supports true color.func () bool { returntermenv.IsSupportTrueColor() }//// ---------------- for testing ----------------//// ForceEnableColor setting value. TIP: use for unit testing.//// Usage://// ccolor.ForceEnableColor()// defer ccolor.RevertColorSupport()func () {termenv.ForceEnableColor()}// RevertColorSupport valuefunc () {termenv.RevertColorSupport()}//// ---------------- print with color tag style ----------------//// Print parse color tag and print messagesfunc ( ...any) { Fprint(output, ...) }// Printf format and print messagesfunc ( string, ...any) { Fprintf(output, , ...) }// Println messages with new linefunc ( ...any) { Fprintln(output, ...) }// Sprint parse color tags, return rendered stringfunc ( ...any) string {returnReplaceTag(fmt.Sprint(...))}// Sprintf format and return rendered stringfunc ( string, ...any) string {returnReplaceTag(fmt.Sprintf(, ...))}// Fprint auto parse color-tag, print rendered messages to the writerfunc ( io.Writer, ...any) { _, lastErr = fmt.Fprint(, ReplaceTag(fmt.Sprint(...)))}// Fprintf auto parse color-tag, print rendered messages to the writer.func ( io.Writer, string, ...any) { _, lastErr = fmt.Fprint(, ReplaceTag(fmt.Sprintf(, ...)))}// Fprintln auto parse color-tag, print rendered messages to the writerfunc ( io.Writer, ...any) { _, lastErr = fmt.Fprintln(, ReplaceTag(formatLikePrintln()))}// Lprint passes colored messages to a log.Logger for printing.func ( *log.Logger, ...any) { .Print(ReplaceTag(fmt.Sprint(...)))}
The pages are generated with Goldsv0.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.