Skip to content

Commit

Permalink
add new func SupportColor reporting color level
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Dec 10, 2020
1 parent e9c23c2 commit c44376d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
19 changes: 12 additions & 7 deletions color.go
Expand Up @@ -54,8 +54,8 @@ var (
// match color codes
codeRegex = regexp.MustCompile(CodeExpr)
// mark current env is support color.
// Always: isLikeInCmd != isSupportColor
isSupportColor = IsSupportColor()
// Always: isLikeInCmd != supportColor
supportColor = IsSupportColor()
)

/*************************************************************
Expand Down Expand Up @@ -119,15 +119,20 @@ func ResetOptions() {
output = os.Stdout
}

// SupportColor of the current ENV
func SupportColor() bool {
return supportColor
}

// ForceColor force open color render
func ForceColor() bool {
return ForceOpenColor()
}

// ForceOpenColor force open color render
func ForceOpenColor() bool {
oldVal := isSupportColor
isSupportColor = true
oldVal := supportColor
supportColor = true

return oldVal
}
Expand Down Expand Up @@ -161,7 +166,7 @@ func RenderCode(code string, args ...interface{}) string {
}

// disabled OR not support color
if !Enable || !isSupportColor {
if !Enable || !supportColor {
return ClearCode(message)
}

Expand All @@ -177,7 +182,7 @@ func RenderWithSpaces(code string, args ...interface{}) string {
}

// disabled OR not support color
if !Enable || !isSupportColor {
if !Enable || !supportColor {
return ClearCode(message)
}

Expand All @@ -193,7 +198,7 @@ func RenderString(code string, str string) string {
}

// disabled OR not support color
if !Enable || !isSupportColor {
if !Enable || !supportColor {
return ClearCode(str)
}

Expand Down
7 changes: 4 additions & 3 deletions color_test.go
Expand Up @@ -118,6 +118,7 @@ func TestRenderCode(t *testing.T) {
defer resetColorRender()

is := assert.New(t)
is.True(SupportColor())

str := RenderCode("36;1", "Hi,", "babe")
is.Equal("\x1b[36;1mHi,babe\x1b[0m", str)
Expand Down Expand Up @@ -679,8 +680,8 @@ var oldVal bool

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

// set output for test
buf := new(bytes.Buffer)
Expand All @@ -690,7 +691,7 @@ func forceOpenColorRender() *bytes.Buffer {
}

func resetColorRender() {
isSupportColor = oldVal
supportColor = oldVal
// reset
ResetOutput()
}
Expand Down
10 changes: 5 additions & 5 deletions color_windows.go
Expand Up @@ -26,7 +26,7 @@ var (

func init() {
// if at windows's ConEmu, Cmder, putty ... terminals
if isSupportColor {
if supportColor {
return
}

Expand Down Expand Up @@ -61,11 +61,11 @@ func init() {
if err != nil {
fmt.Println("Enable colors error:", err)
saveInternalError(err)
isSupportColor = false
supportColor = false
}

// NOTICE: update var `isSupportColor` to TRUE.
isSupportColor = true
// NOTICE: update var `supportColor` to TRUE.
supportColor = true

// fetch console screen buffer info
// err := getConsoleScreenBufferInfo(uintptr(syscall.Stdout), &defScreenInfo)
Expand Down Expand Up @@ -128,7 +128,7 @@ func EnableVirtualTerminalProcessing(stream syscall.Handle, enable bool) error {
// old := ForceOpenColor()
// fn()
// // revert color setting
// isSupportColor = old
// supportColor = old
//
// err = EnableVirtualTerminalProcessing(syscall.Stdout, false)
// if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion tag.go
Expand Up @@ -118,7 +118,7 @@ func ReplaceTag(str string) string {
}

// disabled OR not support color
if !Enable || !isSupportColor {
if !Enable || !supportColor {
return ClearTag(str)
}

Expand Down

0 comments on commit c44376d

Please sign in to comment.