Repository: raviqqe/muffet (go)

Issue:
Feature Request: Colors
Hello.

Would it be possible to implement parameter which will disable/enable colors in muffet ?

Something like:

```
-C, --color <auto | always | never>    Use color [default: auto]
```

It will help on some "dumb" terminals to "force" color or just disable it completely.

Pull request:
Color option
Close #61 

Patch:
diff --git a/arguments.go b/arguments.go
index 7a9ac34..8821207 100644
--- a/arguments.go
+++ b/arguments.go
@@ -24,6 +24,7 @@ type arguments struct {
 	Verbose               bool     `short:"v" long:"verbose" description:"Show successful results too"`
 	SkipTLSVerification   bool     `long:"skip-tls-verification" description:"Skip TLS certificate verification"`
 	OnePageOnly           bool     `long:"one-page-only" description:"Only check links found in the given URL"`
+	Color                 color    `long:"color" description:"Color output." choice:"auto" choice:"always" choice:"never" default:"auto"`
 	Help                  bool     `short:"h" long:"help" description:"Show this help"`
 	Version               bool     `long:"version" description:"Show version"`
 	URL                   string
diff --git a/color.go b/color.go
new file mode 100644
index 0000000..dbac5e7
--- /dev/null
+++ b/color.go
@@ -0,0 +1,13 @@
+package main
+
+type color string
+
+const (
+	auto   color = "auto"
+	always color = "always"
+	never  color = "never"
+)
+
+func isColorEnabled(c color, terminal bool) bool {
+	return c == always || terminal && c == auto
+}
diff --git a/command.go b/command.go
index 5d77204..1278c76 100644
--- a/command.go
+++ b/command.go
@@ -106,7 +106,11 @@ func (c *command) runWithError(ss []string) (bool, error) {
 		return c.printResultsInJSON(checker.Results())
 	}
 
-	formatter := newPageResultFormatter(args.Verbose, c.terminal)
+	formatter := newPageResultFormatter(
+		args.Verbose,
+		isColorEnabled(args.Color, c.terminal),
+	)
+
 	ok := true
 
 	for r := range checker.Results() {
@@ -153,6 +157,7 @@ func (c *command) print(xs ...interface{}) {
 func (c *command) printError(xs ...interface{}) {
 	s := fmt.Sprint(xs...)
 
+	// Do not check --color option here because this can be used on argument parsing errors.
 	if c.terminal {
 		s = aurora.Red(s).String()
 	}
diff --git a/configuration.go b/configuration.go
index 289141b..925243c 100644
--- a/configuration.go
+++ b/configuration.go
@@ -3,7 +3,7 @@ package main
 import "time"
 
 const (
-	version     = "2.1.0"
+	version     = "2.2.0"
 	agentName   = "muffet"
 	concurrency = 1024
 	tcpTimeout  = 5 * time.Second


Test patch:
diff --git a/.snapshots/TestHelp b/.snapshots/TestHelp
index 53b84c6..593e202 100644
--- a/.snapshots/TestHelp
+++ b/.snapshots/TestHelp
@@ -25,6 +25,7 @@ Application Options:
       --skip-tls-verification               Skip TLS certificate verification
       --one-page-only                       Only check links found in the given
                                             URL
+      --color=[auto|always|never]           Color output. (default: auto)
   -h, --help                                Show this help
       --version                             Show version
 
diff --git a/color_test.go b/color_test.go
new file mode 100644
index 0000000..c7c9e12
--- /dev/null
+++ b/color_test.go
@@ -0,0 +1,16 @@
+package main
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestIsColorEnable(t *testing.T) {
+	assert.True(t, isColorEnabled(auto, true))
+	assert.False(t, isColorEnabled(auto, false))
+	assert.True(t, isColorEnabled(always, true))
+	assert.True(t, isColorEnabled(always, false))
+	assert.False(t, isColorEnabled(never, true))
+	assert.False(t, isColorEnabled(never, false))
+}