Skip to content

Commit

Permalink
Merge pull request #55 from erikh/sprintf
Browse files Browse the repository at this point in the history
Add direct Sprintf support
  • Loading branch information
fatih authored Feb 9, 2017
2 parents 42c364b + cd97404 commit 5ec5d9d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
15 changes: 15 additions & 0 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}) {
Expand Down
13 changes: 13 additions & 0 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 5ec5d9d

Please sign in to comment.