Skip to content

Commit

Permalink
feat: custom color tag support use 256,hex and rgb code
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Apr 9, 2021
1 parent f683000 commit b72663c
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 99 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ color.Tag("info").Printf("%s style text", "info")
color.Tag("info").Println("info style text")
```

Run demo: `go run ./_examples/colortag.go`
Run demo: `go run ./_examples/color_tag.go`

![color-tags](_examples/images/color-tags.png)

Expand Down Expand Up @@ -327,7 +327,7 @@ s.Println("style with options")
s.Printf("style with %s\n", "options")
```

Run demo: `go run ./_examples/color256.go`
Run demo: `go run ./_examples/color_256.go`

![color-tags](_examples/images/color-256.png)

Expand Down
4 changes: 2 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ color.Tag("info").Printf("%s style text", "info")
color.Tag("info").Println("info style text")
```

> 运行 demo: `go run ./_examples/colortag.go`
> 运行 demo: `go run ./_examples/color_tag.go`
![color-tags](_examples/images/color-tags.png)

Expand Down Expand Up @@ -300,7 +300,7 @@ s.Println("style with options")
s.Printf("style with %s\n", "options")
```

> 运行 demo: `go run ./_examples/color256.go`
> 运行 demo: `go run ./_examples/color_256.go`
![color-tags](_examples/images/color-256.png)

Expand Down
3 changes: 1 addition & 2 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ func RenderString(code string, str string) string {
}

