diff --git a/log.go b/log.go index 3658c67d0..fa4bb4c8c 100644 --- a/log.go +++ b/log.go @@ -3,15 +3,18 @@ package main import ( "fmt" "log" + "os" ) const ( - debugLogFormat = "DBG-direnv: %s" - defaultLogFormat = "direnv: %s" - errorLogFormat = "\033[31mdirenv: %s\033[0m" + debugLogFormat = "DBG-direnv: %s" + defaultLogFormat = "direnv: %s" + errorLogFormat = defaultLogFormat + errorLogFormatWithColor = "\033[31mdirenv: %s\033[0m" ) var debugging bool +var noColor = os.Getenv("TERM") == "dumb" func setupLogging(env Env) { log.SetFlags(0) @@ -24,7 +27,11 @@ func setupLogging(env Env) { } func log_error(msg string, a ...interface{}) { - logMsg(errorLogFormat, msg, a...) + if noColor { + logMsg(errorLogFormat, msg, a...) + } else { + logMsg(errorLogFormatWithColor, msg, a...) + } } func log_status(env Env, msg string, a ...interface{}) { diff --git a/stdlib.go b/stdlib.go index 56b757a87..56932d0d4 100644 --- a/stdlib.go +++ b/stdlib.go @@ -43,7 +43,7 @@ const STDLIB = "#!bash\n" + " local color_normal\n" + " local color_error\n" + " color_normal=$(tput sgr0)\n" + - " color_error=\"\\e[0;31m\"\n" + + " color_error=$(tput setaf 1)\n" + " if [[ -n $DIRENV_LOG_FORMAT ]]; then\n" + " local msg=$*\n" + " # shellcheck disable=SC2059\n" + diff --git a/stdlib.sh b/stdlib.sh index e4eb8987d..8ea6f10f7 100644 --- a/stdlib.sh +++ b/stdlib.sh @@ -41,7 +41,7 @@ log_error() { local color_normal local color_error color_normal=$(tput sgr0) - color_error="\e[0;31m" + color_error=$(tput setaf 1) if [[ -n $DIRENV_LOG_FORMAT ]]; then local msg=$* # shellcheck disable=SC2059