浏览代码

More functions for color

Signed-off-by: Vishal Rana <vr@labstack.com>
Vishal Rana 10 年之前
父节点
当前提交
3f1e70588b
共有 3 个文件被更改,包括 205 次插入14 次删除
  1. 47 0
      color/README.md
  2. 106 14
      color/color.go
  3. 52 0
      color/color_test.go

+ 47 - 0
color/README.md

@@ -0,0 +1,47 @@
+### Gommon/color
+Color package for go.
+
+### Example
+```go
+fmt.Println("*** text ***")
+fmt.Println(color.Black("black"))
+fmt.Println(color.Red("red"))
+fmt.Println(color.Green("green"))
+fmt.Println(color.Yellow("yellow"))
+fmt.Println(color.Blue("blue"))
+fmt.Println(color.Magenta("magenta"))
+fmt.Println(color.Cyan("cyan"))
+fmt.Println(color.White("white"))
+fmt.Println(color.Grey("grey"))
+```
+
+```go
+fmt.Println("*** background ***")
+fmt.Println(color.BlackBg("black background", color.Wht))
+fmt.Println(color.RedBg("red background"))
+fmt.Println(color.GreenBg("green background"))
+fmt.Println(color.YellowBg("yellow background"))
+fmt.Println(color.BlueBg("blue background"))
+fmt.Println(color.MagentaBg("magenta background"))
+fmt.Println(color.CyanBg("cyan background"))
+fmt.Println(color.WhiteBg("white background"))
+```
+
+```go
+fmt.Println("*** emphasis ***")
+fmt.Println(color.Bold("bold"))
+fmt.Println(color.Dim("dim"))
+fmt.Println(color.Italic("italic"))
+fmt.Println(color.Underline("underline"))
+fmt.Println(color.Inverse("inverse"))
+fmt.Println(color.Hidden("hidden"))
+fmt.Println(color.Strikeout("strikeout"))
+```
+
+```go
+fmt.Println("*** combo ***")
+fmt.Println(Green("bold green with white background", B, WhtBg))
+fmt.Println(Red("underline red", U))
+fmt.Println(Yellow("dim yellow", D))
+fmt.Println(Cyan("inverse cyan", In))
+```

+ 106 - 14
color/color.go

@@ -1,23 +1,115 @@
 package color
 
-import "fmt"
-
-var (
-	Red     = outer(31)
-	Green   = outer(32)
-	Yellow  = outer(33)
-	Blue    = outer(34)
-	Magenta = outer(35)
-	Cyan    = outer(36)
-	White   = outer(37)
+import (
+	"bytes"
+	"fmt"
 )
 
 type (
-	inner func(m interface{}) string
+	inner func(interface{}, ...string) string
+)
+
+// Color styles
+const (
+	// Blk Black text style
+	Blk = "30"
+	// Rd red text style
+	Rd = "31"
+	// Grn green text style
+	Grn = "32"
+	// Yel yellow text style
+	Yel = "33"
+	// Blu blue text style
+	Blu = "34"
+	// Mgn magenta text style
+	Mgn = "35"
+	// Cyn cyan text style
+	Cyn = "36"
+	// Wht white text style
+	Wht = "37"
+	// Gry grey text style
+	Gry = "90"
+
+	// BlkBg black background style
+	BlkBg = "40"
+	// RdBg red background style
+	RdBg = "41"
+	// GrnBg green background style
+	GrnBg = "42"
+	// YelBg yellow background style
+	YelBg = "43"
+	// BluBg blue background style
+	BluBg = "44"
+	// MgnBg magenta background style
+	MgnBg = "45"
+	// CynBg cyan background style
+	CynBg = "46"
+	// WhtBg white background style
+	WhtBg = "47"
+
+	// R reset emphasis style
+	R = "0"
+	// B bold emphasis style
+	B = "1"
+	// D dim emphasis style
+	D = "2"
+	// I italic emphasis style
+	I = "3"
+	// U underline emphasis style
+	U = "4"
+	// In inverse emphasis style
+	In = "7"
+	// Hd hidden emphasis style
+	Hd = "8"
+	// So strikeout emphasis style
+	So = "9"
+)
+
+// 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)
 )
 
-func outer(n int) inner {
-	return func(m interface{}) string {
-		return fmt.Sprintf("\033[%dm%v\033[0m", n, m)
+func outer(n string) inner {
+	return func(m interface{}, style ...string) string {
+		b := new(bytes.Buffer)
+		b.WriteString("\x1b[")
+		b.WriteString(n)
+		for _, s := range style {
+			b.WriteString(";")
+			b.WriteString(s)
+		}
+		b.WriteString("m")
+		// TODO: Replace fmt for performance
+		return fmt.Sprintf("%s%v\x1b[0m", b.String(), m)
 	}
 }

+ 52 - 0
color/color_test.go

@@ -0,0 +1,52 @@
+package color
+
+import (
+	"fmt"
+	"testing"
+
+	"github.com/labstack/gommon/color"
+)
+
+func TestText(t *testing.T) {
+	fmt.Println("*** text ***")
+	fmt.Println(color.Black("black"))
+	fmt.Println(color.Red("red"))
+	fmt.Println(color.Green("green"))
+	fmt.Println(color.Yellow("yellow"))
+	fmt.Println(color.Blue("blue"))
+	fmt.Println(color.Magenta("magenta"))
+	fmt.Println(color.Cyan("cyan"))
+	fmt.Println(color.White("white"))
+	fmt.Println(color.Grey("grey"))
+}
+
+func TestBackground(t *testing.T) {
+	fmt.Println("*** background ***")
+	fmt.Println(color.BlackBg("black background", color.Wht))
+	fmt.Println(color.RedBg("red background"))
+	fmt.Println(color.GreenBg("green background"))
+	fmt.Println(color.YellowBg("yellow background"))
+	fmt.Println(color.BlueBg("blue background"))
+	fmt.Println(color.MagentaBg("magenta background"))
+	fmt.Println(color.CyanBg("cyan background"))
+	fmt.Println(color.WhiteBg("white background"))
+}
+
+func TestEmphasis(t *testing.T) {
+	fmt.Println("*** emphasis ***")
+	fmt.Println(color.Bold("bold"))
+	fmt.Println(color.Dim("dim"))
+	fmt.Println(color.Italic("italic"))
+	fmt.Println(color.Underline("underline"))
+	fmt.Println(color.Inverse("inverse"))
+	fmt.Println(color.Hidden("hidden"))
+	fmt.Println(color.Strikeout("strikeout"))
+}
+
+func TestCombo(t *testing.T) {
+	fmt.Println("*** combo ***")
+	fmt.Println(Green("bold green with white background", B, WhtBg))
+	fmt.Println(Red("underline red", U))
+	fmt.Println(Yellow("dim yellow", D))
+	fmt.Println(Cyan("inverse cyan", In))
+}