From 68d3af261f6135571b209de4f315af5fbc0e74ec Mon Sep 17 00:00:00 2001 From: evilsocket Date: Thu, 8 Feb 2018 07:36:48 +0100 Subject: [PATCH] fix: checking whether colors are available in the current terminal --- core/swag.go | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/core/swag.go b/core/swag.go index e1ecbc4c..4ba00a39 100644 --- a/core/swag.go +++ b/core/swag.go @@ -1,7 +1,12 @@ package core +import ( + "github.com/mattn/go-isatty" + "os" +) + // https://misc.flogisoft.com/bash/tip_colors_and_formatting -const ( +var ( BOLD = "\033[1m" DIM = "\033[2m" @@ -20,10 +25,30 @@ const ( BG_LBLUE = "\033[104m" RESET = "\033[0m" + + NoColors = false ) -const ON = GREEN + "✔" + RESET -const OFF = RED + "✘" + RESET +func init() { + NoColors := os.Getenv("TERM") == "dumb" || + (!isatty.IsTerminal(os.Stdout.Fd()) && !isatty.IsCygwinTerminal(os.Stdout.Fd())) + if NoColors { + BOLD = "" + DIM = "" + RED = "" + GREEN = "" + BLUE = "" + YELLOW = "" + FG_BLACK = "" + FG_WHITE = "" + BG_DGRAY = "" + BG_RED = "" + BG_GREEN = "" + BG_YELLOW = "" + BG_LBLUE = "" + RESET = "" + } +} const ( DEBUG = iota