package ccolorimport ()// ColorsToCode convert colors to code. return like "32;45;3"func ( ...Color) string {iflen() == 0 {return"" }var []stringfor , := range { = append(, .String()) }returnstrings.Join(, ";")}func shouldCleanColor() bool {returntermenv.NoColor() || !termenv.IsSupportColor()}/************************************************************* * render color code *************************************************************/// RenderCode render message by color code.//// Usage://// msg := RenderCode("3;32;45", "some", "message")func ( string, ...any) string {varstringif := len(); == 0 {return"" } = fmt.Sprint(...)iflen() == 0 {return }// disabled OR not support colorifshouldCleanColor() {returnClearCode() }// return fmt.Sprintf(FullColorTpl, code, message)returnStartSet + + "m" + + ResetSet}// RenderString render a string with color code.//// Usage://// msg := RenderString("3;32;45", "a message")func ( string, string) string {iflen() == 0 || == "" {return }// disabled OR not support colorifshouldCleanColor() {returnClearCode() }// return fmt.Sprintf(FullColorTpl, code, str)returnStartSet + + "m" + + ResetSet}// RenderWithSpaces Render code with spaces.// If the number of args is > 1, a space will be added between the argsfunc ( string, ...any) string { := formatLikePrintln()iflen() == 0 {return }// disabled OR not support colorifshouldCleanColor() {returnClearCode() }returnStartSet + + "m" + + ResetSet}// ClearCode clear color codes.//// eg://// "\033[36;1mText\x1b[0m" -> "Text"func ( string) string {if !strings.Contains(, CodeSuffix) {return }returncodeRegex.ReplaceAllString(, "")}/************************************************************* * helper methods for print *************************************************************/// new implementation, support render full color code on pwsh.exe, cmd.exefunc doPrint(, string) { _, lastErr = fmt.Fprint(output, RenderString(, ))}func doPrintTo( io.Writer, , string) { _, lastErr = fmt.Fprint(, RenderString(, ))}// new implementation, support render full color code on pwsh.exe, cmd.exefunc doPrintln( string, []any) {doPrintlnTo(output, , )}// new implementation, support render full color code on pwsh.exe, cmd.exefunc doPrintlnTo( io.Writer, string, []any) { _, lastErr = fmt.Fprintln(, RenderString(, formatLikePrintln()))}// use Println, will add spaces for each argfunc formatLikePrintln( []any) ( string) {if := len(); == 0 { = "" } elseif == 1 { = fmt.Sprint([0]) } else { = fmt.Sprintln(...)// clear last "\n" = [:len()-1] }return}
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.