add vendor agtorre/gocolorize

This commit is contained in:
lealife
2017-12-01 10:52:41 +08:00
parent c084792d32
commit 347e79610e
9 changed files with 673 additions and 0 deletions

2
vendor/github.com/agtorre/gocolorize/.gitignore generated vendored Normal file
View File

@ -0,0 +1,2 @@
*.swp
*.test

22
vendor/github.com/agtorre/gocolorize/LICENSE.txt generated vendored Normal file
View File

@ -0,0 +1,22 @@
# This is the MIT license
# Copyright (c) 2013 Aaron G. Torres All rights reserved.
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.

135
vendor/github.com/agtorre/gocolorize/README.md generated vendored Normal file
View File

@ -0,0 +1,135 @@
#Gocolorize
Gocolorize is a package that allows Go programs to provide ANSI coloring in a stateful manner. Gocolorize is ideal for logging or cli applications.
![colored tests passing](https://raw.github.com/agtorre/gocolorize/master/screenshot/tests.png)
[![wercker status](https://app.wercker.com/status/ee73c1abee9900c475def6ff9a142237/s "wercker status")](https://app.wercker.com/project/bykey/ee73c1abee9900c475def6ff9a142237)
##Features
- Stateful ANSI coloring
- Supports Foreground and background colors
- Supports a nuber of text properties such as bold or underline
- Color multiple arguments
- Color multiple interfaces, including complex types
- Tests with 100% coverage
- Working examples
- Disable ability for portability
##Install Gocolorize
To install:
$ go get github.com/agtorre/gocolorize
##Usage
Ways to initialize a Colorize object:
```go
//It can be done like this
var c gocolorize.Colorize
c.SetFg(gocolorize.Red)
c.SetBg(gocolorize.Black)
//Or this
c := gocolorize.Colorize{Fg: gocolorize.Red, Bg: gocolorize.Black}
//Or this
c := gocolorize.NewColor("red:black")
```
Once you have an object:
```go
//Call Paint to take inputs and return a colored string
c.Paint("This", "accepts", "multiple", "arguments", "and", "types:", 1, 1.25, "etc")
//If you want a short-hand closure
p = c.Paint
p("Neat")
//To print it:
Fmt.Println(p("test"))
//It can also be appended to other strings, used in logging to stdout, etc.
a := "test " + p("case")
//The closure allows you to reuse the original object, for example
p = c.Paint
c.SetFg(gocolorize.Green)
p2 = c.Paint
Fmt.Println(p("different" + " " + p2("colors")))
```
Object Properties:
```go
//These will only apply if there is a Fg and Bg respectively
c.ToggleFgIntensity()
c.ToggleBgIntensity()
//Set additional attributes
c.ToggleBold()
c.ToggleBlink()
c.ToggleUnderLine()
c.ToggleInverse()
//To disable or renable everything color (for example on Windows)
//the other functions will still work, they'll just return plain
//text for portability
gocolorize.SetPlain(true)
```
##NewColor String Format
```go
"foregroundColor+attributes:backgroundColor+attributes"
```
Colors:
* black
* red
* green
* yellow
* blue
* magenta
* cyan
* white
Attributes:
* b = bold foreground
* B = blink foreground
* u = underline foreground
* h = high intensity (bright) foreground, background
* i = inverse
##Examples
See examples directory for examples:
$ cd examples/
$ go run song.go
$ go run logging.go
##Tests
Tests are another good place to see examples. In order to run tests:
$ go test -cover
##Portability
ANSI coloring will not work in default Windows environments and may not work in other environments correctly. In order to allow compatibility with these environments, you can call:
```go
gocolorize.SetPlain(true)
```
Once toggled, the library will still function, but it will not color the output.
## References
Wikipedia ANSI escape codes [Colors](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors)
[A stylesheet author's guide to terminal colors](http://wynnnetherland.com/journal/a-stylesheet-author-s-guide-to-terminal-colors)
## Special Thanks
https://github.com/mgutz/ansi was an inspiration for the latest version. I did a lot of rewriting, removed the 'paints' module, and did a lot of general cleanup. I learned a lot reading this and using this code.

View File

@ -0,0 +1,50 @@
// Copyright (c) 2013 Aaron Torres. All rights reserved.
package main
import (
"github.com/agtorre/gocolorize"
"log"
"os"
)
var (
INFO *log.Logger
WARNING *log.Logger
CRITICAL *log.Logger
)
type MyError struct {
What string
}
func main() {
//Revel Example
//first set some color information
info := gocolorize.NewColor("green")
warning := gocolorize.NewColor("yellow")
critical := gocolorize.NewColor("black+u:red")
//We could also do this
//critical.ToggleUnderline()
//helper functions to shorten code
i := info.Paint
w := warning.Paint
c := critical.Paint
//Define the look/feel of the INFO logger
INFO = log.New(os.Stdout, i("INFO "), log.Ldate|log.Lmicroseconds|log.Lshortfile)
WARNING = log.New(os.Stdout, w("WARNING "), log.Ldate|log.Lmicroseconds|log.Lshortfile)
CRITICAL = log.New(os.Stdout, c("CRITICAL")+" ", log.Ldate|log.Lmicroseconds|log.Lshortfile)
//print out some messages, note the i wrappers for yellow text on the actual info string
INFO.Println(i("Loaded module x"))
INFO.Println(i("Loaded module y"))
WARNING.Println(w("Failed to load module z"))
e := MyError{What: "Failed"}
CRITICAL.Println(c("Failed with an error code:", e))
}

View File

@ -0,0 +1,44 @@
// Copyright (c) 2013 Aaron Torres. All rights reserved.
package main
import (
"fmt"
"github.com/agtorre/gocolorize"
)
func main() {
// one way to define a stateful colorizer
var green gocolorize.Colorize
green.SetColor(gocolorize.Green)
g := green.Paint
// Another way to do it
red := gocolorize.Colorize{Fg: gocolorize.Red}
r := red.Paint
// now with string construction
green_black := gocolorize.Colorize{Fg: gocolorize.Blue}
// toggle attributes
green_black.ToggleUnderline()
b := green_black.Paint
//all in 1 line
c := gocolorize.NewColor("yellow:black").Paint
fmt.Println(b("On the twelfth day of Christmas"))
fmt.Println(b("my true love sent to me:"))
fmt.Println(g("Twelve"), c("Drummers"), r("Drumming"))
fmt.Println(g("Eleven"), c("Pipers"), r("Piping"))
fmt.Println(g("Ten"), c("Lords"), r("a Leaping"))
fmt.Println(g("Nine"), c("Ladies"), r("Dancing"))
fmt.Println(g("Eight"), c("Maids"), r("a Milking"))
fmt.Println(g("Seven"), c("Swans"), r("a Swimming"))
fmt.Println(g("Six"), c("Geese"), r("a Laying"))
fmt.Println(g("Five"), c("Golden"), r("Rings"))
fmt.Println(g("Four"), c("Calling"), r("Birds"))
fmt.Println(g("Three"), c("French"), r("Hens"))
fmt.Println(g("Two"), c("Turtle"), r("Doves"))
fmt.Println(b("and a Partridge in a Pear Tree"))
}

233
vendor/github.com/agtorre/gocolorize/gocolorize.go generated vendored Normal file
View File

@ -0,0 +1,233 @@
package gocolorize
import (
"fmt"
"strings"
)
//internal usage
var plain = false
const (
start = "\033["
reset = "\033[0m"
bold = "1;"
blink = "5;"
underline = "4;"
inverse = "7;"
normalIntensityFg = 30
highIntensityFg = 90
normalIntensityBg = 40
highIntensityBg = 100
)
//The rest is external facing
//Color can be used for Fg and Bg
type Color int
const (
ColorNone = iota
//placeholder here so we don't confuse with ColorNone
Red
Green
Yellow
Blue
Magenta
Cyan
White
Black Color = -1
)
var colors = map[string]Color{
"red": Red,
"green": Green,
"yellow": Yellow,
"blue": Blue,
"magenta": Magenta,
"cyan": Cyan,
"white": White,
"black": Black,
}
//Can set 1 or more of these properties
//This struct holds the state
type Property struct {
Bold bool
Blink bool
Underline bool
Inverse bool
Fgi bool
Bgi bool
}
//Where the magic happens
type Colorize struct {
Value []interface{}
Fg Color
Bg Color
Prop Property
}
//returns a value you can stick into print, of type string
func (c Colorize) Paint(v ...interface{}) string {
c.Value = v
return fmt.Sprint(c)
}
func propsString(p Property) string {
var result string
if p.Bold {
result += bold
}
if p.Blink {
result += blink
}
if p.Underline {
result += underline
}
if p.Inverse {
result += inverse
}
return result
}
// Format allows ColorText to satisfy the fmt.Formatter interface. The format
// behaviour is the same as for fmt.Print.
func (ct Colorize) Format(fs fmt.State, c rune) {
var base int
//fmt.Println(ct.Fg, ct.Fgi, ct.Bg, ct.Bgi, ct.Prop)
//First Handle the Fg styles and options
if ct.Fg != ColorNone && !plain {
if ct.Prop.Fgi {
base = int(highIntensityFg)
} else {
base = int(normalIntensityFg)
}
if ct.Fg == Black {
base = base
} else {
base = base + int(ct.Fg)
}
fmt.Fprint(fs, start, "0;", propsString(ct.Prop), base, "m")
}
//Next Handle the Bg styles and options
if ct.Bg != ColorNone && !plain {
if ct.Prop.Bgi {
base = int(highIntensityBg)
} else {
base = int(normalIntensityBg)
}
if ct.Bg == Black {
base = base
} else {
base = base + int(ct.Bg)
}
//We still want to honor props if only the background is set
if ct.Fg == ColorNone {
fmt.Fprint(fs, start, propsString(ct.Prop), base, "m")
//fmt.Fprint(fs, start, base, "m")
} else {
fmt.Fprint(fs, start, base, "m")
}
}
// I simplified this to be a bit less efficient,
// but more robust, it will work with anything that
// printf("%v") will support
for i, v := range ct.Value {
fmt.Fprintf(fs, fmt.Sprint(v))
if i < len(ct.Value)-1 {
fmt.Fprintf(fs, " ")
}
}
//after we finish go back to a clean state
if !plain {
fmt.Fprint(fs, reset)
}
}
func NewColor(style string) Colorize {
//Thank you https://github.com/mgutz/ansi for
//this code example and for a bunch of other ideas
foreground_background := strings.Split(style, ":")
foreground := strings.Split(foreground_background[0], "+")
fg := colors[foreground[0]]
fgStyle := ""
if len(foreground) > 1 {
fgStyle = foreground[1]
}
var bg Color
bgStyle := ""
if len(foreground_background) > 1 {
background := strings.Split(foreground_background[1], "+")
bg = colors[background[0]]
if len(background) > 1 {
bgStyle = background[1]
}
}
c := Colorize{Fg: fg, Bg: bg}
if len(fgStyle) > 0 {
if strings.Contains(fgStyle, "b") {
c.ToggleBold()
}
if strings.Contains(fgStyle, "B") {
c.ToggleBlink()
}
if strings.Contains(fgStyle, "u") {
c.ToggleUnderline()
}
if strings.Contains(fgStyle, "i") {
c.ToggleInverse()
}
if strings.Contains(fgStyle, "h") {
c.ToggleFgIntensity()
}
}
if len(bgStyle) > 0 {
if strings.Contains(bgStyle, "h") {
c.ToggleBgIntensity()
}
}
return c
}
func (C *Colorize) SetColor(c Color) {
C.Fg = c
}
func (C *Colorize) SetBgColor(b Color) {
C.Bg = b
}
func (C *Colorize) ToggleFgIntensity() {
C.Prop.Fgi = !C.Prop.Fgi
}
func (C *Colorize) ToggleBgIntensity() {
C.Prop.Bgi = !C.Prop.Bgi
}
func (C *Colorize) ToggleBold() {
C.Prop.Bold = !C.Prop.Bold
}
func (C *Colorize) ToggleBlink() {
C.Prop.Blink = !C.Prop.Blink
}
func (C *Colorize) ToggleUnderline() {
C.Prop.Underline = !C.Prop.Underline
}
func (C *Colorize) ToggleInverse() {
C.Prop.Inverse = !C.Prop.Inverse
}
func SetPlain(p bool) {
plain = p
}

141
vendor/github.com/agtorre/gocolorize/gocolorize_test.go generated vendored Normal file
View File

@ -0,0 +1,141 @@
package gocolorize
import (
"fmt"
"log"
"testing"
)
var (
DEBUG *log.Logger
WARN *log.Logger
)
func TestPaint(t *testing.T) {
var blue Colorize
//set some state
blue.SetColor(Blue)
outString := fmt.Sprintf("Testing %s", blue.Paint("paint"))
basisString := "Testing \033[0;34mpaint\033[0m"
if outString != basisString {
t.Errorf("Error: string '%s' does not match '%s'\n", outString, basisString)
} else {
fmt.Printf("Success: string: '%s' matches '%s'\n", outString, basisString)
}
}
func TestPaintString(t *testing.T) {
var red Colorize
//set some state
red.SetColor(Red)
outString := red.Paint("Returning a string")
basisString := "\033[0;31mReturning a string\033[0m"
if outString != basisString {
t.Errorf("Error: string '%s' does not match '%s'\n", outString, basisString)
} else {
fmt.Printf("Success: string: '%s' matches '%s'\n", outString, basisString)
}
}
func TestSetColorSetBgColor(t *testing.T) {
var whiteRedBg Colorize
//set color and background
whiteRedBg.SetColor(White)
whiteRedBg.SetBgColor(Red)
outString := whiteRedBg.Paint("Setting a foreground and background color!")
basisString := "\033[0;37m\033[41mSetting a foreground and background color!\033[0m"
if outString != basisString {
t.Errorf("Error: string '%s' does not match '%s'\n", outString, basisString)
} else {
fmt.Printf("Success: string: '%s' matches '%s'\n", outString, basisString)
}
}
func TestPaintMultipleInterface(t *testing.T) {
blue := Colorize{Fg: Blue}
outString := blue.Paint("Multiple types of args:", 1, 1.24)
basisString := "\033[0;34mMultiple types of args: 1 1.24\033[0m"
if outString != basisString {
t.Errorf("Error: string '%s' does not match '%s'\n", outString, basisString)
} else {
fmt.Printf("Success: string: '%s' matches '%s'\n", outString, basisString)
}
}
func TestPaintComplexType(t *testing.T) {
green := Colorize{Bg: Green}
outString := green.Paint("Complex types:",
struct {
int
string
}{})
basisString := fmt.Sprintf("\033[42mComplex types: %v\033[0m", struct {
int
string
}{})
if outString != basisString {
t.Errorf("Error: string '%s' does not match '%s'\n", outString, basisString)
} else {
fmt.Printf("Success: string: '%s' matches '%s'\n", outString, basisString)
}
}
func TestInitialize(t *testing.T) {
blackOnWhite := Colorize{Fg: Black, Bg: White}
f := blackOnWhite.Paint
outString := f("Now this is cool")
basisString := "\033[0;30m\033[47mNow this is cool\033[0m"
if outString != basisString {
t.Errorf("Error: string '%s' does not match '%s'\n", outString, basisString)
} else {
fmt.Printf("Success: string: '%s' matches '%s'\n", outString, basisString)
}
}
func TestToggle(t *testing.T) {
craziness := Colorize{Fg: Yellow, Bg: Black}
craziness.ToggleFgIntensity()
craziness.ToggleBgIntensity()
craziness.ToggleBold()
craziness.ToggleBlink()
craziness.ToggleUnderline()
craziness.ToggleInverse()
outString := craziness.Paint("craziness")
basisString := "\033[0;1;5;4;7;93m\033[100mcraziness\033[0m"
if outString != basisString {
t.Errorf("Error: string '%s' does not match '%s'\n", outString, basisString)
} else {
fmt.Printf("Success: string: '%s' matches '%s'\n", outString, basisString)
}
}
func TestNewAllToggle(t *testing.T) {
n := NewColor("yellow+bBuih:black+h")
outString := n.Paint("all toggles in 1 line!")
basisString := "\033[0;1;5;4;7;93m\033[100mall toggles in 1 line!\033[0m"
if outString != basisString {
t.Errorf("Error: string '%s' does not match '%s'\n", outString, basisString)
} else {
fmt.Printf("Success: string: '%s' matches '%s'\n", outString, basisString)
}
}
func TestPlain(t *testing.T) {
plain := Colorize{Fg: Magenta}
SetPlain(true)
outString := plain.Paint("plain", "text")
basisString := "plain text"
if outString != basisString {
t.Errorf("Error: string '%s' does not match '%s'\n", outString, basisString)
} else {
fmt.Printf("Success: string: '%s' matches '%s'\n", outString, basisString)
}
SetPlain(false)
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 19 KiB

46
vendor/github.com/agtorre/gocolorize/wercker.yml generated vendored Normal file
View File

@ -0,0 +1,46 @@
# This references the default golang container from
# the Docker Hub: https://registry.hub.docker.com/u/library/golang/
# If you want Google's container you would reference google/golang
# Read more about containers on our dev center
# http://devcenter.wercker.com/docs/containers/index.html
box: golang
# This is the build pipeline. Pipelines are the core of wercker
# Read more about pipelines on our dev center
# http://devcenter.wercker.com/docs/pipelines/index.html
# You can also use services such as databases. Read more on our dev center:
# http://devcenter.wercker.com/docs/services/index.html
# services:
# - postgres
# http://devcenter.wercker.com/docs/services/postgresql.html
# - mongo
# http://devcenter.wercker.com/docs/services/mongodb.html
build:
# The steps that will be executed on build
# Steps make up the actions in your pipeline
# Read more about steps on our dev center:
# http://devcenter.wercker.com/docs/steps/index.html
steps:
# Sets the go workspace and places you package
# at the right place in the workspace tree
- setup-go-workspace
# Gets the dependencies
- script:
name: go get
code: |
go get
# Build the project
- script:
name: go build
code: |
go build ./...
# Test the project
- script:
name: go test
code: |
go test ./...