Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

color: add newline after wrapping text #192

Merged
merged 3 commits into from
Jul 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 4 additions & 10 deletions color.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
}