diff --git a/README.md b/README.md index d68432e..bbef0d1 100644 --- a/README.md +++ b/README.md @@ -299,7 +299,7 @@ c.Printf("format %s", "message") Can be used to set foreground and background colors at the same time. -- `color.S256(fgAndBg ...uint8) *Style256` +- `S256(fgAndBg ...uint8) *Style256` ```go s := color.S256(32, 203) @@ -307,6 +307,16 @@ s.Println("message") s.Printf("format %s", "message") ``` +with options: + +```go +s := color.S256(32, 203) +s.SetOpts(color.Opts{color.OpBold}) + +s.Println("style with options") +s.Printf("style with %s\n", "options") +``` + Run demo: `go run ./_examples/color256.go` ![color-tags](_examples/images/color-256.png) @@ -335,7 +345,7 @@ color.HEXStyle("eee", "D50000").Println("deep-purple color") ### Set the foreground or background color - - `color.RGB(r, g, b uint8, isBg ...bool) RGBColor` +- `color.RGB(r, g, b uint8, isBg ...bool) RGBColor` ```go c := color.RGB(30,144,255) // fg color @@ -347,9 +357,9 @@ c.Println("message") c.Printf("format %s", "message") ``` - Create a style from an hexadecimal color string: +Create a style from an hexadecimal color string: - - `color.HEX(hex string, isBg ...bool) RGBColor` +- `color.HEX(hex string, isBg ...bool) RGBColor` ```go c := color.HEX("ccc") // can also: "cccccc" "#cccccc" @@ -373,7 +383,7 @@ s.Println("message") s.Printf("format %s", "message") ``` - Create a style from an hexadecimal color string: +Create a style from an hexadecimal color string: - `color.HEXStyle(fg string, bg ...string) *RGBStyle` @@ -383,6 +393,16 @@ s.Println("message") s.Printf("format %s", "message") ``` +with options: + +```go +s := color.HEXStyle("11aa23", "eee") +s.SetOpts(color.Opts{color.OpBold}) + +s.Println("style with options") +s.Printf("style with %s\n", "options") +``` + ## Func refer there are some useful functions reference @@ -390,6 +410,7 @@ there are some useful functions reference - `Disable()` disable color render - `SetOutput(io.Writer)` custom set the colored text output writer - `ForceOpenColor()` force open color render +- `Colors2code(colors ...Color) string` Convert colors to code. return like "32;45;3" - `ClearCode(str string) string` Use for clear color codes - `ClearTag(s string) string` clear all color html-tag for a string - `IsConsole(w io.Writer)` Determine whether w is one of stderr, stdout, stdin @@ -408,7 +429,7 @@ there are some useful functions reference - [gookit/filter](https://github.com/gookit/filter) Provide filtering, sanitizing, and conversion of golang data - [gookit/validate](https://github.com/gookit/validate) Use for data validation and filtering. support Map, Struct, Form data - [gookit/goutil](https://github.com/gookit/goutil) Some utils for the Go: string, array/slice, map, format, cli, env, filesystem, test and more - - More please see https://github.com/gookit + - More, please see https://github.com/gookit ## See also diff --git a/README.zh-CN.md b/README.zh-CN.md index 9d2b998..7f4dcb6 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -272,7 +272,7 @@ c.Printf("format %s", "message") > 可同时设置前景和背景色 - - `color.S256(fgAndBg ...uint8) *Style256` +- `color.S256(fgAndBg ...uint8) *Style256` ```go s := color.S256(32, 203) @@ -280,6 +280,16 @@ s.Println("message") s.Printf("format %s", "message") ``` +可以同时添加选项设置: + +```go +s := color.S256(32, 203) +s.SetOpts(color.Opts{color.OpBold}) + +s.Println("style with options") +s.Printf("style with %s\n", "options") +``` + > 运行 demo: `go run ./_examples/color256.go` ![color-tags](_examples/images/color-256.png) @@ -308,7 +318,7 @@ color.HEXStyle("eee", "D50000").Println("deep-purple color") ### 使用前景或后景色 - - `color.RGB(r, g, b uint8, isBg ...bool) RGBColor` +- `color.RGB(r, g, b uint8, isBg ...bool) RGBColor` ```go c := color.RGB(30,144,255) // fg color @@ -320,7 +330,7 @@ c.Println("message") c.Printf("format %s", "message") ``` - - `color.HEX(hex string, isBg ...bool) RGBColor` 从16进制颜色创建 +- `color.HEX(hex string, isBg ...bool) RGBColor` 从16进制颜色创建 ```go c := color.HEX("ccc") // 也可以写为: "cccccc" "#cccccc" @@ -336,7 +346,7 @@ c.Printf("format %s", "message") > 可同时设置前景和背景色 - - `color.NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyle` +- `color.NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyle` ```go s := color.NewRGBStyle(RGB(20, 144, 234), RGB(234, 78, 23)) @@ -352,6 +362,16 @@ s.Println("message") s.Printf("format %s", "message") ``` +- 可以同时添加选项设置: + +```go +s := color.HEXStyle("11aa23", "eee") +s.SetOpts(color.Opts{color.OpBold}) + +s.Println("style with options") +s.Printf("style with %s\n", "options") +``` + ## 方法参考 一些有用的工具方法参考 @@ -360,6 +380,7 @@ s.Printf("format %s", "message") - `SetOutput(io.Writer)` custom set the colored text output writer - `ForceOpenColor()` force open color render - `ClearCode(str string) string` Use for clear color codes +- `Colors2code(colors ...Color) string` Convert colors to code. return like "32;45;3" - `ClearTag(s string) string` clear all color html-tag for a string - `IsConsole(w io.Writer)` Determine whether w is one of stderr, stdout, stdin - `HexToRgb(hex string) (rgb []int)` Convert hex color string to RGB numbers diff --git a/_examples/basic_color.go b/_examples/basic_color.go index 68ca281..2083bf9 100644 --- a/_examples/basic_color.go +++ b/_examples/basic_color.go @@ -19,7 +19,7 @@ func main() { } fmt.Println("\nBasic Options:") - for name, c := range color.Options { + for name, c := range color.AllOptions { c.Print(" ", name, " ") } diff --git a/_examples/basiccolor.go b/_examples/basiccolor.go index 68ca281..2083bf9 100644 --- a/_examples/basiccolor.go +++ b/_examples/basiccolor.go @@ -19,7 +19,7 @@ func main() { } fmt.Println("\nBasic Options:") - for name, c := range color.Options { + for name, c := range color.AllOptions { c.Print(" ", name, " ") } diff --git a/color.go b/color.go index 0151814..247d763 100644 --- a/color.go +++ b/color.go @@ -73,7 +73,7 @@ func Set(colors ...Color) (int, error) { // return winSet(colors...) // } - return fmt.Printf(SettingTpl, colors2code(colors...)) + return fmt.Printf(SettingTpl, Colors2code(colors...)) } // Reset reset console color attributes diff --git a/color_16.go b/color_16.go index 9d42550..a064f7d 100644 --- a/color_16.go +++ b/color_16.go @@ -3,7 +3,6 @@ package color import ( "fmt" "strconv" - "strings" ) // Color Color16, 16 color value type @@ -34,7 +33,7 @@ func (o Opts) IsEmpty() bool { // String options to string. eg: "1;3" func (o Opts) String() string { - return colors2code(o...) + return Colors2code(o...) } /************************************************************* @@ -300,7 +299,12 @@ var ExBgColors = map[string]Color{ } // Options color options map -var Options = map[string]Color{ +// Deprecated +// NOTICE: please use AllOptions instead. +var Options = AllOptions + +// AllOptions color options map +var AllOptions = map[string]Color{ "reset": OpReset, "bold": OpBold, "fuzzy": OpFuzzy, @@ -310,21 +314,3 @@ var Options = map[string]Color{ "reverse": OpReverse, "concealed": OpConcealed, } - -/************************************************************* - * helper methods - *************************************************************/ - -// convert colors to code. return like "32;45;3" -func colors2code(colors ...Color) string { - if len(colors) == 0 { - return "" - } - - var codes []string - for _, color := range colors { - codes = append(codes, color.String()) - } - - return strings.Join(codes, ";") -} diff --git a/color_256.go b/color_256.go index ae37ba6..57990af 100644 --- a/color_256.go +++ b/color_256.go @@ -157,11 +157,11 @@ func S256(fgAndBg ...uint8) *Style256 { return s } -// Set fg and bg color value -func (s *Style256) Set(fgVal, bgVal uint8, ops ...Color) *Style256 { +// Set fg and bg color value, can also with color options +func (s *Style256) Set(fgVal, bgVal uint8, opts ...Color) *Style256 { s.fg = Color256{fgVal, 1} s.bg = Color256{bgVal, 1} - s.opts.Add(ops...) + s.opts.Add(opts...) return s } diff --git a/color_rgb.go b/color_rgb.go index a20b725..1fa4bae 100644 --- a/color_rgb.go +++ b/color_rgb.go @@ -169,7 +169,7 @@ func (c RGBColor) IsEmpty() bool { // C256 returns the closest approximate 256 (8 bit) color func (c RGBColor) C256() Color256 { - return C256(rgb2short(c[0], c[1], c[2]), c[3] == AsBg) + return C256(Rgb2short(c[0], c[1], c[2]), c[3] == AsBg) } /************************************************************* @@ -232,7 +232,7 @@ func RGBStyleFromString(fg string, bg ...string) *RGBStyle { return s.SetFg(RGBFromString(fg)) } -// Set fg and bg color +// Set fg and bg color, can also with color options func (s *RGBStyle) Set(fg, bg RGBColor, opts ...Color) *RGBStyle { return s.SetFg(fg).SetBg(bg).SetOpts(opts) } @@ -251,7 +251,7 @@ func (s *RGBStyle) SetBg(bg RGBColor) *RGBStyle { return s } -// SetOpts set options +// SetOpts set color options func (s *RGBStyle) SetOpts(opts Opts) *RGBStyle { s.opts = opts return s diff --git a/color_test.go b/color_test.go index 3e42dc9..2172f46 100644 --- a/color_test.go +++ b/color_test.go @@ -302,7 +302,7 @@ func TestPrintBasicColor(t *testing.T) { } fmt.Println("\nBasic Options:") - for name, c := range Options { + for name, c := range AllOptions { c.Print(" ", name, " ") } diff --git a/rgb_to_256.go b/rgb_to_256.go index f29bf26..c9ce3cd 100644 --- a/rgb_to_256.go +++ b/rgb_to_256.go @@ -280,7 +280,8 @@ var ( incs = []uint8{0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff} ) -func rgb2short(r, g, b uint8) uint8 { +// Rgb2short convert RGB-code to 256-code +func Rgb2short(r, g, b uint8) uint8 { res := make([]uint8, 3) for partI, part := range [3]uint8{r, g, b} { i := 0 diff --git a/style.go b/style.go index f2e8812..9b36a87 100644 --- a/style.go +++ b/style.go @@ -79,7 +79,7 @@ func (s Style) Code() string { // String convert to code string. returns like "32;45;3" func (s Style) String() string { - return colors2code(s...) + return Colors2code(s...) } // IsEmpty style diff --git a/tag.go b/tag.go index 5963b3b..dec77b8 100644 --- a/tag.go +++ b/tag.go @@ -149,7 +149,7 @@ func ReplaceTag(str string) string { // ParseCodeFromAttr parse color attributes. // attr like: -// "fg=VALUE;bg=VALUE;op=VALUE" // VALUE please see var: FgColors, BgColors, Options +// "fg=VALUE;bg=VALUE;op=VALUE" // VALUE please see var: FgColors, BgColors, AllOptions // eg: // "fg=yellow" // "bg=red" @@ -188,17 +188,17 @@ func ParseCodeFromAttr(attr string) (code string) { if strings.Contains(val, ",") { ns := strings.Split(val, ",") for _, n := range ns { - if c, ok := Options[n]; ok { + if c, ok := AllOptions[n]; ok { colors = append(colors, c) } } - } else if c, ok := Options[val]; ok { + } else if c, ok := AllOptions[val]; ok { colors = append(colors, c) } } } - return colors2code(colors...) + return Colors2code(colors...) } // ClearTag clear all tag for a string diff --git a/utils.go b/utils.go index 4b3a5fe..8572a42 100644 --- a/utils.go +++ b/utils.go @@ -26,6 +26,24 @@ var specialColorTerms = map[string]bool{ "rxvt-unicode-256color": true, } +/************************************************************* + * helper methods + *************************************************************/ + +// Colors2code convert colors to code. return like "32;45;3" +func Colors2code(colors ...Color) string { + if len(colors) == 0 { + return "" + } + + var codes []string + for _, color := range colors { + codes = append(codes, color.String()) + } + + return strings.Join(codes, ";") +} + // IsConsole Determine whether w is one of stderr, stdout, stdin func IsConsole(w io.Writer) bool { o, ok := w.(*os.File) @@ -258,8 +276,12 @@ func Render(a ...interface{}) string { } // Sprint parse color tags, return rendered string -func Sprint(args ...interface{}) string { - return Render(args...) +func Sprint(a ...interface{}) string { + if len(a) == 0 { + return "" + } + + return ReplaceTag(fmt.Sprint(a...)) } // Sprintf format and return rendered string