diff --git a/color.go b/color.go index dba8f21..34cd8e4 100644 --- a/color.go +++ b/color.go @@ -247,6 +247,21 @@ func (c *Color) Println(a ...interface{}) (n int, err error) { return fmt.Fprintln(Output, a...) } +// Sprint is just like Print, but returns a string instead of printing it. +func (c *Color) Sprint(a ...interface{}) string { + return c.wrap(fmt.Sprint(a...)) +} + +// Sprintln is just like Println, but returns a string instead of printing it. +func (c *Color) Sprintln(a ...interface{}) string { + return c.wrap(fmt.Sprintln(a...)) +} + +// Sprintf is just like Printf, but returns a string instead of printing it. +func (c *Color) Sprintf(format string, a ...interface{}) string { + return c.wrap(fmt.Sprintf(format, a...)) +} + // FprintFunc returns a new function that prints the passed arguments as // colorized with color.Fprint(). func (c *Color) FprintFunc() func(w io.Writer, a ...interface{}) { diff --git a/color_test.go b/color_test.go index f8edd63..946b5e3 100644 --- a/color_test.go +++ b/color_test.go @@ -54,6 +54,19 @@ func TestColor(t *testing.T) { t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine) } } + + for _, c := range testColors { + line := New(c.code).Sprintf("%s", c.text) + scannedLine := fmt.Sprintf("%q", line) + colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m", c.code, c.text) + escapedForm := fmt.Sprintf("%q", colored) + + fmt.Printf("%s\t: %s\n", c.text, line) + + if scannedLine != escapedForm { + t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine) + } + } } func TestColorEquals(t *testing.T) {