github.com/apcera/util@v0.0.0-20180322191801-7a50bc84ee48/str/colors.go (about)

     1  // Copyright 2012 Apcera Inc. All rights reserved.
     2  
     3  package str
     4  
     5  import (
     6  	"os"
     7  )
     8  
     9  var disableColors bool
    10  
    11  func DisableColors() {
    12  	disableColors = true
    13  }
    14  
    15  func ColorForFile(file *os.File, text string, colorIndex string,
    16  	bold bool) string {
    17  	if disableColors || IsTerminal(file) == false {
    18  		return text
    19  	}
    20  	return Color(text, colorIndex, bold)
    21  }
    22  
    23  func Color(text string, colorIndex string, bold bool) string {
    24  	if disableColors {
    25  		return text
    26  	}
    27  	// Two explicit constructions to make it a bit faster
    28  	if bold == false {
    29  		return "\033[" + "38;5;" + colorIndex + "m" + text + "\033[0m"
    30  	}
    31  	return "\033[0;1m\033[" + "38;5;" + colorIndex + "m" + text + "\033[0m"
    32  }