Skip to content

Commit

Permalink
update some method comments. export some methods
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Nov 4, 2020
1 parent 2904452 commit c9f591d
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 49 deletions.
33 changes: 27 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,24 @@ 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)
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)
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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`

Expand All @@ -383,13 +393,24 @@ 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

- `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
Expand All @@ -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

Expand Down
29 changes: 25 additions & 4 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -272,14 +272,24 @@ c.Printf("format %s", "message")

> 可同时设置前景和背景色
- `color.S256(fgAndBg ...uint8) *Style256`
- `color.S256(fgAndBg ...uint8) *Style256`

```go
s := color.S256(32, 203)
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)
Expand Down Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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))
Expand All @@ -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")
```

## 方法参考

一些有用的工具方法参考
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion _examples/basic_color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")
}

Expand Down
2 changes: 1 addition & 1 deletion _examples/basiccolor.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")
}

Expand Down
2 changes: 1 addition & 1 deletion color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 7 additions & 21 deletions color_16.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package color
import (
"fmt"
"strconv"
"strings"
)

// Color Color16, 16 color value type
Expand Down Expand Up @@ -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...)
}

/*************************************************************
Expand Down Expand Up @@ -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,
Expand All @@ -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, ";")
}
6 changes: 3 additions & 3 deletions color_256.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
6 changes: 3 additions & 3 deletions color_rgb.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

/*************************************************************
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")
}

Expand Down
3 changes: 2 additions & 1 deletion rgb_to_256.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion style.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
26 changes: 24 additions & 2 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c9f591d

Please sign in to comment.