// ClearCode clear color codes.
// eg:
// "\033[36;1mText\x1b[0m" -> "Text"
// eg: "\033[36;1mText\x1b[0m" -> "Text"
func ClearCode(str string) string {
return codeRegex.ReplaceAllString(str, "")
}
78 changes: 49 additions & 29 deletions color_16.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func Bit4(code uint8) Color {

// Name get color code name.
func (c Color) Name() string {
name, ok := Basic2name[uint8(c)]
name, ok := basic2nameMap[uint8(c)]
if ok {
return name
}
Expand Down Expand Up @@ -389,32 +389,52 @@ var AllOptions = map[string]Color{
"concealed": OpConcealed,
}

// Basic2name basic color code to name
var Basic2name = map[uint8]string{
30: "black",
31: "red",
32: "green",
33: "yellow",
34: "blue",
35: "magenta",
36: "cyan",
37: "white",
// hi color code
90: "lightBlack",
91: "lightRed",
92: "lightGreen",
93: "lightYellow",
94: "lightBlue",
95: "lightMagenta",
96: "lightCyan",
97: "lightWhite",
// options
0: "reset",
1: "bold",
2: "fuzzy",
3: "italic",
4: "underscore",
5: "blink",
7: "reverse",
8: "concealed",
var (
// TODO basic name alias
// basicNameAlias = map[string]string{}

// basic color name to code
name2basicMap = initName2basicMap()
// basic2nameMap basic color code to name
basic2nameMap = map[uint8]string{
30: "black",
31: "red",
32: "green",
33: "yellow",
34: "blue",
35: "magenta",
36: "cyan",
37: "white",
// hi color code
90: "lightBlack",
91: "lightRed",
92: "lightGreen",
93: "lightYellow",
94: "lightBlue",
95: "lightMagenta",
96: "lightCyan",
97: "lightWhite",
// options
0: "reset",
1: "bold",
2: "fuzzy",
3: "italic",
4: "underscore",
5: "blink",
7: "reverse",
8: "concealed",
}
)

// Basic2nameMap data
func Basic2nameMap() map[uint8]string {
return basic2nameMap
}

func initName2basicMap() map[string]uint8 {
n2b := make(map[string]uint8, len(basic2nameMap))
for u, s := range basic2nameMap {
n2b[s] = u
}
return n2b
}
9 changes: 7 additions & 2 deletions color_256.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,17 @@ func (c Color256) Value() uint8 {
return c[0]
}

// Code convert to color code string. eg: "38;5;12"
// Code convert to color code string. eg: "12"
func (c Color256) Code() string {
return strconv.Itoa(int(c[0]))
}

// FullCode convert to color code string with prefix. eg: "38;5;12"
func (c Color256) FullCode() string {
return c.String()
}

// String convert to color code string. eg: "38;5;12"
// String convert to color code string with prefix. eg: "38;5;12"
func (c Color256) String() string {
if c[1] == AsFg { // 0 is Fg
// return fmt.Sprintf(TplFg256, c[0])
Expand Down
24 changes: 18 additions & 6 deletions color_rgb.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
const (
TplFgRGB = "38;2;%d;%d;%d"
TplBgRGB = "48;2;%d;%d;%d"
FgRGBPfx = "38;2;"
BgRGBPfx = "48;2;"
)

// mark color is fg or bg.
Expand Down Expand Up @@ -174,25 +176,30 @@ func (c RGBColor) Sprint(a ...interface{}) string {

// Sprintf returns format and rendered message
func (c RGBColor) Sprintf(format string, a ...interface{}) string {
return RenderString(c.Code(), fmt.Sprintf(format, a...))
return RenderString(c.String(), fmt.Sprintf(format, a...))
}

// Values to RGB values
func (c RGBColor) Values() []int {
return []int{int(c[0]), int(c[1]), int(c[2])}
}

// Code to color code string
// Code to color code string without prefix. eg: "204;123;56"
func (c RGBColor) Code() string {
return c.String()
return fmt.Sprintf("%d;%d;%d", c[0], c[1], c[2])
}

// Hex color rgb to hex string. as in "ff0080".
func (c RGBColor) Hex() string {
return fmt.Sprintf("%02x%02x%02x", c[0], c[1], c[2])
}

// String to color code string. eg: "38;2;204;123;56"
// FullCode to color code string with prefix
func (c RGBColor) FullCode() string {
return c.String()
}

// String to color code string with prefix. eg: "38;2;204;123;56"
func (c RGBColor) String() string {
if c[3] == AsFg {
return fmt.Sprintf(TplFgRGB, c[0], c[1], c[2])
Expand All @@ -202,7 +209,7 @@ func (c RGBColor) String() string {
return fmt.Sprintf(TplBgRGB, c[0], c[1], c[2])
}

// >1 is empty
// c[3] > 1 is empty
return ""
}

Expand Down Expand Up @@ -346,14 +353,19 @@ func (s *RGBStyle) Sprint(a ...interface{}) string {

// Sprintf returns format and rendered message
func (s *RGBStyle) Sprintf(format string, a ...interface{}) string {
return RenderString(s.Code(), fmt.Sprintf(format, a...))
return RenderString(s.String(), fmt.Sprintf(format, a...))
}

// Code convert to color code string
func (s *RGBStyle) Code() string {
return s.String()
}

// FullCode convert to color code string
func (s *RGBStyle) FullCode() string {
return s.String()
}

// String convert to color code string
func (s *RGBStyle) String() string {
var ss []string
Expand Down
8 changes: 7 additions & 1 deletion color_rgb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ func TestRGBColor(t *testing.T) {
// bg
c = RGB(204, 204, 204, true)
is.False(c.IsEmpty())
is.Equal("48;2;204;204;204", c.FullCode())
is.Equal("48;2;204;204;204", c.String())

// fg
c = RGB(204, 204, 204)
is.False(c.IsEmpty())
is.Equal("38;2;204;204;204", c.FullCode())
is.Equal("38;2;204;204;204", c.String())

// RGBColor.Sprint
Expand All @@ -38,8 +40,10 @@ func TestRGBColor(t *testing.T) {
// RGBColor.Sprintf
str = c.Sprintf("msg")
is.Equal("\x1b[38;2;204;204;204mmsg\x1b[0m", str)
is.Equal("[204 204 204]", fmt.Sprint(c.Values()))
is.Equal("cccccc", c.Hex())
is.Equal("204;204;204", c.Code())
is.Equal("38;2;204;204;204", c.FullCode())
is.Equal("[204 204 204]", fmt.Sprint(c.Values()))

// RGBColor.Print
c.Print("msg")
Expand Down Expand Up @@ -130,9 +134,11 @@ func TestRGBStyle(t *testing.T) {
s = &RGBStyle{}
s.Set(fg, bg, OpUnderscore)
is.False(s.IsEmpty())
is.Equal("38;2;20;144;234;48;2;234;78;23;4", s.FullCode())
is.Equal("38;2;20;144;234;48;2;234;78;23;4", s.String())

s.SetOpts(Opts{OpBold, OpBlink})
is.Equal("38;2;20;144;234;48;2;234;78;23;1;5", s.Code())
is.Equal("38;2;20;144;234;48;2;234;78;23;1;5", s.String())

s.AddOpts(OpItalic)
Expand Down
Loading

0 comments on commit b72663c

Please sign in to comment.