func TestNewAnsiColor2(t *testing.T) { inner := bytes.NewBufferString("") w1 := ansicolor.NewAnsiColorWriter(inner) w2 := ansicolor.NewAnsiColorWriter(w1) if w1 != w2 { t.Errorf("Get %#v, want %#v", w1, w2) } }
func TestNewAnsiColor1(t *testing.T) { inner := bytes.NewBufferString("") w := ansicolor.NewAnsiColorWriter(inner) if w == inner { t.Errorf("Get %#v, want %#v", w, inner) } }
func TestWritePlanText(t *testing.T) { inner := bytes.NewBufferString("") w := ansicolor.NewAnsiColorWriter(inner) expected := "plain text" fmt.Fprintf(w, expected) actual := inner.String() if actual != expected { t.Errorf("Get %q, want %q", actual, expected) } }
func ExampleNewAnsiColorWriter() { w := ansicolor.NewAnsiColorWriter(os.Stdout) text := "%sforeground %sbold%s %sbackground%s\n" fmt.Fprintf(w, text, "\x1b[31m", "\x1b[1m", "\x1b[21m", "\x1b[41;32m", "\x1b[0m") fmt.Fprintf(w, text, "\x1b[32m", "\x1b[1m", "\x1b[21m", "\x1b[42;31m", "\x1b[0m") fmt.Fprintf(w, text, "\x1b[33m", "\x1b[1m", "\x1b[21m", "\x1b[43;34m", "\x1b[0m") fmt.Fprintf(w, text, "\x1b[34m", "\x1b[1m", "\x1b[21m", "\x1b[44;33m", "\x1b[0m") fmt.Fprintf(w, text, "\x1b[35m", "\x1b[1m", "\x1b[21m", "\x1b[45;36m", "\x1b[0m") fmt.Fprintf(w, text, "\x1b[36m", "\x1b[1m", "\x1b[21m", "\x1b[46;35m", "\x1b[0m") fmt.Fprintf(w, text, "\x1b[37m", "\x1b[1m", "\x1b[21m", "\x1b[47;30m", "\x1b[0m") }
func writeAnsiColor(expectedText, colorCode string) (actualText string, actualAttributes uint16, err error) { inner := bytes.NewBufferString("") w := ansicolor.NewAnsiColorWriter(inner) fmt.Fprintf(w, "\x1b[%sm%s", colorCode, expectedText) actualText = inner.String() screenInfo := GetConsoleScreenBufferInfo(uintptr(syscall.Stdout)) if screenInfo != nil { actualAttributes = screenInfo.WAttributes } else { err = &screenNotFoundError{} } return }
func TestWriteParseText(t *testing.T) { inner := bytes.NewBufferString("") w := ansicolor.NewAnsiColorWriter(inner) inputTail := "\x1b[0mtail text" expectedTail := "tail text" fmt.Fprintf(w, inputTail) actualTail := inner.String() inner.Reset() if actualTail != expectedTail { t.Errorf("Get %q, want %q", actualTail, expectedTail) } inputHead := "head text\x1b[0m" expectedHead := "head text" fmt.Fprintf(w, inputHead) actualHead := inner.String() inner.Reset() if actualHead != expectedHead { t.Errorf("Get %q, want %q", actualHead, expectedHead) } inputBothEnds := "both ends \x1b[0m text" expectedBothEnds := "both ends text" fmt.Fprintf(w, inputBothEnds) actualBothEnds := inner.String() inner.Reset() if actualBothEnds != expectedBothEnds { t.Errorf("Get %q, want %q", actualBothEnds, expectedBothEnds) } inputManyEsc := "\x1b\x1b\x1b\x1b[0m many esc" expectedManyEsc := "\x1b\x1b\x1b many esc" fmt.Fprintf(w, inputManyEsc) actualManyEsc := inner.String() inner.Reset() if actualManyEsc != expectedManyEsc { t.Errorf("Get %q, want %q", actualManyEsc, expectedManyEsc) } expectedSplit := "split text" for _, ch := range "split \x1b[0m text" { fmt.Fprintf(w, string(ch)) } actualSplit := inner.String() inner.Reset() if actualSplit != expectedSplit { t.Errorf("Get %q, want %q", actualSplit, expectedSplit) } }
// Initialization of supported colors func init() { colors = make(map[string]string) colors["black"] = "30m" colors["red"] = "31m" colors["green"] = "32m" colors["yellow"] = "33m" colors["blue"] = "34m" colors["magenta"] = "35m" colors["cyan"] = "36m" colors["white"] = "37m" colors["default"] = "39m" if runtime.GOOS == "windows" { Out = ansicolor.NewAnsiColorWriter(os.Stdout) } else { Out = os.Stdout } }
func main() { w := ansicolor.NewAnsiColorWriter(os.Stdout) io.Copy(w, os.Stdin) }