Skip to content

Commit

Permalink
Merge pull request #192 from fatih/new-line-fix
Browse files Browse the repository at this point in the history
color: add newline after wrapping text
  • Loading branch information
fatih committed Jul 11, 2023
2 parents 9e98307 + dffd300 commit 73fdd8c
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 10 deletions.
14 changes: 4 additions & 10 deletions color.go
Expand Up @@ -246,10 +246,7 @@ func (c *Color) Printf(format string, a ...interface{}) (n int, err error) {
// On Windows, users should wrap w with colorable.NewColorable() if w is of
// type *os.File.
func (c *Color) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
c.SetWriter(w)
defer c.UnsetWriter(w)

return fmt.Fprintln(w, a...)
return fmt.Fprintln(w, c.wrap(fmt.Sprint(a...)))
}

// Println formats using the default formats for its operands and writes to
Expand All @@ -258,10 +255,7 @@ func (c *Color) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
// encountered. This is the standard fmt.Print() method wrapped with the given
// color.
func (c *Color) Println(a ...interface{}) (n int, err error) {
c.Set()
defer c.unset()

return fmt.Fprintln(Output, a...)
return fmt.Fprintln(Output, c.wrap(fmt.Sprint(a...)))
}

// Sprint is just like Print, but returns a string instead of printing it.
Expand All @@ -271,7 +265,7 @@ func (c *Color) Sprint(a ...interface{}) string {

// 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...))
return fmt.Sprintln(c.Sprint(a...))
}

// Sprintf is just like Printf, but returns a string instead of printing it.
Expand Down Expand Up @@ -353,7 +347,7 @@ func (c *Color) SprintfFunc() func(format string, a ...interface{}) string {
// string. Windows users should use this in conjunction with color.Output.
func (c *Color) SprintlnFunc() func(a ...interface{}) string {
return func(a ...interface{}) string {
return c.wrap(fmt.Sprintln(a...))
return fmt.Sprintln(c.Sprint(a...))
}
}

Expand Down
51 changes: 51 additions & 0 deletions color_test.go
Expand Up @@ -3,6 +3,7 @@ package color
import (
"bytes"
"fmt"
"io"
"os"
"testing"

Expand Down Expand Up @@ -418,3 +419,53 @@ func TestNoFormatString(t *testing.T) {
}
}
}

func TestColor_Println_Newline(t *testing.T) {
rb := new(bytes.Buffer)
Output = rb

c := New(FgRed)
c.Println("foo")

got := readRaw(t, rb)
want := "\x1b[31mfoo\x1b[0m\n"

if want != got {
t.Errorf("Println newline error\n\nwant: %q\n got: %q", want, got)
}
}

func TestColor_Sprintln_Newline(t *testing.T) {
c := New(FgRed)

got := c.Sprintln("foo")
want := "\x1b[31mfoo\x1b[0m\n"

if want != got {
t.Errorf("Println newline error\n\nwant: %q\n got: %q", want, got)
}
}

func TestColor_Fprintln_Newline(t *testing.T) {
rb := new(bytes.Buffer)
c := New(FgRed)
c.Fprintln(rb, "foo")

got := readRaw(t, rb)
want := "\x1b[31mfoo\x1b[0m\n"

if want != got {
t.Errorf("Println newline error\n\nwant: %q\n got: %q", want, got)
}
}

func readRaw(t *testing.T, r io.Reader) string {
t.Helper()

out, err := io.ReadAll(r)
if err != nil {
t.Fatal(err)
}

return string(out)
}

0 comments on commit 73fdd8c

Please sign in to comment.