mirror of
https://github.com/fatih/color.git
synced 2024-11-24 08:02:14 +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
|
||||
used in several way, pick one one that suits you.
|
||||
|
||||
The package is under ongoing development, checkout for regular updates.
|
||||
|
||||
## Install
|
||||
|
||||
@ -51,6 +52,19 @@ whiteBackground := red.Add(color.BgWhite)
|
||||
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
|
||||
|
||||
```go
|
||||
@ -71,8 +85,8 @@ fmt.Println("All text will be now bold magenta.")
|
||||
|
||||
## Todo
|
||||
|
||||
* Add partial coloring of lines.
|
||||
* Save/Return previous values
|
||||
* Save/Return previous values
|
||||
* Implement SprintXxx functions()
|
||||
|
||||
## 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...)
|
||||
}
|
||||
|
||||
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"
|
||||
// an example output might be: "1;36" -> bold cyan
|
||||
func (c *Color) sequence() string {
|
||||
|
@ -87,6 +87,7 @@ func TestColor(t *testing.T) {
|
||||
White("white")
|
||||
|
||||
// Third visual test
|
||||
fmt.Println()
|
||||
Set(FgBlue)
|
||||
fmt.Println("is this blue?")
|
||||
Unset()
|
||||
@ -95,4 +96,12 @@ func TestColor(t *testing.T) {
|
||||
fmt.Println("and this magenta?")
|
||||
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.")
|
||||
|
||||
|
||||
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
|
||||
the standard output to the given parameters. That way a rewrite of an existing
|
||||
code is not required.
|
||||
|
Loading…
Reference in New Issue
Block a user