From 95e7214071e7143f2fc05c71a3445da707ee492b Mon Sep 17 00:00:00 2001 From: Fatih Arslan Date: Wed, 22 Apr 2015 11:13:04 +0300 Subject: [PATCH] color: add helper function for bool pointer --- color.go | 12 +++++----- color_test.go | 66 ++++++++++++++++++++++++++------------------------- 2 files changed, 40 insertions(+), 38 deletions(-) diff --git a/color.go b/color.go index 35c7f06..c973aa0 100644 --- a/color.go +++ b/color.go @@ -239,17 +239,13 @@ func (c *Color) unformat() string { // code and still being able to output. Can be used for flags like // "--no-color". To enable back use EnableColor() method. func (c *Color) DisableColor() { - t := new(bool) - *t = true - c.noColor = t + c.noColor = boolPtr(true) } // EnableColor enables the color output. Use it in conjuction with // DisableColor(). Otherwise this method has no side effects. func (c *Color) EnableColor() { - t := new(bool) - *t = false - c.noColor = t + c.noColor = boolPtr(false) } func (c *Color) isNoColorSet() bool { @@ -262,6 +258,10 @@ func (c *Color) isNoColorSet() bool { return NoColor } +func boolPtr(v bool) *bool { + return &v +} + // Black is an convenient helper function to print with black foreground. A // newline is appended to format by default. func Black(format string, a ...interface{}) { printColor(format, FgBlack, a...) } diff --git a/color_test.go b/color_test.go index 05440bf..548584e 100644 --- a/color_test.go +++ b/color_test.go @@ -44,7 +44,41 @@ func TestColor(t *testing.T) { t.Errorf("Expecting %s, got '%s'\n", escapedForm, scannedLine) } } +} +func TestNoColor(t *testing.T) { + rb := new(bytes.Buffer) + Output = rb + + testColors := []struct { + text string + code Attribute + }{ + {text: "black", code: FgBlack}, + {text: "red", code: FgRed}, + {text: "green", code: FgGreen}, + {text: "yellow", code: FgYellow}, + {text: "blue", code: FgBlue}, + {text: "magent", code: FgMagenta}, + {text: "cyan", code: FgCyan}, + {text: "white", code: FgWhite}, + } + + for _, c := range testColors { + p := New(c.code) + p.DisableColor() + p.Print(c.text) + + line, _ := rb.ReadString('\n') + + if line != c.text { + t.Errorf("Expecting %s, got '%s'\n", c.text, line) + } + } + +} + +func TestColorVisual(t *testing.T) { // First Visual Test fmt.Println("") Output = ansicolor.NewAnsiColorWriter(os.Stdout) @@ -126,35 +160,3 @@ func TestColor(t *testing.T) { fmt.Fprintln(Output, CyanString("cyan")) fmt.Fprintln(Output, WhiteString("white")) } - -func TestNoColor(t *testing.T) { - rb := new(bytes.Buffer) - Output = rb - - testColors := []struct { - text string - code Attribute - }{ - {text: "black", code: FgBlack}, - {text: "red", code: FgRed}, - {text: "green", code: FgGreen}, - {text: "yellow", code: FgYellow}, - {text: "blue", code: FgBlue}, - {text: "magent", code: FgMagenta}, - {text: "cyan", code: FgCyan}, - {text: "white", code: FgWhite}, - } - - for _, c := range testColors { - p := New(c.code) - p.DisableColor() - p.Print(c.text) - - line, _ := rb.ReadString('\n') - - if line != c.text { - t.Errorf("Expecting %s, got '%s'\n", c.text, line) - } - } - -}