mirror of
https://github.com/fatih/color.git
synced 2025-11-23 21:54:41 +02:00
Improve README and add examples to test.
This commit is contained in:
20
LICENSE.md
Normal file
20
LICENSE.md
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
The MIT License (MIT)
|
||||||
|
|
||||||
|
Copyright (c) 2013 Fatih Arslan
|
||||||
|
|
||||||
|
Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||||
|
this software and associated documentation files (the "Software"), to deal in
|
||||||
|
the Software without restriction, including without limitation the rights to
|
||||||
|
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
|
||||||
|
the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
subject to the following conditions:
|
||||||
|
|
||||||
|
The above copyright notice and this permission notice shall be included in all
|
||||||
|
copies or substantial portions of the Software.
|
||||||
|
|
||||||
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||||
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||||
|
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||||
|
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
|
||||||
|
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||||
|
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
66
README.md
66
README.md
@@ -0,0 +1,66 @@
|
|||||||
|
# Color [](http://godoc.org/github.com/fatih/color)
|
||||||
|
|
||||||
|
Color let you use colorized outputs in terms of [ASCI 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.
|
||||||
|
|
||||||
|
|
||||||
|
## Install
|
||||||
|
|
||||||
|
```bash
|
||||||
|
go get github.com/fatih/color
|
||||||
|
```
|
||||||
|
|
||||||
|
## Examples
|
||||||
|
|
||||||
|
# Standard colors
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Print with default foreground colors
|
||||||
|
color.Cyan.Print("Prints text in cyan.")
|
||||||
|
color.Blue.Print("Prints text in blue.")
|
||||||
|
|
||||||
|
// Chain SGR paramaters
|
||||||
|
color.Green.Add(color.Bold).Println("Green with bold")
|
||||||
|
color.Red.Add(color.BgWhite, color.Underline).Printf("Red with Black background and underscore: %s\n", "format too!")
|
||||||
|
```
|
||||||
|
|
||||||
|
# Custom colors
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Create and reuse color objects
|
||||||
|
c := color.Cyan.Add(color.Underline)
|
||||||
|
c.Println("Prints bold cyan.")
|
||||||
|
c.Printf("Thir prints bold cyan %s\n", "too!.")
|
||||||
|
|
||||||
|
|
||||||
|
// Create custom color objects:
|
||||||
|
c := color.New(color.FgGreen, color.BgCyan, color.Italic)
|
||||||
|
c.Print("Italic green with cyan backround")
|
||||||
|
```
|
||||||
|
|
||||||
|
# Plug into your existing code
|
||||||
|
|
||||||
|
```go
|
||||||
|
// Use handy standard colors.
|
||||||
|
color.Yello.Set()
|
||||||
|
fmt.Println("Existing text in your codebase will be now in Cyan")
|
||||||
|
fmt.Printf("This one %s\n", "too")
|
||||||
|
color.Unset() // don't forget to unset
|
||||||
|
|
||||||
|
// You can use set custom objects too
|
||||||
|
color.New(color.FgBlack, color.BgWhite, color.Bold).Set()
|
||||||
|
defer color.Unset() // use it in your function
|
||||||
|
|
||||||
|
fmt.Println("All text will be now bold red with white background.")
|
||||||
|
```
|
||||||
|
|
||||||
|
## Credits
|
||||||
|
|
||||||
|
* [Fatih Arslan](https://github.com/fatih)
|
||||||
|
|
||||||
|
## License
|
||||||
|
|
||||||
|
The MIT License (MIT) - see LICENSE.md for more details
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,31 +1,37 @@
|
|||||||
package color
|
package color
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bufio"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"testing"
|
"testing"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestColor(t *testing.T) {
|
func TestColor(t *testing.T) {
|
||||||
go func() {
|
Cyan.Print("Prints text in cyan.")
|
||||||
scanner := bufio.NewScanner(os.Stdout)
|
Blue.Print("Prints text in blue.")
|
||||||
for scanner.Scan() {
|
|
||||||
fmt.Println(scanner.Text()) // Println will add back the final '\n'
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
|
|
||||||
time.Sleep(time.Millisecond * 300)
|
// Chain SGR paramaters
|
||||||
|
Green.Add(Bold).Println("Green with bold")
|
||||||
|
Red.Add(BgWhite, Underline).Printf("Red with White background and underscore: %s\n", "format too!")
|
||||||
|
|
||||||
c := Cyan.Bold()
|
// Create and reuse color objects
|
||||||
c.Println("ankara")
|
c := Cyan.Add(Underline)
|
||||||
|
c.Println("Prints bold cyan.")
|
||||||
|
c.Printf("Thir prints bold cyan %s\n", "too!.")
|
||||||
|
|
||||||
// New(FgGreen, Bold).Begin()
|
// Create custom color objects:
|
||||||
// fmt.Println("san francisco")
|
d := New(FgGreen, BgCyan, Italic)
|
||||||
// End()
|
d.Print("Italic green with cyan backround")
|
||||||
|
|
||||||
|
// You can use set custom objects too
|
||||||
|
Cyan.Set()
|
||||||
|
fmt.Println("Existing text in your codebase will be now in Cyan")
|
||||||
|
fmt.Printf("This one %s\n", "too")
|
||||||
|
Unset() // don't forget to unset
|
||||||
|
|
||||||
|
// You can use set custom objects too
|
||||||
|
New(FgBlack, BgWhite, Bold).Set()
|
||||||
|
defer Unset() // use it in your function
|
||||||
|
|
||||||
|
fmt.Println("All text will be now bold red with white background.")
|
||||||
|
|
||||||
// Output:
|
|
||||||
// ankara
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user