diff --git a/color.go b/color.go index edb2a5d..b215424 100644 --- a/color.go +++ b/color.go @@ -28,16 +28,18 @@ const ( ) // color render templates +// // ESC 操作的表示: // "\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制) const ( - SettingTpl = "\x1b[%sm" + StartSet = "\x1b[" + // ResetSet close all properties. + ResetSet = "\x1b[0m" + SettingTpl = "\x1b[%sm" + // FullColorTpl for build color code FullColorTpl = "\x1b[%sm%s\x1b[0m" ) -// ResetSet Close all properties. -const ResetSet = "\x1b[0m" - // CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m" const CodeExpr = `\033\[[\d;?]+m` @@ -144,7 +146,7 @@ func ResetOptions() { output = os.Stdout } -// ForceColor force open color render +// ForceSetColorLevel force open color render func ForceSetColorLevel(level terminfo.ColorLevel) terminfo.ColorLevel { oldLevelVal := colorLevel colorLevel = level @@ -178,6 +180,7 @@ func InnerErrs() []error { *************************************************************/ // RenderCode render message by color code. +// // Usage: // msg := RenderCode("3;32;45", "some", "message") func RenderCode(code string, args ...interface{}) string { @@ -196,7 +199,8 @@ func RenderCode(code string, args ...interface{}) string { return ClearCode(message) } - return fmt.Sprintf(FullColorTpl, code, message) + // return fmt.Sprintf(FullColorTpl, code, message) + return StartSet + code + "m" + message + ResetSet } // RenderWithSpaces Render code with spaces. @@ -212,10 +216,12 @@ func RenderWithSpaces(code string, args ...interface{}) string { return ClearCode(message) } - return fmt.Sprintf(FullColorTpl, code, message) + // return fmt.Sprintf(FullColorTpl, code, message) + return StartSet + code + "m" + message + ResetSet } // RenderString render a string with color code. +// // Usage: // msg := RenderString("3;32;45", "a message") func RenderString(code string, str string) string { @@ -228,10 +234,12 @@ func RenderString(code string, str string) string { return ClearCode(str) } - return fmt.Sprintf(FullColorTpl, code, str) + // return fmt.Sprintf(FullColorTpl, code, str) + return StartSet + code + "m" + str + ResetSet } // ClearCode clear color codes. +// // eg: "\033[36;1mText\x1b[0m" -> "Text" func ClearCode(str string) string { return codeRegex.ReplaceAllString(str, "") diff --git a/color_16.go b/color_16.go index f2a211a..cadf385 100644 --- a/color_16.go +++ b/color_16.go @@ -167,9 +167,7 @@ const ( ) // Bit4 an method for create Color -func Bit4(code uint8) Color { - return Color(code) -} +func Bit4(code uint8) Color { return Color(code) } /************************************************************* * Color render methods @@ -185,33 +183,27 @@ func (c Color) Name() string { } // Text render a text message -func (c Color) Text(message string) string { - return RenderString(c.String(), message) -} +func (c Color) Text(message string) string { return RenderString(c.String(), message) } // Render messages by color setting // Usage: // green := color.FgGreen.Render // fmt.Println(green("message")) -func (c Color) Render(a ...interface{}) string { - return RenderCode(c.String(), a...) -} +func (c Color) Render(a ...interface{}) string { return RenderCode(c.String(), a...) } // Renderln messages by color setting. // like Println, will add spaces for each argument +// // Usage: // green := color.FgGreen.Renderln // fmt.Println(green("message")) -func (c Color) Renderln(a ...interface{}) string { - return RenderWithSpaces(c.String(), a...) -} +func (c Color) Renderln(a ...interface{}) string { return RenderWithSpaces(c.String(), a...) } // Sprint render messages by color setting. is alias of the Render() -func (c Color) Sprint(a ...interface{}) string { - return RenderCode(c.String(), a...) -} +func (c Color) Sprint(a ...interface{}) string { return RenderCode(c.String(), a...) } // Sprintf format and render message. +// // Usage: // green := color.Green.Sprintf // colored := green("message") @@ -220,6 +212,7 @@ func (c Color) Sprintf(format string, args ...interface{}) string { } // Print messages. +// // Usage: // color.Green.Print("message") // OR: @@ -230,6 +223,7 @@ func (c Color) Print(args ...interface{}) { } // Printf format and print messages. +// // Usage: // color.Cyan.Printf("string %s", "arg0") func (c Color) Printf(format string, a ...interface{}) { @@ -237,9 +231,7 @@ func (c Color) Printf(format string, a ...interface{}) { } // Println messages with new line -func (c Color) Println(a ...interface{}) { - doPrintlnV2(c.String(), a) -} +func (c Color) Println(a ...interface{}) { doPrintlnV2(c.String(), a) } // Light current color. eg: 36(FgCyan) -> 96(FgLightCyan). // @@ -326,21 +318,13 @@ func (c Color) RGB() RGBColor { } // Code convert to code string. eg "35" -func (c Color) Code() string { - // return fmt.Sprintf("%d", c) - return strconv.Itoa(int(c)) -} +func (c Color) Code() string { return strconv.Itoa(int(c)) } // String convert to code string. eg "35" -func (c Color) String() string { - // return fmt.Sprintf("%d", c) - return strconv.Itoa(int(c)) -} +func (c Color) String() string { return strconv.Itoa(int(c)) } // IsValid color value -func (c Color) IsValid() bool { - return c < 107 -} +func (c Color) IsValid() bool { return c < 107 } /************************************************************* * basic color maps