Skip to content

Commit

Permalink
Merge pull request #12 from SHT/master
Browse files Browse the repository at this point in the history
Add Lprint function
  • Loading branch information
inhere committed Mar 3, 2020
2 parents 6a72a07 + 9af8026 commit 833e2ed
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
13 changes: 13 additions & 0 deletions tag.go
Expand Up @@ -3,6 +3,7 @@ package color
import (
"fmt"
"io"
"log"
"regexp"
"strings"
)
Expand Down Expand Up @@ -164,6 +165,18 @@ func Fprintln(w io.Writer, a ...interface{}) {
}
}

// Lprint passes colored messages to a log.Logger for printing.
// Notice: should be goroutine safe
func Lprint(l *log.Logger, a ...interface{}) {
if isLikeInCmd {
renderColorCodeOnCmd(func() {
l.Print(Render(a...))
})
} else {
l.Print(Render(a...))
}
}

// Render parse color tags, return rendered string.
// Usage:
// text := Render("<info>hello</> <cyan>world</>!")
Expand Down
12 changes: 9 additions & 3 deletions tag_test.go
Expand Up @@ -2,7 +2,7 @@ package color

import (
"testing"

"log"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -31,7 +31,7 @@ func TestReplaceTag(t *testing.T) {
is.NotContains(r, ">")

// sample 3
s = `abc <err>err-text</>
s = `abc <err>err-text</>
def <info>info text
</>`
r = ReplaceTag(s)
Expand Down Expand Up @@ -135,6 +135,12 @@ func TestPrint(t *testing.T) {
Fprintf(buf, "<red>%s</>", "MSG")
is.Equal("\x1b[0;31mMSG\x1b[0m", buf.String())
buf.Reset()

// Lprint
logger := log.New(buf, "", 0)
Lprint(logger, "<red>MSG</>\n")
is.Equal("\x1b[0;31mMSG\x1b[0m\n", buf.String())
buf.Reset()
}

func TestWrapTag(t *testing.T) {
Expand All @@ -157,7 +163,7 @@ func TestClearTag(t *testing.T) {
is.Equal("text", ClearTag("<err>text</>"))
is.Equal("abc error def info text", ClearTag("abc <err>error</> def <info>info text</>"))

str := `abc <err>err-text</>
str := `abc <err>err-text</>
def <info>info text
</>`
ret := ClearTag(str)
Expand Down

0 comments on commit 833e2ed

Please sign in to comment.