Skip to content

Commit

Permalink
Only output color if $TERM is not dumb (#264)
Browse files Browse the repository at this point in the history
* Only output color if $TERM is not dumb
Probably should use github.com/fatih/color
Broken, sorry, b/c I don't know how to integrate "go get" in Makefile
I'm not a go programmer. :(
See fatih/color#50
Visit respectmyterm.com to learn why
Thanks!

* remove dependency on isatty
isatty isn't really relevant here. All we really want
is to be able to control the color by setting TERM=dumb
  • Loading branch information
robnagler authored and zimbatm committed Mar 24, 2017
1 parent 8ee08d1 commit bfc910e
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 6 deletions.
15 changes: 11 additions & 4 deletions log.go
Expand Up @@ -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)
Expand All @@ -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{}) {
Expand Down
2 changes: 1 addition & 1 deletion stdlib.go
Expand Up @@ -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" +
Expand Down
2 changes: 1 addition & 1 deletion stdlib.sh
Expand Up @@ -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
Expand Down

0 comments on commit bfc910e

Please sign in to comment.