Skip to content

Commit

Permalink
update readme. add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
inhere committed Aug 23, 2018
1 parent 801a2ac commit 2b36b34
Show file tree
Hide file tree
Showing 10 changed files with 351 additions and 73 deletions.
86 changes: 84 additions & 2 deletions README.md
Expand Up @@ -12,12 +12,14 @@ Command line color library. rich color rendering output, universal API method, c
## Features

- Simple to use
- Supports rich color output
- Supports rich color output 16色(4bit),256色(8bit),RGB色彩(24bit)
- 16 color (4bit) is the most commonly used and most widely supported, supporting Windows `cmd.exe`
- 256 and RGB color support `linux` `mac` and Windows `CONEMU` `git-bash` `mintty` part terminal
- Generic API method: `Print` `Printf` `Println` `Sprint` `Sprintf`
- Supports html tab-style color rendering. like: `<green>message</>`
- Compatible with Windows system environment
- Basic color: `Bold` `Black` `White` `Gray` `Red` `Green` `Yellow` `Blue` `Magenta` `Cyan`
- Extra style: `Info` `Note` `Light` `Error` `Danger` `Notice` `Success` `Comment` `Primary` `Warning` `Question` `Secondary`
- Compatible with Windows system environment

## Install

Expand Down Expand Up @@ -178,6 +180,86 @@ color.Tag("info").Println("info style text")
![color-tags](_examples/images/color-tags.jpg)

## Use 256 color

### Use foreground or background color

- `color.C256(val uint8, isBg ...bool) Color256`

```go
c := color.C256(132) // fg color
c.Println("message")
c.Printf("format %s", "message")

c := color.C256(132, true) // bg color
c.Println("message")
c.Printf("format %s", "message")
```

### Use 256 color style

> Can set foreground and background colors at the same time
- `color.S256(fgAndBg ...uint8) *Style256`

```go
s := color.S256(32, 203)
s.Println("message")
s.Printf("format %s", "message")
```

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

## Use RGB color

### Use foreground or background color

- `color.RGB(r, g, b uint8, isBg ...bool) RGBColor`

```go
c := color.RGB(30,144,255) // fg color
c.Println("message")
c.Printf("format %s", "message")

c := color.RGB(30,144,255, true) // bg color
c.Println("message")
c.Printf("format %s", "message")
```

- `color.HEX(hex string, isBg ...bool) RGBColor` Create from hexadecimal color string

```go
c := HEX("ccc") // 也可以写为: "cccccc" "#cccccc"
c.Println("message")
c.Printf("format %s", "message")

c = HEX("aabbcc", true) // as bg color
c.Println("message")
c.Printf("format %s", "message")
```

### Use RGB color style

> Can set foreground and background colors at the same time
- `color.NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyle`

```go
s := NewRGBStyle(RGB(20, 144, 234), RGB(234, 78, 23))
s.Println("message")
s.Printf("format %s", "message")
```

- `color.HEXStyle(fg string, bg ...string) *RGBStyle` Create from hexadecimal color string

```go
s := HEXStyle("11aa23", "eee")
s.Println("message")
s.Printf("format %s", "message")
```

## Refer

