github.com/abdfnx/gh-api@v0.0.0-20210414084727-f5432eec23b8/pkg/iostreams/color.go (about) 1 package iostreams 2 3 import ( 4 "fmt" 5 "os" 6 "strings" 7 8 "github.com/mgutz/ansi" 9 ) 10 11 var ( 12 magenta = ansi.ColorFunc("magenta") 13 cyan = ansi.ColorFunc("cyan") 14 red = ansi.ColorFunc("red") 15 yellow = ansi.ColorFunc("yellow") 16 blue = ansi.ColorFunc("blue") 17 green = ansi.ColorFunc("green") 18 gray = ansi.ColorFunc("black+h") 19 bold = ansi.ColorFunc("default+b") 20 cyanBold = ansi.ColorFunc("cyan+b") 21 22 gray256 = func(t string) string { 23 return fmt.Sprintf("\x1b[%d;5;%dm%s\x1b[m", 38, 242, t) 24 } 25 ) 26 27 func EnvColorDisabled() bool { 28 return os.Getenv("NO_COLOR") != "" || os.Getenv("CLICOLOR") == "0" 29 } 30 31 func EnvColorForced() bool { 32 return os.Getenv("CLICOLOR_FORCE") != "" && os.Getenv("CLICOLOR_FORCE") != "0" 33 } 34 35 func Is256ColorSupported() bool { 36 term := os.Getenv("TERM") 37 colorterm := os.Getenv("COLORTERM") 38 39 return strings.Contains(term, "256") || 40 strings.Contains(term, "24bit") || 41 strings.Contains(term, "truecolor") || 42 strings.Contains(colorterm, "256") || 43 strings.Contains(colorterm, "24bit") || 44 strings.Contains(colorterm, "truecolor") 45 } 46 47 func NewColorScheme(enabled, is256enabled bool) *ColorScheme { 48 return &ColorScheme{ 49 enabled: enabled, 50 is256enabled: is256enabled, 51 } 52 } 53 54 type ColorScheme struct { 55 enabled bool 56 is256enabled bool 57 } 58 59 func (c *ColorScheme) Bold(t string) string { 60 if !c.enabled { 61 return t 62 } 63 return bold(t) 64 } 65 66 func (c *ColorScheme) Boldf(t string, args ...interface{}) string { 67 return c.Bold(fmt.Sprintf(t, args...)) 68 } 69 70 func (c *ColorScheme) Red(t string) string { 71 if !c.enabled { 72 return t 73 } 74 return red(t) 75 } 76 77 func (c *ColorScheme) Redf(t string, args ...interface{}) string { 78 return c.Red(fmt.Sprintf(t, args...)) 79 } 80 81 func (c *ColorScheme) Yellow(t string) string { 82 if !c.enabled { 83 return t 84 } 85 return yellow(t) 86 } 87 88 func (c *ColorScheme) Yellowf(t string, args ...interface{}) string { 89 return c.Yellow(fmt.Sprintf(t, args...)) 90 } 91 92 func (c *ColorScheme) Green(t string) string { 93 if !c.enabled { 94 return t 95 } 96 return green(t) 97 } 98 99 func (c *ColorScheme) Greenf(t string, args ...interface{}) string { 100 return c.Green(fmt.Sprintf(t, args...)) 101 } 102 103 func (c *ColorScheme) Gray(t string) string { 104 if !c.enabled { 105 return t 106 } 107 if c.is256enabled { 108 return gray256(t) 109 } 110 return gray(t) 111 } 112 113 func (c *ColorScheme) Grayf(t string, args ...interface{}) string { 114 return c.Gray(fmt.Sprintf(t, args...)) 115 } 116 117 func (c *ColorScheme) Magenta(t string) string { 118 if !c.enabled { 119 return t 120 } 121 return magenta(t) 122 } 123 124 func (c *ColorScheme) Magentaf(t string, args ...interface{}) string { 125 return c.Magenta(fmt.Sprintf(t, args...)) 126 } 127 128 func (c *ColorScheme) Cyan(t string) string { 129 if !c.enabled { 130 return t 131 } 132 return cyan(t) 133 } 134 135 func (c *ColorScheme) Cyanf(t string, args ...interface{}) string { 136 return c.Cyan(fmt.Sprintf(t, args...)) 137 } 138 139 func (c *ColorScheme) CyanBold(t string) string { 140 if !c.enabled { 141 return t 142 } 143 return cyanBold(t) 144 } 145 146 func (c *ColorScheme) Blue(t string) string { 147 if !c.enabled { 148 return t 149 } 150 return blue(t) 151 } 152 153 func (c *ColorScheme) Bluef(t string, args ...interface{}) string { 154 return c.Blue(fmt.Sprintf(t, args...)) 155 } 156 157 func (c *ColorScheme) SuccessIcon() string { 158 return c.SuccessIconWithColor(c.Green) 159 } 160 161 func (c *ColorScheme) SuccessIconWithColor(colo func(string) string) string { 162 return colo("✓") 163 } 164 165 func (c *ColorScheme) WarningIcon() string { 166 return c.Yellow("!") 167 } 168 169 func (c *ColorScheme) FailureIcon() string { 170 return c.FailureIconWithColor(c.Red) 171 } 172 173 func (c *ColorScheme) FailureIconWithColor(colo func(string) string) string { 174 return colo("X") 175 } 176 177 func (c *ColorScheme) ColorFromString(s string) func(string) string { 178 s = strings.ToLower(s) 179 var fn func(string) string 180 switch s { 181 case "bold": 182 fn = c.Bold 183 case "red": 184 fn = c.Red 185 case "yellow": 186 fn = c.Yellow 187 case "green": 188 fn = c.Green 189 case "gray": 190 fn = c.Gray 191 case "magenta": 192 fn = c.Magenta 193 case "cyan": 194 fn = c.Cyan 195 case "blue": 196 fn = c.Blue 197 default: 198 fn = func(s string) string { 199 return s 200 } 201 } 202 203 return fn 204 }