Skip to content

Commit

Permalink
updat some color print logic
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 30, 2019
1 parent 4dab071 commit b4f0cc9
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 38 deletions.
37 changes: 15 additions & 22 deletions color_16.go
Expand Up @@ -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).
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)))
}
}
21 changes: 5 additions & 16 deletions style.go
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions style_test.go
Expand Up @@ -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")
Expand Down
16 changes: 16 additions & 0 deletions utils.go
Expand Up @@ -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 == "" {
Expand Down

0 comments on commit b4f0cc9

Please sign in to comment.