Pārlūkot izejas kodu

Added API to enable/disable the package.

Signed-off-by: Vishal Rana <vr@labstack.com>
Vishal Rana 10 gadi atpakaļ
vecāks
revīzija
0ca40531ef
5 mainītis faili ar 59 papildinājumiem un 30 dzēšanām
  1. 7 0
      color/README.md
  2. 43 28
      color/color.go
  3. 8 0
      color/color_test.go
  4. 1 1
      gytes/gytes.go
  5. 0 1
      gytes/gytes_test.go

+ 7 - 0
color/README.md

@@ -71,6 +71,13 @@ fmt.Println(color.Blue("bold underline dim blue", color.B, color.U, color.D))
 ```
 ```
 ![Mix and match](http://i.imgur.com/jWGq9Ca.png)
 ![Mix and match](http://i.imgur.com/jWGq9Ca.png)
 
 
+### Enable/Disable the package
+
+```go
+color.Disable()
+color.Enable()
+```
+
 ### New instance
 ### New instance
 
 
 ```go
 ```go

+ 43 - 28
color/color.go

@@ -6,7 +6,7 @@ import (
 )
 )
 
 
 type (
 type (
-	inner func(interface{}, []string) string
+	inner func(interface{}, []string, *Color) string
 )
 )
 
 
 // Color styles
 // Color styles
@@ -98,7 +98,12 @@ var (
 )
 )
 
 
 func outer(n string) inner {
 func outer(n string) inner {
-	return func(msg interface{}, styles []string) string {
+	return func(msg interface{}, styles []string, c *Color) string {
+		// TODO: May be drop fmt to boost performance
+		if c.disabled {
+			return fmt.Sprintf("%s", msg)
+		}
+
 		b := new(bytes.Buffer)
 		b := new(bytes.Buffer)
 		b.WriteString("\x1b[")
 		b.WriteString("\x1b[")
 		b.WriteString(n)
 		b.WriteString(n)
@@ -107,13 +112,13 @@ func outer(n string) inner {
 			b.WriteString(s)
 			b.WriteString(s)
 		}
 		}
 		b.WriteString("m")
 		b.WriteString("m")
-		// TODO: Replace fmt for performance
 		return fmt.Sprintf("%s%v\x1b[0m", b.String(), msg)
 		return fmt.Sprintf("%s%v\x1b[0m", b.String(), msg)
 	}
 	}
 }
 }
 
 
 type (
 type (
 	Color struct {
 	Color struct {
+		disabled bool
 	}
 	}
 )
 )
 
 
@@ -122,104 +127,114 @@ func New() *Color {
 	return &Color{}
 	return &Color{}
 }
 }
 
 
