// 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 ccolor import ( ) // color render templates // // ESC 操作的表示: // // "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制) 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" ) // CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m" const CodeExpr = `\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 writer func ( io.Writer) { output = } // LastErr info func () error { defer func() { lastErr = nil }() return lastErr } // // ---------------- 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 { return termenv.TermColorLevel() } // IsSupportColor returns true if the terminal supports color. func () bool { return termenv.IsSupportColor() } // IsSupport256Color returns true if the terminal supports 256 colors. func () bool { return termenv.IsSupport256Color() } // IsSupportTrueColor returns true if the terminal supports true color. func () bool { return termenv.IsSupportTrueColor() } // // ---------------- for testing ---------------- // // ForceEnableColor setting value. TIP: use for unit testing. // // Usage: // // ccolor.ForceEnableColor() // defer ccolor.RevertColorSupport() func () { termenv.ForceEnableColor() } // RevertColorSupport value func () { termenv.RevertColorSupport() } // // ---------------- print with color tag style ---------------- // // Print parse color tag and print messages func ( ...any) { Fprint(output, ...) } // Printf format and print messages func ( string, ...any) { Fprintf(output, , ...) } // Println messages with new line func ( ...any) { Fprintln(output, ...) } // Sprint parse color tags, return rendered string func ( ...any) string { return ReplaceTag(fmt.Sprint(...)) } // Sprintf format and return rendered string func ( string, ...any) string { return ReplaceTag(fmt.Sprintf(, ...)) } // Fprint auto parse color-tag, print rendered messages to the writer func ( 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 writer func ( 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(...))) }