Skip to content

Commit a2983f4

Browse files
committed
color: add newline after wrapping text
The newline should be added after wrapping the input with escape sequences. That way `less` or other commands can parse the output correctly. fixes #157
1 parent 12126ed commit a2983f4

File tree

2 files changed

+55
-10
lines changed

2 files changed

+55
-10
lines changed

color.go

+4-10
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,7 @@ func (c *Color) Printf(format string, a ...interface{}) (n int, err error) {
246246
// On Windows, users should wrap w with colorable.NewColorable() if w is of
247247
// type *os.File.
248248
func (c *Color) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
249-
c.SetWriter(w)
250-
defer c.UnsetWriter(w)
251-
252-
return fmt.Fprintln(w, a...)
249+
return fmt.Fprintln(w, c.wrap(fmt.Sprint(a...)))
253250
}
254251

255252
// Println formats using the default formats for its operands and writes to
@@ -258,10 +255,7 @@ func (c *Color) Fprintln(w io.Writer, a ...interface{}) (n int, err error) {
258255
// encountered. This is the standard fmt.Print() method wrapped with the given
259256
// color.
260257
func (c *Color) Println(a ...interface{}) (n int, err error) {
261-
c.Set()
262-
defer c.unset()
263-
264-
return fmt.Fprintln(Output, a...)
258+
return fmt.Fprintln(Output, c.wrap(fmt.Sprint(a...)))
265259
}
266260

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

272266
// Sprintln is just like Println, but returns a string instead of printing it.
273267
func (c *Color) Sprintln(a ...interface{}) string {
274-
return c.wrap(fmt.Sprintln(a...))
268+
return fmt.Sprintln(c.Sprint(a...))
275269
}
276270

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

color_test.go

+51
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package color
33
import (
44
"bytes"
55
"fmt"
6+
"io"
67
"os"
78
"testing"
89

@@ -418,3 +419,53 @@ func TestNoFormatString(t *testing.T) {
418419
}
419420
}
420421
}
422+
423+
func TestColor_Println_Newline(t *testing.T) {
424+
rb := new(bytes.Buffer)
425+
Output = rb
426+
427+
c := New(FgRed)
428+
c.Println("foo")
429+
430+
got := readRaw(t, rb)
431+
want := "\x1b[31mfoo\x1b[0m\n"
432+
433+
if want != got {
434+
t.Errorf("Println newline error\n\nwant: %q\n got: %q", want, got)
435+
}
436+
}
437+
438+
func TestColor_Sprintln_Newline(t *testing.T) {
439+
c := New(FgRed)
440+
441+
got := c.Sprintln("foo")
442+
want := "\x1b[31mfoo\x1b[0m\n"
443+
444+
if want != got {
445+
t.Errorf("Println newline error\n\nwant: %q\n got: %q", want, got)
446+
}
447+
}
448+
449+
func TestColor_Fprintln_Newline(t *testing.T) {
450+
rb := new(bytes.Buffer)
451+
c := New(FgRed)
452+
c.Fprintln(rb, "foo")
453+
454+
got := readRaw(t, rb)
455+
want := "\x1b[31mfoo\x1b[0m\n"
456+
457+
if want != got {
458+
t.Errorf("Println newline error\n\nwant: %q\n got: %q", want, got)
459+
}
460+
}
461+
462+
func readRaw(t *testing.T, r io.Reader) string {
463+
t.Helper()
464+
465+
out, err := io.ReadAll(r)
466+
if err != nil {
467+
t.Fatal(err)
468+
}
469+
470+
return string(out)
471+
}

0 commit comments

Comments
 (0)