+// Disable disables the package.
+func (c *Color) Disable() {
+	c.disabled = true
+}
+
+// Enable enables the package.
+func (c *Color) Enable() {
+	c.disabled = false
+}
+
 func (c *Color) Black(msg interface{}, styles ...string) string {
 func (c *Color) Black(msg interface{}, styles ...string) string {
-	return black(msg, styles)
+	return black(msg, styles, c)
 }
 }
 
 
 func (c *Color) Red(msg interface{}, styles ...string) string {
 func (c *Color) Red(msg interface{}, styles ...string) string {
-	return red(msg, styles)
+	return red(msg, styles, c)
 }
 }
 
 
 func (c *Color) Green(msg interface{}, styles ...string) string {
 func (c *Color) Green(msg interface{}, styles ...string) string {
-	return green(msg, styles)
+	return green(msg, styles, c)
 }
 }
 
 
 func (c *Color) Yellow(msg interface{}, styles ...string) string {
 func (c *Color) Yellow(msg interface{}, styles ...string) string {
-	return yellow(msg, styles)
+	return yellow(msg, styles, c)
 }
 }
 
 
 func (c *Color) Blue(msg interface{}, styles ...string) string {
 func (c *Color) Blue(msg interface{}, styles ...string) string {
-	return blue(msg, styles)
+	return blue(msg, styles, c)
 }
 }
 
 
 func (c *Color) Magenta(msg interface{}, styles ...string) string {
 func (c *Color) Magenta(msg interface{}, styles ...string) string {
-	return magenta(msg, styles)
+	return magenta(msg, styles, c)
 }
 }
 
 
 func (c *Color) Cyan(msg interface{}, styles ...string) string {
 func (c *Color) Cyan(msg interface{}, styles ...string) string {
-	return cyan(msg, styles)
+	return cyan(msg, styles, c)
 }
 }
 
 
 func (c *Color) White(msg interface{}, styles ...string) string {
 func (c *Color) White(msg interface{}, styles ...string) string {
-	return white(msg, styles)
+	return white(msg, styles, c)
 }
 }
 
 
 func (c *Color) Grey(msg interface{}, styles ...string) string {
 func (c *Color) Grey(msg interface{}, styles ...string) string {
-	return grey(msg, styles)
+	return grey(msg, styles, c)
 }
 }
 
 
 func (c *Color) BlackBg(msg interface{}, styles ...string) string {
 func (c *Color) BlackBg(msg interface{}, styles ...string) string {
-	return blackBg(msg, styles)
+	return blackBg(msg, styles, c)
 }
 }
 
 
 func (c *Color) RedBg(msg interface{}, styles ...string) string {
 func (c *Color) RedBg(msg interface{}, styles ...string) string {
-	return redBg(msg, styles)
+	return redBg(msg, styles, c)
 }
 }
 
 
 func (c *Color) GreenBg(msg interface{}, styles ...string) string {
 func (c *Color) GreenBg(msg interface{}, styles ...string) string {
-	return greenBg(msg, styles)
+	return greenBg(msg, styles, c)
 }
 }
 
 
 func (c *Color) YellowBg(msg interface{}, styles ...string) string {
 func (c *Color) YellowBg(msg interface{}, styles ...string) string {
-	return yellowBg(msg, styles)
+	return yellowBg(msg, styles, c)
 }
 }
 
 
 func (c *Color) BlueBg(msg interface{}, styles ...string) string {
 func (c *Color) BlueBg(msg interface{}, styles ...string) string {
-	return blueBg(msg, styles)
+	return blueBg(msg, styles, c)
 }
 }
 
 
 func (c *Color) MagentaBg(msg interface{}, styles ...string) string {
 func (c *Color) MagentaBg(msg interface{}, styles ...string) string {
-	return magentaBg(msg, styles)
+	return magentaBg(msg, styles, c)
 }
 }
 
 
 func (c *Color) CyanBg(msg interface{}, styles ...string) string {
 func (c *Color) CyanBg(msg interface{}, styles ...string) string {
-	return cyanBg(msg, styles)
+	return cyanBg(msg, styles, c)
 }
 }
 
 
 func (c *Color) WhiteBg(msg interface{}, styles ...string) string {
 func (c *Color) WhiteBg(msg interface{}, styles ...string) string {
-	return whiteBg(msg, styles)
+	return whiteBg(msg, styles, c)
 }
 }
 
 
 func (c *Color) Reset(msg interface{}, styles ...string) string {
 func (c *Color) Reset(msg interface{}, styles ...string) string {
-	return reset(msg, styles)
+	return reset(msg, styles, c)
 }
 }
 
 
 func (c *Color) Bold(msg interface{}, styles ...string) string {
 func (c *Color) Bold(msg interface{}, styles ...string) string {
-	return bolt(msg, styles)
+	return bolt(msg, styles, c)
 }
 }
 
 
 func (c *Color) Dim(msg interface{}, styles ...string) string {
 func (c *Color) Dim(msg interface{}, styles ...string) string {
-	return dim(msg, styles)
+	return dim(msg, styles, c)
 }
 }
 
 
 func (c *Color) Italic(msg interface{}, styles ...string) string {
 func (c *Color) Italic(msg interface{}, styles ...string) string {
-	return italic(msg, styles)
+	return italic(msg, styles, c)
 }
 }
 
 
 func (c *Color) Underline(msg interface{}, styles ...string) string {
 func (c *Color) Underline(msg interface{}, styles ...string) string {
-	return underline(msg, styles)
+	return underline(msg, styles, c)
 }
 }
 
 
 func (c *Color) Inverse(msg interface{}, styles ...string) string {
 func (c *Color) Inverse(msg interface{}, styles ...string) string {
-	return inverse(msg, styles)
+	return inverse(msg, styles, c)
 }
 }
 
 
 func (c *Color) Hidden(msg interface{}, styles ...string) string {
 func (c *Color) Hidden(msg interface{}, styles ...string) string {
-	return hidden(msg, styles)
+	return hidden(msg, styles, c)
 }
 }
 
 
 func (c *Color) Strikeout(msg interface{}, styles ...string) string {
 func (c *Color) Strikeout(msg interface{}, styles ...string) string {
-	return strikeout(msg, styles)
+	return strikeout(msg, styles, c)
 }
 }
 
 
 func Black(msg interface{}, styles ...string) string {
 func Black(msg interface{}, styles ...string) string {

+ 8 - 0
color/color_test.go

@@ -2,6 +2,7 @@ package color
 
 
 import (
 import (
 	"fmt"
 	"fmt"
+	"github.com/stretchr/testify/assert"
 	"testing"
 	"testing"
 )
 )
 
 
@@ -50,3 +51,10 @@ func TestMixMatch(t *testing.T) {
 	fmt.Println(Cyan("inverse cyan", In))
 	fmt.Println(Cyan("inverse cyan", In))
 	fmt.Println(Blue("bold underline dim blue", B, U, D))
 	fmt.Println(Blue("bold underline dim blue", B, U, D))
 }
 }
+
+func TestEnableDisable(t *testing.T) {
+	global.Disable()
+	assert.Equal(t, "red", Red("red"))
+	global.Enable()
+	assert.NotEqual(t, "green", Green("green"))
+}

+ 1 - 1
gytes/gytes.go

@@ -7,7 +7,7 @@ import (
 )
 )
 
 
 var (
 var (
-	global       = New()
+	global = New()
 )
 )
 
 
 type (
 type (

+ 0 - 1
gytes/gytes_test.go

@@ -28,4 +28,3 @@ func TestGytes(t *testing.T) {
 		t.Errorf("expected `1.29 KiB`, got %s", b)
 		t.Errorf("expected `1.29 KiB`, got %s", b)
 	}
 	}
 }
 }
-