Skip to content

Commit

Permalink
Revert "Remove k0kubun/colorstring from dependencies"
Browse files Browse the repository at this point in the history
This reverts commit 2c60775.
  • Loading branch information
k0kubun committed Jan 6, 2022
1 parent b6cad77 commit 59385c8
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 43 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/k0kubun/pp/v3
go 1.17

require (
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88
github.com/mattn/go-colorable v0.1.12
golang.org/x/text v0.3.7
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88 h1:uC1QfSlInpQF+M0ao65imhwqKnz3Q2z/d8PWZRMQvDM=
github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k=
github.com/mattn/go-colorable v0.1.12 h1:jF+Du6AlPIjs2BiUiQlKOX0rt3SujHxPnksPKZbaA40=
github.com/mattn/go-colorable v0.1.12/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-isatty v0.0.14 h1:yVuAays6BHfxijgZPzw+3Zlu5yQgKGP2/hcQbHb7S9Y=
Expand Down
48 changes: 5 additions & 43 deletions printer_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pp

import (
"bytes"
"fmt"
"math/big"
"reflect"
Expand All @@ -10,6 +9,10 @@ import (
"testing"
"time"
"unsafe"

// Use fork until following PR is merged
// https://github.com/mitchellh/colorstring/pull/3
"github.com/k0kubun/colorstring"
)

type testCase struct {
Expand Down Expand Up @@ -247,7 +250,7 @@ func processTestCases(t *testing.T, printer *PrettyPrinter, cases []testCase) {
trimmed := strings.Replace(test.expect, "\t", "", -1)
trimmed = strings.TrimPrefix(trimmed, "\n")
trimmed = strings.TrimSuffix(trimmed, "\n")
expect := colorString(trimmed)
expect := colorstring.Color(trimmed)
if expect != actual {
v := reflect.ValueOf(test.object)
t.Errorf("\nTestCase: %#v\nType: %s\nExpect: %# v\nActual: %# v\n", test.object, v.Kind(), expect, actual)
Expand All @@ -273,44 +276,3 @@ func logResult(t *testing.T, object interface{}, actual string) {
func isMultiLine(text string) bool {
return strings.Contains(text, "\n")
}

func colorString(text string) string {
buf := new(bytes.Buffer)
colored := false

lastMatch := []int{0, 0}
for _, match := range colorRe.FindAllStringIndex(text, -1) {
buf.WriteString(text[lastMatch[1]:match[0]])
lastMatch = match

var colorText string
color := text[lastMatch[0]+1 : lastMatch[1]-1]
if code, ok := colors[color]; ok {
colored = (color != "reset")
colorText = fmt.Sprintf("\033[%sm", code)
} else {
colorText = text[lastMatch[0]:lastMatch[1]]
}
buf.WriteString(colorText)
}
buf.WriteString(text[lastMatch[1]:])

if colored {
buf.WriteString("\033[0m")
}
return buf.String()
}

var (
colorRe = regexp.MustCompile(`(?i)\[[a-z0-9_-]+\]`)
colors = map[string]string{
"red": "31",
"green": "32",
"yellow": "33",
"blue": "34",
"magenta": "35",
"cyan": "36",
"bold": "1",
"reset": "0",
}
)

0 comments on commit 59385c8

Please sign in to comment.