- `issue9/term` https://github.com/issue9/term
Expand Down
93 changes: 87 additions & 6 deletions README_cn.md
Expand Up @@ -5,19 +5,21 @@
[![Coverage Status](https://coveralls.io/repos/github/gookit/color/badge.svg?branch=master)](https://coveralls.io/github/gookit/color?branch=master)
[![Go Report Card](https://goreportcard.com/badge/github.com/gookit/color)](https://goreportcard.com/report/github.com/gookit/color)

golang下的命令行色彩使用库
golang下的命令行色彩使用库, 拥有丰富的色彩渲染输出,通用的API方法,兼容Windows系统

**[EN Readme](README.md)**

## 功能特色

- 使用简单方便
- 支持丰富的颜色输出
- 支持丰富的颜色输出, 16色(4bit),256色(8bit),RGB色彩(24bit)
- 16色(4bit)是最常用和支持最广的,支持Windows `cmd.exe`
- 另外两种支持 `linux` `mac` 和 Windows下的 `CONEMU` `git-bash` `mintty` 等部分终端
- 通用的API方法:`Print` `Printf` `Println` `Sprint` `Sprintf`
- 同时支持html标签式的颜色渲染. eg: `<green>message</>`
- 兼容Windows系统环境
- 基础色彩: `Bold` `Black` `White` `Gray` `Red` `Green` `Yellow` `Blue` `Magenta` `Cyan`
- 扩展风格: `Info` `Note` `Light` `Error` `Danger` `Notice` `Success` `Comment` `Primary` `Warning` `Question` `Secondary`
- 兼容Windows系统环境

## 获取安装

Expand Down Expand Up @@ -179,12 +181,11 @@ color.Success.Println("Success message")
![theme-style](_examples/images/theme-style.jpg)


### 使用颜色html标签
### 使用颜色标签

> **** 支持在windows `cmd.exe` 使用,但不影响使用,会自动去除颜色标签
使用内置的颜色标签可以非常方便简单的构建自己需要的任何格式
使用内置的颜色标签,可以非常方便简单的构建自己需要的任何格式

```go
// 使用内置的 color tag
Expand Down Expand Up @@ -212,6 +213,86 @@ color.Tag("info").Println("info style text")
![color-tags](_examples/images/color-tags.jpg)

## 256色使用

### 使用前景或后景色

- `color.C256(val uint8, isBg ...bool) Color256`

```go
c := color.C256(132) // fg color
c.Println("message")
c.Printf("format %s", "message")

c := color.C256(132, true) // bg color
c.Println("message")
c.Printf("format %s", "message")
```

### 使用风格

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

```go
s := color.S256(32, 203)
s.Println("message")
s.Printf("format %s", "message")
```

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

## RGB色彩使用

### 使用前景或后景色

- `color.RGB(r, g, b uint8, isBg ...bool) RGBColor`

```go
c := color.RGB(30,144,255) // fg color
c.Println("message")
c.Printf("format %s", "message")

c := color.RGB(30,144,255, true) // bg color
c.Println("message")
c.Printf("format %s", "message")
```

- `color.HEX(hex string, isBg ...bool) RGBColor` 从16进制颜色创建

```go
c := HEX("ccc") // 也可以写为: "cccccc" "#cccccc"
c.Println("message")
c.Printf("format %s", "message")

c = HEX("aabbcc", true) // as bg color
c.Println("message")
c.Printf("format %s", "message")
```

### 使用风格

> 可同时设置前景和背景色
- `color.NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyle`

```go
s := NewRGBStyle(RGB(20, 144, 234), RGB(234, 78, 23))
s.Println("message")
s.Printf("format %s", "message")
```

- `color.HEXStyle(fg string, bg ...string) *RGBStyle` 从16进制颜色创建

```go
s := HEXStyle("11aa23", "eee")
s.Println("message")
s.Printf("format %s", "message")
```

## 参考项目

- `issue9/term` https://github.com/issue9/term
Expand Down
25 changes: 16 additions & 9 deletions color.go
Expand Up @@ -16,22 +16,19 @@ import (
)

// console color mode
const (
ModeNormal = iota
Mode256 // 8 bite
ModeRGB // 24 bite
ModeGrayscale
)
// const (
// ModeNormal = iota
// Mode256 // 8 bite
// ModeRGB // 24 bite
// ModeGrayscale
// )

// color render templates
const (
SettingTpl = "\x1b[%sm"
FullColorTpl = "\x1b[%sm%s\x1b[0m"
)

// ResetCode value
const ResetCode = "0"

// ResetSet 重置/正常 关闭所有属性。
const ResetSet = "\x1b[0m"

Expand Down Expand Up @@ -156,6 +153,11 @@ type Printer struct {
ColorCode string
}

// NewPrinter instance
func NewPrinter(colorCode string) *Printer {
return &Printer{colorCode}
}

// String returns color code string. eg: "32;45;3"
func (p *Printer) String() string {
// panic("implement me")
Expand Down Expand Up @@ -186,3 +188,8 @@ func (p *Printer) Printf(format string, a ...interface{}) {
func (p *Printer) Println(a ...interface{}) {
fmt.Println(RenderCode(p.String(), a...))
}

// IsEmpty color code
func (p *Printer) IsEmpty() bool {
return p.ColorCode == ""
}
2 changes: 1 addition & 1 deletion color_256.go
Expand Up @@ -107,7 +107,7 @@ func (c Color256) String() string {
}

// empty
return ResetCode
return ""
}

// IsEmpty value
Expand Down
13 changes: 2 additions & 11 deletions color_not_win.go
Expand Up @@ -5,22 +5,13 @@

package color

// winSet
func winSet(_ ...Color) (n int, err error) {
return
}

// winReset
func winReset() (n int, err error) {
return
}

// winPrint
func winPrint(_ string, _ ...Color) (n int, err error) {
return
}

// winPrintln
func winPrintln(_ string, _ ...Color) (n int, err error) {
return
}
func winPrint(_ string, _ ...Color) {}
func winPrintln(_ string, _ ...Color) {}
8 changes: 4 additions & 4 deletions color_rgb.go
Expand Up @@ -147,7 +147,7 @@ func (c RGBColor) String() string {
}

// >1 is empty
return ResetCode
return ""
}

// IsEmpty value
Expand Down Expand Up @@ -314,6 +314,6 @@ func (s *RGBStyle) IsEmpty() bool {
}

// RGBto256 value
func RGBto256(r, g, b uint8) {

}
// func RGBto256(r, g, b uint8) {
//
// }

0 comments on commit 2b36b34

Please sign in to comment.