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

     1  package colors
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"strings"
     7  )
     8  
     9  var ColorCodeLength = len(red) + len(defaultStyle)
    10  
    11  const (
    12  	red             string = "\x1b[91m"
    13  	cyan            string = "\x1b[36m"
    14  	green           string = "\x1b[32m"
    15  	yellow          string = "\x1b[33m"
    16  	purpleUnderline string = "\x1b[35;4m"
    17  	defaultStyle    string = "\x1b[0m"
    18  	boldStyle       string = "\x1b[1m"
    19  	grayColor       string = "\x1b[90m"
    20  )
    21  
    22  func Red(output string) string {
    23  	return colorText(output, red)
    24  }
    25  
    26  func Green(output string) string {
    27  	return colorText(output, green)
    28  }
    29  
    30  func Cyan(output string) string {
    31  	return colorText(output, cyan)
    32  }
    33  
    34  func Yellow(output string) string {
    35  	return colorText(output, yellow)
    36  }
    37  
    38  func Gray(output string) string {
    39  	return colorText(output, grayColor)
    40  }
    41  
    42  func NoColor(output string) string {
    43  	return colorText(output, defaultStyle)
    44  }
    45  
    46  func Bold(output string) string {
    47  	return colorText(output, boldStyle)
    48  }
    49  
    50  func PurpleUnderline(output string) string {
    51  	return colorText(output, purpleUnderline)
    52  }
    53  
    54  func colorText(output string, color string) string {
    55  	if strings.TrimSpace(output) == "" || os.Getenv("TERM") == "" {
    56  		return output
    57  	} else {
    58  		return fmt.Sprintf("%s%s%s", color, output, defaultStyle)
    59  	}
    60  }