Преглед изворни кода

Color now has New function

Signed-off-by: Vishal Rana <vr@labstack.com>
Vishal Rana пре 10 година
родитељ
комит
a1a2f7c066
6 измењених фајлова са 231 додато и 75 уклоњено
  1. 9 2
      color/README.md
  2. 218 38
      color/color.go
  3. 2 2
      color/color_test.go
  4. 1 15
      gytes/README.md
  5. 1 2
      gytes/gytes.go
  6. 0 16
      gytes/gytes_test.go

+ 9 - 2
color/README.md

@@ -65,8 +65,15 @@ fmt.Println(color.Strikeout("strikeout"))
 ```go
 fmt.Println(color.Green("bold green with white background", color.B, color.WhtBg))
 fmt.Println(color.Red("underline red", color.U))
-fmt.Println(color.Yellow("dim yellow", color.D))
+fmt.Println(color.Yellow("dim yellow", color.Dm))
 fmt.Println(color.Cyan("inverse cyan", color.In))
-fmt.Println(color.Blue("bold underline dim blue", color.B, color.U, color.D))
+fmt.Println(color.Blue("bold underline dim blue", color.B, color.U, color.Dm))
 ```
 ![Mix and match](http://i.imgur.com/jWGq9Ca.png)
+
+### New instance
+
+```go
+c := New()
+c.Green("green")
+```

+ 218 - 38
color/color.go

@@ -6,7 +6,7 @@ import (
 )
 
 type (
-	inner func(interface{}, ...string) string
+	inner func(interface{}, []string) string
 )
 
 // Color styles
@@ -47,12 +47,12 @@ const (
 	// WhtBg white background style
 	WhtBg = "47"
 
-	// R reset emphasis style
-	R = "0"
+	// Rst reset emphasis style
+	Rst = "0"
 	// B bold emphasis style
 	B = "1"
-	// D dim emphasis style
-	D = "2"
+	// Dm dim emphasis style
+	Dm = "2"
 	// I italic emphasis style
 	I = "3"
 	// U underline emphasis style
@@ -67,49 +67,229 @@ const (
 
 // Color functions
 var (
-	// Text color
-	Black   = outer(Blk)
-	Red     = outer(Rd)
-	Green   = outer(Grn)
-	Yellow  = outer(Yel)
-	Blue    = outer(Blu)
-	Magenta = outer(Mgn)
-	Cyan    = outer(Cyn)
-	White   = outer(Wht)
-	Grey    = outer(Gry)
-
-	// Background color
-	BlackBg   = outer(BlkBg)
-	RedBg     = outer(RdBg)
-	GreenBg   = outer(GrnBg)
-	YellowBg  = outer(YelBg)
-	BlueBg    = outer(BluBg)
-	MagentaBg = outer(MgnBg)
-	CyanBg    = outer(CynBg)
-	WhiteBg   = outer(WhtBg)
-
-	// Emphasis
-	Reset     = outer(R)
-	Bold      = outer(B)
-	Dim       = outer(D)
-	Italic    = outer(I)
-	Underline = outer(U)
-	Inverse   = outer(In)
-	Hidden    = outer(Hd)
-	Strikeout = outer(So)
+	global = New()
 )
 
 func outer(n string) inner {
-	return func(m interface{}, style ...string) string {
+	return func(msg interface{}, styles []string) string {
 		b := new(bytes.Buffer)
 		b.WriteString("\x1b[")
 		b.WriteString(n)
-		for _, s := range style {
+		for _, s := range styles {
 			b.WriteString(";")
 			b.WriteString(s)
 		}
 		b.WriteString("m")
 		// TODO: Replace fmt for performance
-		return fmt.Sprintf("%s%v\x1b[0m", b.String(), m)
+		return fmt.Sprintf("%s%v\x1b[0m", b.String(), msg)
 	}
 }
+
+type (
+	Color struct {
+	}
+)
+
+func New() *Color {
+	return &Color{}
+}
+
+func (c *Color) Black(msg interface{}, styles ...string) string {
+	return outer(Blk)(msg, styles)
+}
+
+func (c *Color) Red(msg interface{}, styles ...string) string {
+	return outer(Rd)(msg, styles)
+}
+
+func (c *Color) Green(msg interface{}, styles ...string) string {
+	return outer(Grn)(msg, styles)
+}
+
+func (c *Color) Yellow(msg interface{}, styles ...string) string {
+	return outer(Yel)(msg, styles)
+}
+
+func (c *Color) Blue(msg interface{}, styles ...string) string {
+	return outer(Blu)(msg, styles)
+}
+
+func (c *Color) Magenta(msg interface{}, styles ...string) string {
+	return outer(Mgn)(msg, styles)
+}
+
+func (c *Color) Cyan(msg interface{}, styles ...string) string {
+	return outer(Cyn)(msg, styles)
+}
+
+func (c *Color) White(msg interface{}, styles ...string) string {
+	return outer(Wht)(msg, styles)
+}
+
+func (c *Color) Grey(msg interface{}, styles ...string) string {
+	return outer(Gry)(msg, styles)
+}
+
+func (c *Color) BlackBg(msg interface{}, styles ...string) string {
+	return outer(BlkBg)(msg, styles)
+}
+
+func (c *Color) RedBg(msg interface{}, styles ...string) string {
+	return outer(RdBg)(msg, styles)
+}
+
+func (c *Color) GreenBg(msg interface{}, styles ...string) string {
+	return outer(GrnBg)(msg, styles)
+}
+
+func (c *Color) YellowBg(msg interface{}, styles ...string) string {
+	return outer(YelBg)(msg, styles)
+}
+
+func (c *Color) BlueBg(msg interface{}, styles ...string) string {
+	return outer(BluBg)(msg, styles)
+}
+
+func (c *Color) MagentaBg(msg interface{}, styles ...string) string {
+	return outer(MgnBg)(msg, styles)
+}
+
+func (c *Color) CyanBg(msg interface{}, styles ...string) string {
+	return outer(CynBg)(msg, styles)
+}
+
+func (c *Color) WhiteBg(msg interface{}, styles ...string) string {
+	return outer(WhtBg)(msg, styles)
+}
+
+func (c *Color) Reset(msg interface{}, styles ...string) string {
+	return outer(Rst)(msg, styles)
+}
+
+func (c *Color) Bold(msg interface{}, styles ...string) string {
+	return outer(B)(msg, styles)
+}
+
+func (c *Color) Dim(msg interface{}, styles ...string) string {
+	return outer(Dm)(msg, styles)
+}
+
+func (c *Color) Italic(msg interface{}, styles ...string) string {
+	return outer(I)(msg, styles)
+}
+
+func (c *Color) Underline(msg interface{}, styles ...string) string {
+	return outer(U)(msg, styles)
+}
+
+func (c *Color) Inverse(msg interface{}, styles ...string) string {
+	return outer(In)(msg, styles)
+}
+
+func (c *Color) Hidden(msg interface{}, styles ...string) string {
+	return outer(Hd)(msg, styles)
+}
+
+func (c *Color) Strikeout(msg interface{}, styles ...string) string {
+	return outer(So)(msg, styles)
+}
+
+func Black(msg interface{}, styles ...string) string {
+	return global.Black(msg, styles...)
+}
+
+func Red(msg interface{}, styles ...string) string {
+	return global.Red(msg, styles...)
+}
+
+func Green(msg interface{}, styles ...string) string {
+	return global.Green(msg, styles...)
+}
+
+func Yellow(msg interface{}, styles ...string) string {
+	return global.Yellow(msg, styles...)
+}
+
+func Blue(msg interface{}, styles ...string) string {
+	return global.Blue(msg, styles...)
+}
+
+func Magenta(msg interface{}, styles ...string) string {
+	return global.Magenta(msg, styles...)
+}
+
+func Cyan(msg interface{}, styles ...string) string {
+	return global.Cyan(msg, styles...)
+}
+
+func White(msg interface{}, styles ...string) string {
+	return global.White(msg, styles...)
+}
+
+func Grey(msg interface{}, styles ...string) string {
+	return global.Grey(msg, styles...)
+}
+
+func BlackBg(msg interface{}, styles ...string) string {
+	return global.BlackBg(msg, styles...)
+}
+
+func RedBg(msg interface{}, styles ...string) string {
+	return global.RedBg(msg, styles...)
+}
+
+func GreenBg(msg interface{}, styles ...string) string {
+	return global.GreenBg(msg, styles...)
+}
+
+func YellowBg(msg interface{}, styles ...string) string {
+	return global.YellowBg(msg, styles...)
+}
+
+func BlueBg(msg interface{}, styles ...string) string {
+	return global.BlueBg(msg, styles...)
+}
+
+func MagentaBg(msg interface{}, styles ...string) string {
+	return global.MagentaBg(msg, styles...)
+}
+
+func CyanBg(msg interface{}, styles ...string) string {
+	return global.CyanBg(msg, styles...)
+}
+
+func WhiteBg(msg interface{}, styles ...string) string {
+	return global.WhiteBg(msg, styles...)
+}
+
+func Reset(msg interface{}, styles ...string) string {
+	return global.Reset(msg, styles...)
+}
+
+func Bold(msg interface{}, styles ...string) string {
+	return global.Bold(msg, styles...)
+}
+
+func Dim(msg interface{}, styles ...string) string {
+	return global.Dim(msg, styles...)
+}
+
+func Italic(msg interface{}, styles ...string) string {
+	return global.Italic(msg, styles...)
+}
+
+func Underline(msg interface{}, styles ...string) string {
+	return global.Underline(msg, styles...)
+}
+
+func Inverse(msg interface{}, styles ...string) string {
+	return global.Underline(msg, styles...)
+}
+
+func Hidden(msg interface{}, styles ...string) string {
+	return global.Hidden(msg, styles...)
+}
+
+func Strikeout(msg interface{}, styles ...string) string {
+	return global.Strikeout(msg, styles...)
+}

+ 2 - 2
color/color_test.go

@@ -46,7 +46,7 @@ func TestMixMatch(t *testing.T) {
 	fmt.Println("*** mix and match ***")
 	fmt.Println(Green("bold green with white background", B, WhtBg))
 	fmt.Println(Red("underline red", U))
-	fmt.Println(Yellow("dim yellow", D))
+	fmt.Println(Yellow("dim yellow", Dm))
 	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, Dm))
 }

+ 1 - 15
gytes/README.md

@@ -31,23 +31,9 @@ fmt.Println(gytes.Format(1323))
 
 `1.29 KiB`
 
-### New intance
-
-#### Decimal prefix 
+### New instance
 
 ```go
 g := New()
 fmt.Println(g.Format(13231323))
 ```
-
-`13.23 MB`
-
-#### Binary prefix
-
-```go
-g := New()
-g.SetBinaryPrefix(true)
-fmt.Println(g.Format(13231323))
-```
-
-`12.62 MiB`

+ 1 - 2
gytes/gytes.go

@@ -7,7 +7,7 @@ import (
 )
 
 var (
-	global = New()
+	global       = New()
 )
 
 type (
@@ -49,7 +49,6 @@ func (g *Gytes) Format(b uint64) string {
 	}
 }
 
-// SetBinaryPrefix wraps default instance's BinaryPrefix function.
 func BinaryPrefix(on bool) {
 	global.SetBinaryPrefix(on)
 }

+ 0 - 16
gytes/gytes_test.go

@@ -29,19 +29,3 @@ func TestGytes(t *testing.T) {
 	}
 }
 
-func TestNew(t *testing.T) {
-	g := New()
-
-	b := g.Format(132313231323)
-	if b != "132.31 GB" {
-		t.Errorf("expected `132.31 GB`, got %s", b)
-	}
-
-	// Binary prefix
-	g.SetBinaryPrefix(true)
-	println(g.Format(13231323))
-	b = Format(1323132313231323)
-	if b != "1.18 PiB" {
-		t.Errorf("expected `1.18 PiB`, got %s", b)
-	}
-}