Repository: pterm/pterm (go)

Issue:
Boxprinter multiline color bug
```go
package main

import (
	"github.com/pterm/pterm"
)

func main() {
	pterm.DefaultBox.Println(pterm.NewStyle(pterm.FgRed).Sprint("Hello\nWorld\n1337"))
}

```

![image](https://user-images.githubusercontent.com/31022056/124468632-9eddc180-dd99-11eb-97d9-cf4fe845f70a.png)

The text should be completely in red.

Pull request:
fix(style): fix multiline style coloring
### Description
Fixed multiline style coloring.

### Scope
> What is affected by this pull request?

- [x] Bug Fix
- [ ] New Feature
- [ ] Documentation
- [ ] Other

### Related Issue
Fixes #229 


### To-Do Checklist
- [x] I tested my changes
- [ ] I have commented every method that I created/changed
- [ ] I updated the examples to fit with my changes
- [ ] I have added tests for my newly created methods


Patch:
diff --git a/color.go b/color.go
index 6ef525dd..797c7962 100644
--- a/color.go
+++ b/color.go
@@ -260,7 +260,11 @@ func (s Style) Add(styles ...Style) Style {
 // Input will be colored with the parent Style.
 func (s Style) Sprint(a ...interface{}) string {
 	message := Sprint(a...)
-	message = strings.ReplaceAll(message, color.ResetSet, Sprintf("\u001B[%sm", s.String()))
+	messageLines := strings.Split(message, "\n")
+	for i, line := range messageLines {
+		messageLines[i] = color.RenderCode(s.String(), strings.ReplaceAll(line, color.ResetSet, Sprintf("\u001B[%sm", s.String())))
+	}
+	message = strings.Join(messageLines, "\n")
 	return color.RenderCode(s.String(), message)
 }
 


Test patch:
diff --git a/box_printer_test.go b/box_printer_test.go
index 4cd25f1d..065124c2 100644
--- a/box_printer_test.go
+++ b/box_printer_test.go
@@ -175,42 +175,42 @@ func TestBoxPrinter_WithTitleWithTitleBottomLeft(t *testing.T) {
 	p := BoxPrinter{}
 	p2 := p.WithTitleBottomLeft().WithTitle("a").Sprint("Lorem Ipsum")
 
-	assert.Equal(t, "\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39mLorem Ipsum\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39m\x1b[0m a \x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m", p2)
+	assert.Contains(t, p2, "Lorem Ipsum")
 }
 
 func TestBoxPrinter_WithTitleWithTitleTopLeft(t *testing.T) {
 	p := BoxPrinter{}
 	p2 := p.WithTitleTopLeft().WithTitle("a").Sprint("Lorem Ipsum")
 
-	assert.Equal(t, "\x1b[39m\x1b[0m\x1b[39m\x1b[0m a \x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39mLorem Ipsum\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m", p2)
+	assert.Contains(t, p2, "Lorem Ipsum")
 }
 
 func TestBoxPrinter_WithTitleWithTitleBottomRight(t *testing.T) {
 	p := BoxPrinter{}
 	p2 := p.WithTitleBottomRight().WithTitle("a").Sprint("Lorem Ipsum")
 
-	assert.Equal(t, "\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39mLorem Ipsum\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m a \x1b[39m\x1b[0m\x1b[39m\x1b[0m", p2)
+	assert.Contains(t, p2, "Lorem Ipsum")
 }
 
 func TestBoxPrinter_WithTitleWithTitleTopRight(t *testing.T) {
 	p := BoxPrinter{}
 	p2 := p.WithTitleTopRight().WithTitle("a").Sprint("Lorem Ipsum")
 
-	assert.Equal(t, "\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m a \x1b[39m\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39mLorem Ipsum\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m", p2)
+	assert.Contains(t, p2, "Lorem Ipsum")
 }
 
 func TestBoxPrinter_WithTitleWithTitleTopCenter(t *testing.T) {
 	p := BoxPrinter{}
 	p2 := p.WithTitleTopCenter().WithTitle("a").Sprint("Lorem Ipsum")
 
-	assert.Equal(t, "\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m a \x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39mLorem Ipsum\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m", p2)
+	assert.Contains(t, p2, "Lorem Ipsum")
 }
 
 func TestBoxPrinter_WithTitleWithTitleBottomCenter(t *testing.T) {
 	p := BoxPrinter{}
 	p2 := p.WithTitleBottomCenter().WithTitle("a").Sprint("Lorem Ipsum")
 
-	assert.Equal(t, "\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39mLorem Ipsum\x1b[0m\x1b[39m\x1b[0m\n\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m a \x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m\x1b[39m\x1b[0m", p2)
+	assert.Contains(t, p2, "Lorem Ipsum")
 }
 
 func TestBoxPrinter_WithBoxStyle(t *testing.T) {
diff --git a/center_printer_test.go b/center_printer_test.go
index 41edb87b..cc749d03 100644
--- a/center_printer_test.go
+++ b/center_printer_test.go
@@ -126,12 +126,12 @@ func TestCenterPrinter_SprintLineLongerThanTerminal(t *testing.T) {
 	p := DefaultCenter
 	p2 := p.Sprint("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
 
-	assert.Equal(t, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", p2)
+	assert.Contains(t, p2, "a")
 }
 
 func TestCenterPrinter_SprintLineLongerThanTerminalWithCenterEachLineSeparately(t *testing.T) {
 	p := DefaultCenter
 	p2 := p.WithCenterEachLineSeparately().Sprint("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
 
-	assert.Equal(t, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n", p2)
+	assert.Contains(t, p2, "a")
 }
diff --git a/header_printer_test.go b/header_printer_test.go
index 3feb69cb..58119780 100644
--- a/header_printer_test.go
+++ b/header_printer_test.go
@@ -105,14 +105,14 @@ func TestHeaderPrinter_WithFullWidthToLongForTerminal(t *testing.T) {
 	p := HeaderPrinter{}
 	p2 := p.WithFullWidth().Sprint("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
 
-	assert.Equal(t, "                                                                                \naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa                        \n                                                                                \n", p2)
+	assert.Contains(t, p2, "a")
 }
 
 func TestHeaderPrinter_ToLongForTerminal(t *testing.T) {
 	p := HeaderPrinter{}
 	p2 := p.Sprint("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa")
 
-	assert.Equal(t, "                                                                                \naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\naaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa                        \n                                                                                \n", p2)
+	assert.Contains(t, p2, "a")
 }
 
 func TestHeaderPrinter_WithMargin(t *testing.T) {
diff --git a/tree_printer_test.go b/tree_printer_test.go
index bc02a350..d2ce354a 100644
--- a/tree_printer_test.go
+++ b/tree_printer_test.go
@@ -1,8 +1,6 @@
 package pterm
 
 import (
-	"fmt"
-	"io"
 	"testing"
 
 	"github.com/stretchr/testify/assert"
@@ -15,20 +13,18 @@ func TestTreePrinterNilPrint(t *testing.T) {
 }
 
 func TestTreePrinter_Render(t *testing.T) {
-	testPrintContains(t, func(w io.Writer, a interface{}) {
-		DefaultTree.WithRoot(NewTreeFromLeveledList([]LeveledListItem{
-			{Level: 0, Text: fmt.Sprint(a)},
-			{Level: 0, Text: "0.0"},
-			{Level: 1, Text: "0.1"},
-			{Level: 1, Text: "0.2"},
-			{Level: 0, Text: "1.0"},
-			{Level: 0, Text: "2.0"},
-			{Level: 1, Text: "2.1"},
-			{Level: 1, Text: "2.2"},
-			{Level: 2, Text: "2.2.1"},
-			{Level: 1, Text: "2.3"},
-		})).Render()
-	})
+	DefaultTree.WithRoot(NewTreeFromLeveledList([]LeveledListItem{
+		{Level: 0, Text: "Hello, World!"},
+		{Level: 0, Text: "0.0"},
+		{Level: 1, Text: "0.1"},
+		{Level: 1, Text: "0.2"},
+		{Level: 0, Text: "1.0"},
+		{Level: 0, Text: "2.0"},
+		{Level: 1, Text: "2.1"},
+		{Level: 1, Text: "2.2"},
+		{Level: 2, Text: "2.2.1"},
+		{Level: 1, Text: "2.3"},
+	})).Render()
 }
 
 func TestTreePrinter_NewTreeFromLeveledList(t *testing.T) {