github.com/devcamcar/cli@v0.0.0-20181107134215-706a05759d18/common/color/color.go (about) 1 package color 2 3 import ( 4 "os" 5 "strings" 6 7 "github.com/mattn/go-isatty" 8 ) 9 10 var useColors bool 11 12 var Colors map[string]interface{} 13 14 func init() { 15 useColors = isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) 16 Colors = map[string]interface{}{ 17 "bold": Bold, 18 "italic": Italic, 19 "join": strings.Join, 20 "trim": strings.TrimLeft, 21 "cyan": Cyan, 22 "brightcyan": BrightCyan, 23 "boldcyan": BoldCyan, 24 "yellow": Yellow, 25 "red": Red, 26 "brightred": BrightRed, 27 "boldred": BoldRed, 28 "underlinebrightred": UnderlineBrightRed, 29 } 30 } 31 32 func Bold(text string) string { 33 if useColors { 34 return "\x1b[1m" + text + "\x1b[0m" 35 } else { 36 return text 37 } 38 } 39 40 func Italic(text string) string { 41 if useColors { 42 return "\x1b[3m" + text + "\x1b[0m" 43 } else { 44 return text 45 } 46 } 47 48 func BoldRed(text string) string { 49 if useColors { 50 return "\x1b[31;1m" + text + "\x1b[0m" 51 } else { 52 return text 53 } 54 } 55 56 func BrightRed(text string) string { 57 if useColors { 58 return "\x1b[91;21m" + text + "\x1b[0m" 59 } else { 60 return text 61 } 62 } 63 64 func Red(text string) string { 65 if useColors { 66 return "\x1b[31;21m" + text + "\x1b[0m" 67 } else { 68 return text 69 } 70 } 71 72 func UnderlineBrightRed(text string) string { 73 if useColors { 74 return "\x1b[91;4m" + text + "\x1b[0m" 75 } else { 76 return text 77 } 78 } 79 80 func BrightCyan(text string) string { 81 if useColors { 82 return "\x1b[96;21m" + text + "\x1b[0m" 83 } else { 84 return text 85 } 86 } 87 88 func Cyan(text string) string { 89 if useColors { 90 return "\x1b[36;21m" + text + "\x1b[0m" 91 } else { 92 return text 93 } 94 } 95 96 func BoldCyan(text string) string { 97 if useColors { 98 return "\x1b[36;1m" + text + "\x1b[0m" 99 } else { 100 return text 101 } 102 } 103 104 func Yellow(text string) string { 105 if useColors { 106 return "\x1b[33;21m" + text + "\x1b[0m" 107 } else { 108 return text 109 } 110 }