Skip to content

Commit 5ec5d9d

Browse files
authored
Merge pull request #55 from erikh/sprintf
Add direct Sprintf support
2 parents 42c364b + cd97404 commit 5ec5d9d

2 files changed

Lines changed: 28 additions & 0 deletions

File tree

color.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,21 @@ func (c *Color) Println(a ...interface{}) (n int, err error) {
247247
return fmt.Fprintln(Output, a...)
248248
}
249249

250+
// Sprint is just like Print, but returns a string instead of printing it.
251+
func (c *Color) Sprint(a ...interface{}) string {
252+
return c.wrap(fmt.Sprint(a...))
253+
}
254+
255+
// Sprintln is just like Println, but returns a string instead of printing it.
256+
func (c *Color) Sprintln(a ...interface{}) string {
257+
return c.wrap(fmt.Sprintln(a...))
258+
}
259+
260+
// Sprintf is just like Printf, but returns a string instead of printing it.
261+
func (c *Color) Sprintf(format string, a ...interface{}) string {
262+
return c.wrap(fmt.Sprintf(format, a...))
263+
}
264+
250265
// FprintFunc returns a new function that prints the passed arguments as
251266
// colorized with color.Fprint().
252267
func (c *Color) FprintFunc() func(w io.Writer, a ...interface{}) {

color_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,19 @@ func TestColor(t *testing.T) {
5454
t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
5555
}
5656
}
57+
58+
for _, c := range testColors {
59+
line := New(c.code).Sprintf("%s", c.text)
60+
scannedLine := fmt.Sprintf("%q", line)
61+
colored := fmt.Sprintf("\x1b[%dm%s\x1b[0m", c.code, c.text)
62+
escapedForm := fmt.Sprintf("%q", colored)
63+
64+
fmt.Printf("%s\t: %s\n", c.text, line)
65+
66+
if scannedLine != escapedForm {
67+
t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine)
68+
}
69+
}
5770
}
5871

5972
func TestColorEquals(t *testing.T) {

0 commit comments

Comments
 (0)