Skip to content

Commit

Permalink
color: add helper function for bool pointer
Browse files Browse the repository at this point in the history
  • Loading branch information
fatih committed Apr 22, 2015
1 parent 85fc70c commit 95e7214
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 38 deletions.
12 changes: 6 additions & 6 deletions color.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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...) }
Expand Down
66 changes: 34 additions & 32 deletions color_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
}

}

0 comments on commit 95e7214

Please sign in to comment.