--- a/.travis.yml Mon May 01 12:25:43 2017 +0200
+++ b/.travis.yml Mon May 01 12:42:37 2017 +0200
@@ -18,3 +18,4 @@
- go get github.com/pkg/errors
- go get github.com/jaytaylor/html2text
- go get github.com/m0t0k1ch1/gomif
+- go get github.com/mattn/go-isatty
--- a/printer/templateprinter.go Mon May 01 12:25:43 2017 +0200
+++ b/printer/templateprinter.go Mon May 01 12:42:37 2017 +0200
@@ -14,11 +14,15 @@
"text/template"
"github.com/m0t0k1ch1/gomif"
+ "github.com/mattn/go-isatty"
"github.com/McKael/madon"
"github.com/McKael/madonctl/printer/colors"
)
+// DisableColors can be set to true to disable the color template function
+var DisableColors bool
+
// TemplatePrinter represents a Template printer
type TemplatePrinter struct {
rawTemplate string
@@ -32,11 +36,17 @@
t, err := template.New("output").Funcs(template.FuncMap{
"fromhtml": html2string,
"fromunix": unix2string,
- "color": colors.ANSICodeString,
+ "color": ansiColor,
}).Parse(tmpl)
if err != nil {
return nil, err
}
+
+ // Check if stdout is a TTY
+ if !isatty.IsTerminal(os.Stdout.Fd()) {
+ DisableColors = true
+ }
+
return &TemplatePrinter{
rawTemplate: tmpl,
template: t,
@@ -118,3 +128,10 @@
}
return nil
}
+
+func ansiColor(desc string) (string, error) {
+ if DisableColors {
+ return "", nil
+ }
+ return colors.ANSICodeString(desc)
+}