Don Newton | 2bdfd3f | 2019-04-08 17:06:33 -0400 | [diff] [blame] | 1 | // github.com/bclicn/color |
| 2 | // colorized output for Mac & Linux terminal |
| 3 | // version: 1.0.0 |
| 4 | // author: bcli, bclicn@gmail.com, 2018-7-11 |
| 5 | // see: http://misc.flogisoft.com/bash/tip_colors_and_formatting |
| 6 | // usage: |
| 7 | // Official layout |
| 8 | // $ go get github.com/bclicn/color |
| 9 | // # in your project |
| 10 | // import "github.com/bclicn/color" |
| 11 | // func main() { |
| 12 | // fmt.Println(color.Red("I'm red!")) |
| 13 | // color.Test() |
| 14 | // } |
| 15 | |
| 16 | package color |
| 17 | |
| 18 | import ( |
| 19 | "strconv" |
| 20 | ) |
| 21 | |
| 22 | const ( |
| 23 | // common |
| 24 | reset = "\033[0m" // auto reset the rest of text to default color |
| 25 | normal = 0 |
| 26 | bold = 1 // increase this value if you want bolder text |
| 27 | // special |
| 28 | dim = 2 |
| 29 | underline = 4 |
| 30 | blink = 5 |
| 31 | reverse = 7 |
| 32 | hidden = 8 |
| 33 | // color |
| 34 | black = 30 // default = 39 |
| 35 | red = 31 |
| 36 | green = 32 |
| 37 | yellow = 33 |
| 38 | blue = 34 |
| 39 | purple = 35 // purple = magenta |
| 40 | cyan = 36 |
| 41 | lightGray = 37 |
| 42 | darkGray = 90 |
| 43 | lightRed = 91 |
| 44 | lightGreen = 92 |
| 45 | lightYellow = 93 |
| 46 | lightBlue = 94 |
| 47 | lightPurple = 95 |
| 48 | lightCyan = 96 |
| 49 | white = 97 |
| 50 | ) |
| 51 | |
| 52 | // you can use custom color code and font size by calling this function |
| 53 | func Render(colorCode int, fontSize int, content string) string { |
| 54 | return "\033[" + strconv.Itoa(fontSize) + ";" + strconv.Itoa(colorCode) + "m" + content + reset |
| 55 | } |
| 56 | |
| 57 | // black text (use this with caution since most geeks use dark console) |
| 58 | func Black(txt string) string { |
| 59 | return Render(black, normal, txt) |
| 60 | } |
| 61 | |
| 62 | // red text |
| 63 | func Red(txt string) string { |
| 64 | return Render(red, normal, txt) |
| 65 | } |
| 66 | |
| 67 | // green text |
| 68 | func Green(txt string) string { |
| 69 | return Render(green, normal, txt) |
| 70 | } |
| 71 | |
| 72 | // yellow text |
| 73 | func Yellow(txt string) string { |
| 74 | return Render(yellow, normal, txt) |
| 75 | } |
| 76 | |
| 77 | // blue text |
| 78 | func Blue(txt string) string { |
| 79 | return Render(blue, normal, txt) |
| 80 | } |
| 81 | |
| 82 | // purple text |
| 83 | func Purple(txt string) string { |
| 84 | return Render(purple, normal, txt) |
| 85 | } |
| 86 | |
| 87 | // cyan text |
| 88 | func Cyan(txt string) string { |
| 89 | return Render(cyan, normal, txt) |
| 90 | } |
| 91 | |
| 92 | // light gray text |
| 93 | func LightGray(txt string) string { |
| 94 | return Render(lightGray, normal, txt) |
| 95 | } |
| 96 | |
| 97 | // dark gray text |
| 98 | func DarkGray(txt string) string { |
| 99 | return Render(darkGray, normal, txt) |
| 100 | } |
| 101 | |
| 102 | // light red text |
| 103 | func LightRed(txt string) string { |
| 104 | return Render(lightRed, normal, txt) |
| 105 | } |
| 106 | |
| 107 | // light green text |
| 108 | func LightGreen(txt string) string { |
| 109 | return Render(lightGreen, normal, txt) |
| 110 | } |
| 111 | |
| 112 | // light yellow text |
| 113 | func LightYellow(txt string) string { |
| 114 | return Render(lightYellow, normal, txt) |
| 115 | } |
| 116 | |
| 117 | // light blue text |
| 118 | func LightBlue(txt string) string { |
| 119 | return Render(lightBlue, normal, txt) |
| 120 | } |
| 121 | |
| 122 | // light purple text |
| 123 | func LightPurple(txt string) string { |
| 124 | return Render(lightPurple, normal, txt) |
| 125 | } |
| 126 | |
| 127 | // light cyan text |
| 128 | func LightCyan(txt string) string { |
| 129 | return Render(lightCyan, normal, txt) |
| 130 | } |
| 131 | |
| 132 | // white text |
| 133 | func White(txt string) string { |
| 134 | return Render(white, normal, txt) |
| 135 | } |
| 136 | |
| 137 | // black text (use this with caution since most geeks use dark console) |
| 138 | func BBlack(txt string) string { |
| 139 | return Render(black, bold, txt) |
| 140 | } |
| 141 | |
| 142 | // bold red |
| 143 | func BRed(txt string) string { |
| 144 | return Render(red, bold, txt) |
| 145 | } |
| 146 | |
| 147 | // bold green |
| 148 | func BGreen(txt string) string { |
| 149 | return Render(green, bold, txt) |
| 150 | } |
| 151 | |
| 152 | // bold yellow |
| 153 | func BYellow(txt string) string { |
| 154 | return Render(yellow, bold, txt) |
| 155 | } |
| 156 | |
| 157 | // bold blue |
| 158 | func BBlue(txt string) string { |
| 159 | return Render(blue, bold, txt) |
| 160 | } |
| 161 | |
| 162 | // bold purple |
| 163 | func BPurple(txt string) string { |
| 164 | return Render(purple, bold, txt) |
| 165 | } |
| 166 | |
| 167 | // bold cyan |
| 168 | func BCyan(txt string) string { |
| 169 | return Render(cyan, bold, txt) |
| 170 | } |
| 171 | |
| 172 | // bold light gray |
| 173 | func BLightGray(txt string) string { |
| 174 | return Render(lightGray, bold, txt) |
| 175 | } |
| 176 | |
| 177 | // bold dark gray |
| 178 | func BDarkGray(txt string) string { |
| 179 | return Render(darkGray, bold, txt) |
| 180 | } |
| 181 | |
| 182 | // bold light red |
| 183 | func BLightRed(txt string) string { |
| 184 | return Render(lightRed, bold, txt) |
| 185 | } |
| 186 | |
| 187 | // bold light green |
| 188 | func BLightGreen(txt string) string { |
| 189 | return Render(lightGreen, bold, txt) |
| 190 | } |
| 191 | |
| 192 | // bold light yellow |
| 193 | func BLightYellow(txt string) string { |
| 194 | return Render(lightYellow, bold, txt) |
| 195 | } |
| 196 | |
| 197 | // bold light blue |
| 198 | func BLightBlue(txt string) string { |
| 199 | return Render(lightBlue, bold, txt) |
| 200 | } |
| 201 | |
| 202 | // bold light purple |
| 203 | func BLightPurple(txt string) string { |
| 204 | return Render(lightPurple, bold, txt) |
| 205 | } |
| 206 | |
| 207 | // bold light cyan |
| 208 | func BLightCyan(txt string) string { |
| 209 | return Render(lightCyan, bold, txt) |
| 210 | } |
| 211 | |
| 212 | // bold white |
| 213 | func BWhite(txt string) string { |
| 214 | return Render(white, bold, txt) |
| 215 | } |
| 216 | |
| 217 | // black background (use this with caution since most of geeks use dark console) |
| 218 | func GBlack(txt string) string { |
| 219 | return Render(black+1, normal, txt) |
| 220 | } |
| 221 | |
| 222 | // red background |
| 223 | func GRed(txt string) string { |
| 224 | return Render(red+1, normal, txt) |
| 225 | } |
| 226 | |
| 227 | // green background |
| 228 | func GGreen(txt string) string { |
| 229 | return Render(green+1, normal, txt) |
| 230 | } |
| 231 | |
| 232 | // yellow background |
| 233 | func GYellow(txt string) string { |
| 234 | return Render(yellow+1, normal, txt) |
| 235 | } |
| 236 | |
| 237 | // blue background |
| 238 | func GBlue(txt string) string { |
| 239 | return Render(blue+1, normal, txt) |
| 240 | } |
| 241 | |
| 242 | // purple background |
| 243 | func GPurple(txt string) string { |
| 244 | return Render(purple+1, normal, txt) |
| 245 | } |
| 246 | |
| 247 | // cyan background |
| 248 | func GCyan(txt string) string { |
| 249 | return Render(cyan+1, normal, txt) |
| 250 | } |
| 251 | |
| 252 | // light gray background |
| 253 | func GLightGray(txt string) string { |
| 254 | return Render(lightGray+1, normal, txt) |
| 255 | } |
| 256 | |
| 257 | // dark gray background |
| 258 | func GDarkGray(txt string) string { |
| 259 | return Render(darkGray+1, normal, txt) |
| 260 | } |
| 261 | |
| 262 | // light red background |
| 263 | func GLightRed(txt string) string { |
| 264 | return Render(lightRed+1, normal, txt) |
| 265 | } |
| 266 | |
| 267 | // light green background |
| 268 | func GLightGreen(txt string) string { |
| 269 | return Render(lightGreen+1, normal, txt) |
| 270 | } |
| 271 | |
| 272 | // light yellow background |
| 273 | func GLightYellow(txt string) string { |
| 274 | return Render(lightYellow+1, normal, txt) |
| 275 | } |
| 276 | |
| 277 | // blue background |
| 278 | func GLightBlue(txt string) string { |
| 279 | return Render(lightBlue+1, normal, txt) |
| 280 | } |
| 281 | |
| 282 | // light purple background |
| 283 | func GLightPurple(txt string) string { |
| 284 | return Render(lightPurple+1, normal, txt) |
| 285 | } |
| 286 | |
| 287 | // light cyan background |
| 288 | func GLightCyan(txt string) string { |
| 289 | return Render(lightCyan+1, normal, txt) |
| 290 | } |
| 291 | |
| 292 | // give text a white background |
| 293 | func GWhite(txt string) string { |
| 294 | return Render(white+1, normal, txt) |
| 295 | } |
| 296 | |
| 297 | // bold text |
| 298 | func Bold(txt string) string { |
| 299 | return Render(bold, normal, txt) |
| 300 | } |
| 301 | |
| 302 | // dimmed text |
| 303 | func Dim(txt string) string { |
| 304 | return Render(dim, normal, txt) |
| 305 | } |
| 306 | |
| 307 | // underlined text |
| 308 | func Underline(txt string) string { |
| 309 | return Render(underline, 0, txt) |
| 310 | } |
| 311 | |
| 312 | // make given text blink, not supported by all consoles |
| 313 | func Blink(txt string) string { |
| 314 | return Render(blink, normal, txt) |
| 315 | } |
| 316 | |
| 317 | // invert the color of text and its background |
| 318 | func Invert(txt string) string { |
| 319 | return Render(reverse, normal, txt) |
| 320 | } |
| 321 | |
| 322 | // hide given text, useful for password input |
| 323 | func Hide(txt string) string { |
| 324 | return Render(hidden, normal, txt) |
| 325 | } |
| 326 | |
| 327 | // test all functions |
| 328 | func Test() { |
| 329 | ColorTest() |
| 330 | } |