printer/templateprinter.go
changeset 51 300ac09051a7
parent 50 c6adf9d9e996
child 83 57afac822019
--- a/printer/templateprinter.go	Mon May 01 12:42:37 2017 +0200
+++ b/printer/templateprinter.go	Mon May 01 13:03:06 2017 +0200
@@ -20,8 +20,11 @@
 	"github.com/McKael/madonctl/printer/colors"
 )
 
-// DisableColors can be set to true to disable the color template function
-var DisableColors bool
+// ColorMode defines the color behaviour: 0=auto, 1=forced, 2=disabled
+var ColorMode int
+
+// disableColors can be set to true to disable the color template function
+var disableColors bool
 
 // TemplatePrinter represents a Template printer
 type TemplatePrinter struct {
@@ -42,9 +45,10 @@
 		return nil, err
 	}
 
-	// Check if stdout is a TTY
-	if !isatty.IsTerminal(os.Stdout.Fd()) {
-		DisableColors = true
+	// Update disableColors.
+	// In auto-mode, check if stdout is a TTY.
+	if ColorMode == 2 || (ColorMode == 0 && !isatty.IsTerminal(os.Stdout.Fd())) {
+		disableColors = true
 	}
 
 	return &TemplatePrinter{
@@ -130,7 +134,7 @@
 }
 
 func ansiColor(desc string) (string, error) {
-	if DisableColors {
+	if disableColors {
 		return "", nil
 	}
 	return colors.ANSICodeString(desc)