mirror of
https://github.com/fatih/color.git
synced 2024-11-30 08:06:42 +02:00
Add PrintFunc.. functions and update readme.md
This commit is contained in:
parent
e653ecf96d
commit
c297f2223f
18
README.md
18
README.md
@ -4,6 +4,7 @@ Color let you use colorized outputs in terms of [ANSI Escape
|
|||||||
Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors). The API can be
|
Codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors). The API can be
|
||||||
used in several way, pick one one that suits you.
|
used in several way, pick one one that suits you.
|
||||||
|
|
||||||
|
The package is under ongoing development, checkout for regular updates.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
@ -51,6 +52,19 @@ whiteBackground := red.Add(color.BgWhite)
|
|||||||
whiteBackground.Println("Red text with White background.")
|
whiteBackground.Println("Red text with White background.")
|
||||||
```
|
```
|
||||||
|
|
||||||
|
### Custom print functions
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Create a custom print function for convenient
|
||||||
|
red := color.New(color.FgRed).PrintfFunc()
|
||||||
|
red("warning")
|
||||||
|
red("error: %s", err)
|
||||||
|
|
||||||
|
// Mix up multiple attributes
|
||||||
|
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
|
||||||
|
notice("don't forget this...")
|
||||||
|
```
|
||||||
|
|
||||||
### Plug into existing code
|
### Plug into existing code
|
||||||
|
|
||||||
```go
|
```go
|
||||||
@ -71,8 +85,8 @@ fmt.Println("All text will be now bold magenta.")
|
|||||||
|
|
||||||
## Todo
|
## Todo
|
||||||
|
|
||||||
* Add partial coloring of lines.
|
* Save/Return previous values
|
||||||
* Save/Return previous values
|
* Implement SprintXxx functions()
|
||||||
|
|
||||||
## Credits
|
## Credits
|
||||||
|
|
||||||
|
26
color.go
26
color.go
@ -128,6 +128,32 @@ func (c *Color) Println(a ...interface{}) (n int, err error) {
|
|||||||
return fmt.Fprintln(Output, a...)
|
return fmt.Fprintln(Output, a...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Color) Sprintln() func(a ...interface{}) string {
|
||||||
|
return func(a ...interface{}) string {
|
||||||
|
c.Set()
|
||||||
|
defer Unset()
|
||||||
|
return fmt.Sprintln(a...)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrintFunc returns a new function prints the passed arguments as colorized
|
||||||
|
// with color.Print().
|
||||||
|
func (c *Color) PrintFunc() func(a ...interface{}) {
|
||||||
|
return func(a ...interface{}) { c.Print(a...) }
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrintfFunc returns a new function prints the passed arguments as colorized
|
||||||
|
// with color.Printf().
|
||||||
|
func (c *Color) PrintfFunc() func(format string, a ...interface{}) {
|
||||||
|
return func(format string, a ...interface{}) { c.Printf(format, a...) }
|
||||||
|
}
|
||||||
|
|
||||||
|
// PrintlnFunc returns a new function prints the passed arguments as colorized
|
||||||
|
// with color.Println().
|
||||||
|
func (c *Color) PrintlnFunc() func(a ...interface{}) {
|
||||||
|
return func(a ...interface{}) { c.Println(a...) }
|
||||||
|
}
|
||||||
|
|
||||||
// sequence returns a formated SGR sequence to be plugged into a "\x1b[...m"
|
// sequence returns a formated SGR sequence to be plugged into a "\x1b[...m"
|
||||||
// an example output might be: "1;36" -> bold cyan
|
// an example output might be: "1;36" -> bold cyan
|
||||||
func (c *Color) sequence() string {
|
func (c *Color) sequence() string {
|
||||||
|
@ -87,6 +87,7 @@ func TestColor(t *testing.T) {
|
|||||||
White("white")
|
White("white")
|
||||||
|
|
||||||
// Third visual test
|
// Third visual test
|
||||||
|
fmt.Println()
|
||||||
Set(FgBlue)
|
Set(FgBlue)
|
||||||
fmt.Println("is this blue?")
|
fmt.Println("is this blue?")
|
||||||
Unset()
|
Unset()
|
||||||
@ -95,4 +96,12 @@ func TestColor(t *testing.T) {
|
|||||||
fmt.Println("and this magenta?")
|
fmt.Println("and this magenta?")
|
||||||
Unset()
|
Unset()
|
||||||
|
|
||||||
|
// Fourth Visual test
|
||||||
|
fmt.Println()
|
||||||
|
blue := New(FgBlue).PrintlnFunc()
|
||||||
|
blue("blue text with custom print func")
|
||||||
|
|
||||||
|
red := New(FgRed).PrintfFunc()
|
||||||
|
red("red text with a printf func: %d\n", 123)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
12
doc.go
12
doc.go
@ -39,6 +39,18 @@ separate color object.
|
|||||||
whiteBackground.Println("Red text with White background.")
|
whiteBackground.Println("Red text with White background.")
|
||||||
|
|
||||||
|
|
||||||
|
You can create PrintXxx functions to simplify even more:
|
||||||
|
|
||||||
|
// Create a custom print function for convenient
|
||||||
|
red := color.New(color.FgRed).PrintfFunc()
|
||||||
|
red("warning")
|
||||||
|
red("error: %s", err)
|
||||||
|
|
||||||
|
// Mix up multiple attributes
|
||||||
|
notice := color.New(color.Bold, color.FgGreen).PrintlnFunc()
|
||||||
|
notice("don't forget this...")
|
||||||
|
|
||||||
|
|
||||||
Using with existing color is possible too. Just use the Set() method to set
|
Using with existing color is possible too. Just use the Set() method to set
|
||||||
the standard output to the given parameters. That way a rewrite of an existing
|
the standard output to the given parameters. That way a rewrite of an existing
|
||||||
code is not required.
|
code is not required.
|
||||||
|
Loading…
Reference in New Issue
Block a user