Skip to content

Commit

Permalink
feat: support set the message print output
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Sep 1, 2019
1 parent 264eadd commit 9bd5686
Show file tree
Hide file tree
Showing 8 changed files with 161 additions and 127 deletions.
20 changes: 15 additions & 5 deletions color.go
Expand Up @@ -42,8 +42,8 @@ const CodeExpr = `\033\[[\d;?]+m`
var (
// Enable switch color display
Enable = true
// Output TODO the default io.Writer message print
Output io.Writer = os.Stdout
// output the default io.Writer message print
output io.Writer = os.Stdout
// mark current env, It's like in `cmd.exe`
// if not in windows, is's always is False.
isLikeInCmd bool
Expand Down Expand Up @@ -91,6 +91,16 @@ func Disable() {
Enable = false
}

// SetOutput set default colored text output
func SetOutput(w io.Writer) {
output = w
}

// ResetOutput reset output
func ResetOutput() {
output = os.Stdout
}

// ForceOpenColor force open color render
func ForceOpenColor() bool {
oldVal := isSupportColor
Expand Down Expand Up @@ -210,17 +220,17 @@ func (p *Printer) Sprintf(format string, a ...interface{}) string {

// Print rendering colored messages
func (p *Printer) Print(a ...interface{}) {
fmt.Print(RenderCode(p.String(), a...))
doPrintV2(p.String(), fmt.Sprint(a...))
}

// Printf format and rendering colored messages
func (p *Printer) Printf(format string, a ...interface{}) {
fmt.Print(RenderString(p.String(), fmt.Sprintf(format, a...)))
doPrintV2(p.String(), fmt.Sprintf(format, a...))
}

// Println rendering colored messages with newline
func (p *Printer) Println(a ...interface{}) {
fmt.Println(RenderString(p.ColorCode, formatArgsForPrintln(a)))
doPrintlnV2(p.ColorCode, a)
}

// IsEmpty color code
Expand Down
2 changes: 1 addition & 1 deletion color_nonwin.go
Expand Up @@ -20,4 +20,4 @@ func renderColorCodeOnCmd(_ func()) {}
// IsTerminal check currently is terminal
func IsTerminal(_ int) bool {
return true
}
}
46 changes: 27 additions & 19 deletions color_test.go
Expand Up @@ -206,7 +206,7 @@ func TestClearCode(t *testing.T) {
*************************************************************/

func TestPrinter(t *testing.T) {
forceOpenColorRender()
buf := forceOpenColorRender()
defer resetColorRender()
at := assert.New(t)

Expand All @@ -223,21 +223,21 @@ func TestPrinter(t *testing.T) {
at.Equal("48;5;132", p.String())

// Color256.Print
rewriteStdout()
p.Print("MSG")
str = restoreStdout()
str = buf.String()
buf.Reset()
at.Equal("\x1b[48;5;132mMSG\x1b[0m", str)

// Color256.Printf
rewriteStdout()
p.Printf("A %s", "MSG")
str = restoreStdout()
str = buf.String()
buf.Reset()
at.Equal("\x1b[48;5;132mA MSG\x1b[0m", str)

// Color256.Println
rewriteStdout()
p.Println("MSG")
str = restoreStdout()
str = buf.String()
buf.Reset()
at.Equal("\x1b[48;5;132mMSG\x1b[0m\n", str)
}

Expand All @@ -246,7 +246,7 @@ func TestPrinter(t *testing.T) {
*************************************************************/

func TestColor16(t *testing.T) {
forceOpenColorRender()
buf := forceOpenColorRender()
defer resetColorRender()
at := assert.New(t)

Expand All @@ -266,47 +266,47 @@ func TestColor16(t *testing.T) {
at.Equal("\x1b[31mA MSG\x1b[0m", str)

// Color.Print
rewriteStdout()
FgGray.Print("MSG")
str = restoreStdout()
str = buf.String()
if isLikeInCmd {
at.Equal("MSG", str)
} else {
at.Equal("\x1b[90mMSG\x1b[0m", str)
}
buf.Reset()

// Color.Printf
rewriteStdout()
BgGray.Printf("A %s", "MSG")
str = restoreStdout()
str = buf.String()
if isLikeInCmd {
at.Equal("A MSG", str)
} else {
at.Equal("\x1b[100mA MSG\x1b[0m", str)
}
buf.Reset()

// Color.Println
rewriteStdout()
LightMagenta.Println("MSG")
str = restoreStdout()
str = buf.String()
if isLikeInCmd {
at.Equal("MSG\n", str)
} else {
at.Equal("\x1b[95mMSG\x1b[0m\n", str)
}
buf.Reset()

rewriteStdout()
LightMagenta.Println()
str = restoreStdout()
str = buf.String()
at.Equal("\n", str)
buf.Reset()

if isLikeInCmd {
rewriteStdout()
LightCyan.Print("msg")
LightRed.Printf("m%s", "sg")
LightGreen.Println("msg")
str = restoreStdout()
str = buf.String()
at.Equal("msgmsgmsg\n", str)
buf.Reset()
}

// Color.Darken
Expand Down Expand Up @@ -644,13 +644,21 @@ func TestOther(t *testing.T) {
var oldVal bool

// force open color render for testing
func forceOpenColorRender() {
func forceOpenColorRender() *bytes.Buffer {
oldVal = isSupportColor
isSupportColor = true

// set output for test
buf := new(bytes.Buffer)
SetOutput(buf)

return buf
}

func resetColorRender() {
isSupportColor = oldVal
// reset
ResetOutput()
}

var oldStdout, newReader *os.File
Expand Down
9 changes: 4 additions & 5 deletions color_windows.go
Expand Up @@ -266,18 +266,17 @@ func winPrintln(str string, colors ...Color) {
func winInternalPrint(str string, attribute uint16, newline bool) (int, error) {
if !Enable { // not enable
if newline {
return fmt.Println(str)
return fmt.Fprintln(output, str)
}
return fmt.Print(str)
return fmt.Fprint(output, str)
}

// fmt.Print("attribute val: ", attribute, "\n")
_, _ = setConsoleTextAttr(uintptr(syscall.Stdout), attribute)

if newline {
fmt.Println(str)
_, _ = fmt.Fprintln(output, str)
} else {
fmt.Print(str)
_, _ = fmt.Fprint(output, str)
}

// handle, _, _ = procSetTextAttribute.Call(uintptr(syscall.Stdout), winDefSetting)
Expand Down
36 changes: 18 additions & 18 deletions style_test.go
Expand Up @@ -8,7 +8,7 @@ import (

func TestStyle(t *testing.T) {
// force open color render for testing
forceOpenColorRender()
buf := forceOpenColorRender()
defer resetColorRender()

is := assert.New(t)
Expand Down Expand Up @@ -41,54 +41,54 @@ func TestStyle(t *testing.T) {
is.Contains(str, FgDarkGray.String())

// Style.Print
rewriteStdout()
Info.Print("MSG")
str = restoreStdout()
str = buf.String()
if isLikeInCmd {
is.Equal("MSG", str)
} else {
is.Equal("\x1b[0;32mMSG\x1b[0m", str)
}
buf.Reset()

// Style.Printf
rewriteStdout()
Info.Printf("A %s", "MSG")
str = restoreStdout()
str = buf.String()
if isLikeInCmd {
is.Equal("A MSG", str)
} else {
is.Equal("\x1b[0;32mA MSG\x1b[0m", str)
}
buf.Reset()

// Style.Println
rewriteStdout()
Info.Println("MSG")
str = restoreStdout()
str = buf.String()
if isLikeInCmd {
is.Equal("MSG\n", str)
} else {
is.Equal("\x1b[0;32mMSG\x1b[0m\n", str)
}
buf.Reset()

rewriteStdout()
Info.Println("MSG", "OK")
str = restoreStdout()
str = buf.String()
if isLikeInCmd {
is.Equal("MSG OK\n", str)
} else {
is.Equal("\x1b[0;32mMSG OK\x1b[0m\n", str)
}
buf.Reset()

s = GetStyle("err")
is.False(s.IsEmpty())

if isLikeInCmd {
rewriteStdout()
s.Print("msg")
s.Printf("m%s", "sg")
s.Println("msg")
str = restoreStdout()
str = buf.String()
is.Equal("msgmsgmsg\n", str)
buf.Reset()
}

// add new
Expand All @@ -110,35 +110,35 @@ func TestStyle(t *testing.T) {

func TestThemes(t *testing.T) {
// force open color render for testing
forceOpenColorRender()
buf := forceOpenColorRender()
defer resetColorRender()

is := assert.New(t)

// Theme.Tips
rewriteStdout()
Info.Tips("MSG")
str := restoreStdout()
str := buf.String()
buf.Reset()
if isLikeInCmd {
is.Equal("INFO: MSG\n", str)
} else {
is.Equal("\x1b[0;32mINFO: \x1b[0mMSG\n", str)
}

// Theme.Prompt
rewriteStdout()
Info.Prompt("MSG")
str = restoreStdout()
str = buf.String()
buf.Reset()
if isLikeInCmd {
is.Equal("INFO: MSG\n", str)
} else {
is.Equal("\x1b[0;32mINFO: MSG\x1b[0m\n", str)
}

// Theme.Block
rewriteStdout()
Info.Block("MSG")
str = restoreStdout()
str = buf.String()
buf.Reset()
if isLikeInCmd {
is.Equal("INFO:\n MSG\n", str)
} else {
Expand Down

0 comments on commit 9bd5686

Please sign in to comment.