ResourcePrinter (templates): Use options to pass color_mode
authorMikael Berthe <mikael@lilotux.net>
Sun, 07 May 2017 17:32:33 +0200
changeset 89 3758bb5f0979
parent 88 028f7b02bd3e
child 90 b1da6dc9f689
ResourcePrinter (templates): Use options to pass color_mode
cmd/utils.go
printer/templateprinter.go
printer/themeprinter.go
--- a/cmd/utils.go	Sun May 07 17:15:00 2017 +0200
+++ b/cmd/utils.go	Sun May 07 17:32:33 2017 +0200
@@ -55,9 +55,11 @@
 	// Initialize color mode
 	switch viper.GetString("color") {
 	case "on", "yes", "force":
-		printer.ColorMode = 1
+		opt["color_mode"] = "on"
 	case "off", "no":
-		printer.ColorMode = 2
+		opt["color_mode"] = "off"
+	default:
+		opt["color_mode"] = "auto"
 	}
 
 	if of == "theme" {
--- a/printer/templateprinter.go	Sun May 07 17:15:00 2017 +0200
+++ b/printer/templateprinter.go	Sun May 07 17:32:33 2017 +0200
@@ -20,9 +20,6 @@
 	"github.com/McKael/madonctl/printer/colors"
 )
 
-// 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
 
@@ -34,6 +31,8 @@
 
 // NewPrinterTemplate returns a Template ResourcePrinter
 // For TemplatePrinter, the options parameter contains the template string.
+// The "color_mode" option defines the color behaviour: it can be
+// "auto" (default), "on" (forced), "off" (disabled).
 func NewPrinterTemplate(options Options) (*TemplatePrinter, error) {
 	tmpl := options["template"]
 	if tmpl == "" {
@@ -50,7 +49,8 @@
 
 	// Update disableColors.
 	// In auto-mode, check if stdout is a TTY.
-	if ColorMode == 2 || (ColorMode == 0 && !isatty.IsTerminal(os.Stdout.Fd())) {
+	colorMode := options["color_mode"]
+	if colorMode == "off" || (colorMode != "on" && !isatty.IsTerminal(os.Stdout.Fd())) {
 		disableColors = true
 	}
 
--- a/printer/themeprinter.go	Sun May 07 17:15:00 2017 +0200
+++ b/printer/themeprinter.go	Sun May 07 17:32:33 2017 +0200
@@ -25,12 +25,15 @@
 type ThemePrinter struct {
 	name        string
 	templateDir string
+	colorMode   string
 }
 
 // NewPrinterTheme returns a Theme ResourcePrinter
 // For ThemePrinter, the options parameter contains the name of the theme
 // and the template base directory (themes are assumed to be in the "themes"
 // subdirectory).
+// The "color_mode" option defines the color behaviour: it can be
+// "auto" (default), "on" (forced), "off" (disabled).
 func NewPrinterTheme(options Options) (*ThemePrinter, error) {
 	name, ok := options["name"]
 	if !ok || name == "" {
@@ -43,6 +46,7 @@
 	return &ThemePrinter{
 		name:        name,
 		templateDir: options["template_directory"],
+		colorMode:   options["color_mode"],
 	}, nil
 }
 
@@ -107,7 +111,11 @@
 			if err != nil {
 				return errors.Wrap(err, "cannot read template")
 			}
-			np, err := NewPrinter("template", Options{"template": string(t)})
+			o := Options{
+				"template":   string(t),
+				"color_mode": p.colorMode,
+			}
+			np, err := NewPrinter("template", o)
 			if err != nil {
 				return errors.Wrap(err, "cannot create template printer")
 			}