github.com/cloudfoundry-attic/ltc@v0.0.0-20151123212628-098adc7919fc/terminal/colors/colorize.go (about)

     1  package colors
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  )
     7  
     8  //TODO: remove Color prefix from color constants
     9  const (
    10  	// ColorRed string = "\x1b[91m"
    11  	ColorRed     string = "\x1b[31m"
    12  	ColorCyan    string = "\x1b[36m"
    13  	ColorGreen   string = "\x1b[32m"
    14  	ColorYellow  string = "\x1b[33m"
    15  	ColorDefault string = "\x1b[0m"
    16  	ColorBold    string = "\x1b[1m"
    17  	ColorGray    string = "\x1b[90m"
    18  	ColorBlue    string = "\x1b[34m"
    19  	ColorPurple  string = "\x1b[35m"
    20  )
    21  
    22  func Colorize(colorCode string, format string, args ...interface{}) string {
    23  	var out string
    24  
    25  	if len(args) > 0 {
    26  		out = fmt.Sprintf(format, args...)
    27  	} else {
    28  		out = format
    29  	}
    30  
    31  	if os.Getenv("TERM") == "" {
    32  		return out
    33  	} else {
    34  		return fmt.Sprintf("%s%s%s", colorCode, out, defaultStyle)
    35  	}
    36  }