diff --git a/color_16.go b/color_16.go index a973cb0..e55f8e1 100644 --- a/color_16.go +++ b/color_16.go @@ -157,24 +157,21 @@ func (c Color) Sprintf(format string, args ...interface{}) string { // green := color.FgGreen.Print // green("message") func (c Color) Print(args ...interface{}) { - message := fmt.Sprint(args...) - if isLikeInCmd { - winPrint(message, c) - } else { - fmt.Print(RenderString(c.String(), message)) - } + str := fmt.Sprint(args...) + doPrint(str, c.Code(), c) } // Printf format and print messages. // Usage: // color.Cyan.Printf("string %s", "arg0") func (c Color) Printf(format string, a ...interface{}) { - msg := fmt.Sprintf(format, a...) - if isLikeInCmd { - winPrint(msg, c) - } else { - fmt.Print(RenderString(c.String(), msg)) - } + str := fmt.Sprintf(format, a...) + doPrint(str, c.Code(), c) +} + +// Println messages with new line +func (c Color) Println(a ...interface{}) { + doPrintln(formatArgsForPrintln(a), c.String(), c) } // Light current color. eg: 36(FgCyan) -> 96(FgLightCyan). @@ -205,7 +202,12 @@ func (c Color) Darken() Color { return c } -// String to code string. eg "35" +// Code convert to code string. eg "35" +func (c Color) Code() string { + return fmt.Sprintf("%d", c) +} + +// String convert to code string. eg "35" func (c Color) String() string { return fmt.Sprintf("%d", c) } @@ -298,12 +300,3 @@ func colors2code(colors ...Color) string { return strings.Join(codes, ";") } - -// Println messages with new line -func (c Color) Println(a ...interface{}) { - if isLikeInCmd { - winPrintln(formatArgsForPrintln(a), c) - } else { - fmt.Println(RenderString(c.String(), formatArgsForPrintln(a))) - } -} diff --git a/style.go b/style.go index 54127ef..f1e4921 100644 --- a/style.go +++ b/style.go @@ -59,30 +59,19 @@ func (s Style) Sprintf(format string, a ...interface{}) string { // Print render and Print text func (s Style) Print(a ...interface{}) { - if isLikeInCmd { - winPrint(fmt.Sprint(a...), s...) - } else { - fmt.Print(RenderCode(s.String(), a...)) - } + str := fmt.Sprint(a...) + doPrint(str, s.String(), s...) } // Printf render and print text func (s Style) Printf(format string, a ...interface{}) { - message := fmt.Sprintf(format, a...) - if isLikeInCmd { - winPrint(message, s...) - } else { - fmt.Print(RenderString(s.String(), message)) - } + str := fmt.Sprintf(format, a...) + doPrint(str, s.String(), s...) } // Println render and print text line func (s Style) Println(a ...interface{}) { - if isLikeInCmd { - winPrintln(formatArgsForPrintln(a), s...) - } else { - fmt.Println(RenderString(s.String(), formatArgsForPrintln(a))) - } + doPrintln(formatArgsForPrintln(a), s.String(), s...) } // Code convert to code string. returns like "32;45;3" diff --git a/style_test.go b/style_test.go index 42ad6b9..7032b0b 100644 --- a/style_test.go +++ b/style_test.go @@ -18,6 +18,7 @@ func TestStyle(t *testing.T) { is.True(s.IsEmpty()) is.Equal("", s.String()) + is.Equal("97;40", Light.Code()) is.Equal("97;40", Light.String()) str := Light.Render("msg") is.Contains(str, "97") diff --git a/utils.go b/utils.go index 2314785..09f5b6f 100644 --- a/utils.go +++ b/utils.go @@ -66,6 +66,22 @@ func IsSupport256Color() bool { // return runtime.GOOS == "windows" // } +func doPrint(str, code string, colors ...Color) { + if isLikeInCmd { + winPrint(str, colors...) + } else { + fmt.Print(RenderString(code, str)) + } +} + +func doPrintln(str, code string, colors ...Color) { + if isLikeInCmd { + winPrintln(str, colors...) + } else { + fmt.Println(RenderString(code, str)) + } +} + func stringToArr(str, sep string) (arr []string) { str = strings.TrimSpace(str) if str == "" {