github.com/asifdxtreme/cli@v6.1.3-0.20150123051144-9ead8700b4ae+incompatible/cf/terminal/color.go (about) 1 package terminal 2 3 import ( 4 "fmt" 5 "os" 6 "regexp" 7 "runtime" 8 9 "code.google.com/p/go.crypto/ssh/terminal" 10 ) 11 12 type Color uint 13 14 const ( 15 red Color = 31 16 green = 32 17 yellow = 33 18 // blue = 34 19 magenta = 35 20 cyan = 36 21 grey = 37 22 white = 38 23 ) 24 25 var ( 26 colorize func(message string, color Color, bold int) string 27 OsSupportsColors = runtime.GOOS != "windows" 28 TerminalSupportsColors = isTerminal() 29 UserAskedForColors = "" 30 ) 31 32 func init() { 33 InitColorSupport() 34 } 35 36 func InitColorSupport() { 37 if colorsEnabled() { 38 colorize = func(message string, color Color, bold int) string { 39 return fmt.Sprintf("\033[%d;%dm%s\033[0m", bold, color, message) 40 } 41 } else { 42 colorize = func(message string, _ Color, _ int) string { 43 return message 44 } 45 } 46 } 47 48 func colorsEnabled() bool { 49 return userDidNotDisableColor() && 50 (userEnabledColors() || (TerminalSupportsColors && OsSupportsColors)) 51 } 52 53 func userEnabledColors() bool { 54 return UserAskedForColors == "true" || os.Getenv("CF_COLOR") == "true" 55 } 56 57 func userDidNotDisableColor() bool { 58 return os.Getenv("CF_COLOR") != "false" && (UserAskedForColors != "false" || os.Getenv("CF_COLOR") == "true") 59 } 60 61 func Colorize(message string, color Color) string { 62 return colorize(message, color, 0) 63 } 64 65 func ColorizeBold(message string, color Color) string { 66 return colorize(message, color, 1) 67 } 68 69 var decolorizerRegex = regexp.MustCompile(`\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]`) 70 71 func Decolorize(message string) string { 72 return string(decolorizerRegex.ReplaceAll([]byte(message), []byte(""))) 73 } 74 75 func HeaderColor(message string) string { 76 return ColorizeBold(message, white) 77 } 78 79 func CommandColor(message string) string { 80 return ColorizeBold(message, yellow) 81 } 82 83 func StoppedColor(message string) string { 84 return ColorizeBold(message, grey) 85 } 86 87 func AdvisoryColor(message string) string { 88 return ColorizeBold(message, yellow) 89 } 90 91 func CrashedColor(message string) string { 92 return ColorizeBold(message, red) 93 } 94 95 func FailureColor(message string) string { 96 return ColorizeBold(message, red) 97 } 98 99 func SuccessColor(message string) string { 100 return ColorizeBold(message, green) 101 } 102 103 func EntityNameColor(message string) string { 104 return ColorizeBold(message, cyan) 105 } 106 107 func PromptColor(message string) string { 108 return ColorizeBold(message, cyan) 109 } 110 111 func TableContentHeaderColor(message string) string { 112 return ColorizeBold(message, cyan) 113 } 114 115 func WarningColor(message string) string { 116 return ColorizeBold(message, magenta) 117 } 118 119 func LogStdoutColor(message string) string { 120 return Colorize(message, white) 121 } 122 123 func LogStderrColor(message string) string { 124 return Colorize(message, red) 125 } 126 127 func LogHealthHeaderColor(message string) string { 128 return Colorize(message, grey) 129 } 130 131 func LogAppHeaderColor(message string) string { 132 return ColorizeBold(message, yellow) 133 } 134 135 func LogSysHeaderColor(message string) string { 136 return ColorizeBold(message, cyan) 137 } 138 139 func isTerminal() bool { 140 return terminal.IsTerminal(1) 141 }