Merge branch 'update-test'
diff --git a/README.md b/README.md
index 5fb3f22..8f8e20b 100644
--- a/README.md
+++ b/README.md
@@ -4,10 +4,10 @@
## Install
-This install the color viewer and the package itself
+Get it
```sh
-go get -u github.com/mgutz/ansi/cmd/ansi-mgutz
+go get -u github.com/mgutz/ansi
```
## Example
@@ -18,7 +18,7 @@
// colorize a string, SLOW
msg := ansi.Color("foo", "red+b:white")
-// create a closure to avoid recalculating ANSI code compilation
+// create a FAST closure function to avoid computation of ANSI code
phosphorize := ansi.ColorFunc("green+h:black")
msg = phosphorize("Bring back the 80s!")
msg2 := phospohorize("Look, I'm a CRT!")
@@ -44,10 +44,10 @@
Color(s, "off") // turn off ansi codes
```
-To view color combinations, from terminal.
+To view color combinations, from project directory in terminal.
```sh
-ansi-mgutz
+go test
```
## Style format
@@ -68,15 +68,18 @@
* white
* 0...255 (256 colors)
-Attributes
+Foreground Attributes
-* b = bold foreground
-* B = Blink foreground
-* u = underline foreground
-* i = inverse
-* h = high intensity (bright) foreground, background
+* B = Blink
+* b = bold
+* h = high intensity (bright)
+* i = inverse
+* s = strikethrough
+* u = underline
- does not work with 256 colors
+Background Attributes
+
+* h = high intensity (bright)
## Constants
@@ -100,7 +103,6 @@
* ansi.LightCyan
* ansi.LightWhite
-
## References
Wikipedia ANSI escape codes [Colors](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors)
diff --git a/ansi.go b/ansi.go
index fbf0271..dc04136 100644
--- a/ansi.go
+++ b/ansi.go
@@ -224,7 +224,7 @@
return buf.String()
}
-// ColorFunc creates a closureto avoid ANSI color code calculation.
+// ColorFunc creates a closure to avoid computation ANSI color code.
func ColorFunc(style string) func(string) string {
if style == "" {
return func(s string) string {
@@ -244,7 +244,42 @@
}
}
-// DisableColors disables ANSI color codes. On by default.
+// DisableColors disables ANSI color codes. The default is false (colors are on).
func DisableColors(disable bool) {
plain = disable
+ if plain {
+ Black = ""
+ Red = ""
+ Green = ""
+ Yellow = ""
+ Blue = ""
+ Magenta = ""
+ Cyan = ""
+ White = ""
+ LightBlack = ""
+ LightRed = ""
+ LightGreen = ""
+ LightYellow = ""
+ LightBlue = ""
+ LightMagenta = ""
+ LightCyan = ""
+ LightWhite = ""
+ } else {
+ Black = ColorCode("black")
+ Red = ColorCode("red")
+ Green = ColorCode("green")
+ Yellow = ColorCode("yellow")
+ Blue = ColorCode("blue")
+ Magenta = ColorCode("magenta")
+ Cyan = ColorCode("cyan")
+ White = ColorCode("white")
+ LightBlack = ColorCode("black+h")
+ LightRed = ColorCode("red+h")
+ LightGreen = ColorCode("green+h")
+ LightYellow = ColorCode("yellow+h")
+ LightBlue = ColorCode("blue+h")
+ LightMagenta = ColorCode("magenta+h")
+ LightCyan = ColorCode("cyan+h")
+ LightWhite = ColorCode("white+h")
+ }
}
diff --git a/ansi_test.go b/ansi_test.go
index 1cf70a2..1630dc5 100644
--- a/ansi_test.go
+++ b/ansi_test.go
@@ -1,42 +1,52 @@
package ansi
import (
- "fmt"
+ "strings"
"testing"
)
func TestPlain(t *testing.T) {
DisableColors(true)
- bgColors := []string{
- "",
- ":black",
- ":red",
- ":green",
- ":yellow",
- ":blue",
- ":magenta",
- ":cyan",
- ":white",
- }
- for fg := range Colors {
- for _, bg := range bgColors {
- println(padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg}))
- println(padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h"}))
- println(padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h", "+s" + bg}))
- }
- }
+ PrintStyles()
}
func TestStyles(t *testing.T) {
- PrintStyles()
DisableColors(false)
+ PrintStyles()
+}
+
+func TestDisableColors(t *testing.T) {
+ fn := ColorFunc("red")
+
buf := colorCode("off")
if buf.String() != "" {
t.Fail()
}
-}
-func ExampleColorFunc() {
- brightGreen := ColorFunc("green+h")
- fmt.Println(brightGreen("lime"))
+ DisableColors(true)
+ if Black != "" {
+ t.Fail()
+ }
+ code := ColorCode("red")
+ if code != "" {
+ t.Fail()
+ }
+ s := fn("foo")
+ if s != "foo" {
+ t.Fail()
+ }
+
+ DisableColors(false)
+ if Black == "" {
+ t.Fail()
+ }
+ code = ColorCode("red")
+ if code == "" {
+ t.Fail()
+ }
+ // will have escape codes around it
+ index := strings.Index(fn("foo"), "foo")
+ if index <= 0 {
+ t.Fail()
+ }
}
diff --git a/print.go b/print.go
index 044c9a6..806f436 100644
--- a/print.go
+++ b/print.go
@@ -1,9 +1,16 @@
package ansi
+import (
+ "fmt"
+ "sort"
+
+ colorable "github.com/mattn/go-colorable"
+)
+
// PrintStyles prints all style combinations to the terminal.
func PrintStyles() {
- oldPlain := plain
- plain = false
+ // for compatibility with Windows, not needed for *nix
+ stdout := colorable.NewColorableStdout()
bgColors := []string{
"",
@@ -16,14 +23,22 @@
":cyan",
":white",
}
- for fg := range Colors {
+
+ keys := make([]string, 0, len(Colors))
+ for k := range Colors {
+ keys = append(keys, k)
+ }
+
+ sort.Sort(sort.StringSlice(keys))
+
+ for _, fg := range keys {
for _, bg := range bgColors {
- println(padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg}))
- println(padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h"}))
- println(padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h"}))
+ fmt.Fprintln(stdout, padColor(fg, []string{"" + bg, "+b" + bg, "+bh" + bg, "+u" + bg}))
+ fmt.Fprintln(stdout, padColor(fg, []string{"+s" + bg, "+i" + bg}))
+ fmt.Fprintln(stdout, padColor(fg, []string{"+uh" + bg, "+B" + bg, "+Bb" + bg /* backgrounds */, "" + bg + "+h"}))
+ fmt.Fprintln(stdout, padColor(fg, []string{"+b" + bg + "+h", "+bh" + bg + "+h", "+u" + bg + "+h", "+uh" + bg + "+h"}))
}
}
- plain = oldPlain
}
func pad(s string, length int) string {
@@ -33,10 +48,10 @@
return s
}
-func padColor(s string, styles []string) string {
+func padColor(color string, styles []string) string {
buffer := ""
for _, style := range styles {
- buffer += Color(pad(s+style, 20), s+style)
+ buffer += Color(pad(color+style, 20), color+style)
}
return buffer
}