1
0
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:
Fatih Arslan
2014-02-17 01:40:02 -08:00
parent 636dfd47b1
commit 8653b308b1
3 changed files with 110 additions and 18 deletions

20
LICENSE.md Normal file
View 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.

View File

@@ -0,0 +1,66 @@
# Color [![GoDoc](https://godoc.org/github.com/fatih/color?status.png)](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

View File

@@ -1,31 +1,37 @@
package color
import (
"bufio"
"fmt"
"os"
"time"
"testing"
)
func TestColor(t *testing.T) {
go func() {
scanner := bufio.NewScanner(os.Stdout)
for scanner.Scan() {
fmt.Println(scanner.Text()) // Println will add back the final '\n'
}
}()
time.Sleep(time.Millisecond * 300)
c := Cyan.Bold()
c.Println("ankara")
// New(FgGreen, Bold).Begin()
// fmt.Println("san francisco")
// End()
// Output:
// ankara
Cyan.Print("Prints text in cyan.")
Blue.Print("Prints text in blue.")
// 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!")
// Create and reuse color objects
c := Cyan.Add(Underline)
c.Println("Prints bold cyan.")
c.Printf("Thir prints bold cyan %s\n", "too!.")
// Create custom color objects:
d := New(FgGreen, BgCyan, Italic)
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.")
}