# HG changeset patch # User Mikael Berthe # Date 1493635357 -7200 # Node ID c6adf9d9e996284f692dfcf047840761bf10d2c6 # Parent d6b4e3b7c6c6113c14d2f49654c60a5bbfc01544 Disable ANSI sequences when stdout is not a TTY diff -r d6b4e3b7c6c6 -r c6adf9d9e996 .travis.yml --- 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 diff -r d6b4e3b7c6c6 -r c6adf9d9e996 printer/templateprinter.go --- 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) +}