| 1 |
Aurora |
| 2 |
====== |
| 3 |
|
| 4 |
[](https://pkg.go.dev/github.com/logrusorgru/aurora/v3?tab=doc) |
| 5 |
[](http://unlicense.org/) |
| 6 |
[](https://github.com/logrusorgru/aurora/actions?workflow=build) |
| 7 |
[](https://coveralls.io/github/logrusorgru/aurora?branch=master) |
| 8 |
[](https://goreportcard.com/report/logrusorgru/aurora) |
| 9 |
|
| 10 |
Ultimate ANSI colors for Golang. The package supports Printf/Sprintf etc. |
| 11 |
|
| 12 |
|
| 13 |
 |
| 14 |
|
| 15 |
# TOC |
| 16 |
|
| 17 |
- [Installation](#installation) |
| 18 |
- [Usage](#usage) |
| 19 |
+ [Simple](#simple) |
| 20 |
+ [Printf](#printf) |
| 21 |
+ [aurora.Sprintf](#aurorasprintf) |
| 22 |
+ [Enable/Disable colors](#enabledisable-colors) |
| 23 |
+ [Hyperlinks, default colorizer, and configurations](#hyperlinks-default-colorizer-and-configurations) |
| 24 |
- [Chains](#chains) |
| 25 |
- [Colorize](#colorize) |
| 26 |
- [Grayscale](#grayscale) |
| 27 |
- [8-bit colors](#8-bit-colors) |
| 28 |
- [Supported Colors & Formats](#supported-colors--formats) |
| 29 |
+ [All colors](#all-colors) |
| 30 |
+ [Standard and bright colors](#standard-and-bright-colors) |
| 31 |
+ [Formats are likely supported](#formats-are-likely-supported) |
| 32 |
+ [Formats are likely unsupported](#formats-are-likely-unsupported) |
| 33 |
- [Limitations](#limitations) |
| 34 |
+ [Windows](#windows) |
| 35 |
+ [TTY](#tty) |
| 36 |
- [Licensing](#licensing) |
| 37 |
|
| 38 |
# Installation |
| 39 |
|
| 40 |
##### Version 1.x |
| 41 |
|
| 42 |
Using gopkg.in. |
| 43 |
|
| 44 |
``` |
| 45 |
go get -u gopkg.in/logrusorgru/aurora.v1 |
| 46 |
``` |
| 47 |
|
| 48 |
##### Version 2.x |
| 49 |
|
| 50 |
``` |
| 51 |
go get -u github.com/logrusorgru/aurora |
| 52 |
``` |
| 53 |
|
| 54 |
##### Go modules support, version v3+ |
| 55 |
|
| 56 |
Get |
| 57 |
``` |
| 58 |
go get -u github.com/logrusorgru/aurora/v3 |
| 59 |
``` |
| 60 |
|
| 61 |
The v3 was introduced to support `go.mod` and leave previous import paths as is. |
| 62 |
Currently, there is no changes between them (excluding the importpath's /v3 tail). |
| 63 |
|
| 64 |
##### The latest version |
| 65 |
|
| 66 |
``` |
| 67 |
go get -u github.com/logrusorgru/aurora/v4 |
| 68 |
``` |
| 69 |
|
| 70 |
With hyperlinks. |
| 71 |
|
| 72 |
# Test |
| 73 |
|
| 74 |
``` |
| 75 |
go test -cover -race github.com/logrusorgru/aurora/v4 |
| 76 |
``` |
| 77 |
|
| 78 |
Replace the import path with your, if it's different. |
| 79 |
|
| 80 |
# Usage |
| 81 |
|
| 82 |
### Simple |
| 83 |
|
| 84 |
```go |
| 85 |
package main |
| 86 |
|
| 87 |
import ( |
| 88 |
"fmt" |
| 89 |
|
| 90 |
"github.com/logrusorgru/aurora/v4" |
| 91 |
) |
| 92 |
|
| 93 |
func main() { |
| 94 |
fmt.Println("Hello,", aurora.Magenta("Aurora")) |
| 95 |
fmt.Println(aurora.Bold(aurora.Cyan("Cya!"))) |
| 96 |
} |
| 97 |
|
| 98 |
``` |
| 99 |
|
| 100 |
 |
| 101 |
|
| 102 |
### Printf |
| 103 |
|
| 104 |
```go |
| 105 |
package main |
| 106 |
|
| 107 |
import ( |
| 108 |
"fmt" |
| 109 |
|
| 110 |
"github.com/logrusorgru/aurora/v4" |
| 111 |
) |
| 112 |
|
| 113 |
func main() { |
| 114 |
fmt.Printf("Got it %d times\n", aurora.Green(1240)) |
| 115 |
fmt.Printf("PI is %+1.2e\n", aurora.Cyan(3.14)) |
| 116 |
} |
| 117 |
|
| 118 |
``` |
| 119 |
|
| 120 |
 |
| 121 |
|
| 122 |
### aurora.Sprintf |
| 123 |
|
| 124 |
```go |
| 125 |
package main |
| 126 |
|
| 127 |
import ( |
| 128 |
"fmt" |
| 129 |
|
| 130 |
"github.com/logrusorgru/aurora/v4" |
| 131 |
) |
| 132 |
|
| 133 |
func main() { |
| 134 |
fmt.Println(aurora.Sprintf(aurora.Magenta("Got it %d times"), aurora.Green(1240))) |
| 135 |
} |
| 136 |
|
| 137 |
``` |
| 138 |
|
| 139 |
 |
| 140 |
|
| 141 |
### Enable/Disable colors |
| 142 |
|
| 143 |
```go |
| 144 |
package main |
| 145 |
|
| 146 |
import ( |
| 147 |
"fmt" |
| 148 |
"flag" |
| 149 |
|
| 150 |
"github.com/logrusorgru/aurora/v4" |
| 151 |
) |
| 152 |
|
| 153 |
// colorizer |
| 154 |
var au *aurora.Aurora |
| 155 |
|
| 156 |
var colors = flag.Bool("colors", false, "enable or disable colors") |
| 157 |
|
| 158 |
func init() { |
| 159 |
flag.Parse() |
| 160 |
au = aurora.New(WithColors(*colors)) |
| 161 |
} |
| 162 |
|
| 163 |
func main() { |
| 164 |
// use colorizer |
| 165 |
fmt.Println(au.Green("Hello")) |
| 166 |
} |
| 167 |
|
| 168 |
``` |
| 169 |
Without flags: |
| 170 |
 |
| 171 |
|
| 172 |
With `-colors` flag: |
| 173 |
 |
| 174 |
|
| 175 |
### Hyperlinks, default colorizer, and configurations |
| 176 |
|
| 177 |
[Hyperlinks feature description](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda). |
| 178 |
|
| 179 |
Add a red hyperlinks with text "Example" that is referencing to |
| 180 |
http://example.com. |
| 181 |
|
| 182 |
```go |
| 183 |
package main |
| 184 |
|
| 185 |
import ( |
| 186 |
"flag" |
| 187 |
"fmt" |
| 188 |
|
| 189 |
"github.com/logrusorgru/aurora/v4" |
| 190 |
) |
| 191 |
|
| 192 |
func main() { |
| 193 |
var conf = aurora.NewConfig() |
| 194 |
conf.AddFlags(flag.CommandLine, "prefix.") |
| 195 |
flag.Parse() |
| 196 |
|
| 197 |
aurora.DefaultColorizer = aurora.New(conf.Options()...) // set global |
| 198 |
|
| 199 |
fmt.Println(aurora.Red("Example").Hyperlink("http://example.com/")) |
| 200 |
} |
| 201 |
``` |
| 202 |
Depending flags: |
| 203 |
 |
| 204 |
 |
| 205 |
|
| 206 |
# Chains |
| 207 |
|
| 208 |
The following samples are equal |
| 209 |
|
| 210 |
```go |
| 211 |
x := aurora.BgMagenta(aurora.Bold(aurora.Red("x"))) |
| 212 |
``` |
| 213 |
|
| 214 |
```go |
| 215 |
x := aurora.Red("x").Bold().BgMagenta() |
| 216 |
``` |
| 217 |
|
| 218 |
The second is more readable |
| 219 |
|
| 220 |
# Colorize |
| 221 |
|
| 222 |
There is `Colorize` function that allows to choose some colors and |
| 223 |
format from a side |
| 224 |
|
| 225 |
```go |
| 226 |
|
| 227 |
func getColors() Color { |
| 228 |
// some stuff that returns appropriate colors and format |
| 229 |
} |
| 230 |
|
| 231 |
// [...] |
| 232 |
|
| 233 |
func main() { |
| 234 |
fmt.Println(aurora.Colorize("Greeting", getColors())) |
| 235 |
} |
| 236 |
|
| 237 |
``` |
| 238 |
Less complicated example |
| 239 |
|
| 240 |
```go |
| 241 |
x := aurora.Colorize("Greeting", GreenFg|GrayBg|BoldFm) |
| 242 |
``` |
| 243 |
|
| 244 |
Unlike other color functions and methods (such as Red/BgBlue etc) |
| 245 |
a `Colorize` clears previous colors |
| 246 |
|
| 247 |
```go |
| 248 |
x := aurora.Red("x").Colorize(BgGreen) // will be with green background only |
| 249 |
``` |
| 250 |
|
| 251 |
# Grayscale |
| 252 |
|
| 253 |
```go |
| 254 |
fmt.Println(" ", |
| 255 |
aurora.Gray(1-1, " 00-23 ").BgGray(24-1), |
| 256 |
aurora.Gray(4-1, " 03-19 ").BgGray(20-1), |
| 257 |
aurora.Gray(8-1, " 07-15 ").BgGray(16-1), |
| 258 |
aurora.Gray(12-1, " 11-11 ").BgGray(12-1), |
| 259 |
aurora.Gray(16-1, " 15-07 ").BgGray(8-1), |
| 260 |
aurora.Gray(20-1, " 19-03 ").BgGray(4-1), |
| 261 |
aurora.Gray(24-1, " 23-00 ").BgGray(1-1), |
| 262 |
) |
| 263 |
``` |
| 264 |
|
| 265 |
 |
| 266 |
|
| 267 |
# 8-bit colors |
| 268 |
|
| 269 |
Methods `Index` and `BgIndex` implements 8-bit colors. |
| 270 |
|
| 271 |
| Index/BgIndex | Meaning | Foreground | Background | |
| 272 |
| -------------- | --------------- | ---------- | ---------- | |
| 273 |
| 0- 7 | standard colors | 30- 37 | 40- 47 | |
| 274 |
| 8- 15 | bright colors | 90- 97 | 100-107 | |
| 275 |
| 16-231 | 216 colors | 38;5;n | 48;5;n | |
| 276 |
| 232-255 | 24 grayscale | 38;5;n | 48;5;n | |
| 277 |
|
| 278 |
Example |
| 279 |
|
| 280 |
```go |
| 281 |
package main |
| 282 |
|
| 283 |
import ( |
| 284 |
"fmt" |
| 285 |
"github.com/logrusorgru/aurora" |
| 286 |
) |
| 287 |
|
| 288 |
func main() { |
| 289 |
for i := uint8(16); i <= 231; i++ { |
| 290 |
fmt.Println(i, aurora.Index(i, "pew-pew"), aurora.BgIndex(i, "pew-pew")) |
| 291 |
} |
| 292 |
} |
| 293 |
``` |
| 294 |
|
| 295 |
# Supported colors & formats |
| 296 |
|
| 297 |
- formats |
| 298 |
+ bold (1) |
| 299 |
+ faint (2) |
| 300 |
+ doubly-underline (21) |
| 301 |
+ fraktur (20) |
| 302 |
+ italic (3) |
| 303 |
+ underline (4) |
| 304 |
+ slow blink (5) |
| 305 |
+ rapid blink (6) |
| 306 |
+ reverse video (7) |
| 307 |
+ conceal (8) |
| 308 |
+ crossed out (9) |
| 309 |
+ framed (51) |
| 310 |
+ encircled (52) |
| 311 |
+ overlined (53) |
| 312 |
- background and foreground colors, including bright |
| 313 |
+ black |
| 314 |
+ red |
| 315 |
+ green |
| 316 |
+ yellow (brown) |
| 317 |
+ blue |
| 318 |
+ magenta |
| 319 |
+ cyan |
| 320 |
+ white |
| 321 |
+ 24 grayscale colors |
| 322 |
+ 216 8-bit colors |
| 323 |
|
| 324 |
### All colors |
| 325 |
|
| 326 |
 |
| 327 |
 |
| 328 |
|
| 329 |
### Standard and bright colors |
| 330 |
|
| 331 |
 |
| 332 |
 |
| 333 |
|
| 334 |
### Formats are likely supported |
| 335 |
|
| 336 |
 |
| 337 |
|
| 338 |
### Formats are likely unsupported |
| 339 |
|
| 340 |
 |
| 341 |
|
| 342 |
# Limitations |
| 343 |
|
| 344 |
There is no way to represent `%T` and `%p` with colors using |
| 345 |
a standard approach |
| 346 |
|
| 347 |
```go |
| 348 |
package main |
| 349 |
|
| 350 |
import ( |
| 351 |
"fmt" |
| 352 |
|
| 353 |
"github.com/logrusorgru/aurora" |
| 354 |
) |
| 355 |
|
| 356 |
func main() { |
| 357 |
var ( |
| 358 |
r = aurora.Red("red") |
| 359 |
i int |
| 360 |
) |
| 361 |
fmt.Printf("%T %p\n", r, aurora.Green(&i)) |
| 362 |
} |
| 363 |
``` |
| 364 |
|
| 365 |
Output will be without colors |
| 366 |
|
| 367 |
``` |
| 368 |
aurora.value %!p(aurora.value={0xc42000a310 768 0}) |
| 369 |
``` |
| 370 |
|
| 371 |
The obvious workaround is `Red(fmt.Sprintf("%T", some))` |
| 372 |
|
| 373 |
### Windows |
| 374 |
|
| 375 |
The Aurora provides ANSI colors only, so there is no support for Windows. That said, there are workarounds available. |
| 376 |
Check out these comments to learn more: |
| 377 |
|
| 378 |
- [Using go-colorable](https://github.com/logrusorgru/aurora/issues/2#issuecomment-299014211). |
| 379 |
- [Using registry for Windows 10](https://github.com/logrusorgru/aurora/issues/10#issue-476361247). |
| 380 |
|
| 381 |
### TTY |
| 382 |
|
| 383 |
The Aurora has no internal TTY detectors by design. Take a look |
| 384 |
[this comment](https://github.com/logrusorgru/aurora/issues/2#issuecomment-299030108) if you want turn |
| 385 |
on colors for a terminal only, and turn them off for a file. |
| 386 |
|
| 387 |
### Licensing |
| 388 |
|
| 389 |
Copyright © 2016-2022 The Aurora Authors. This work is free. |
| 390 |
It comes without any warranty, to the extent permitted by applicable |
| 391 |
law. You can redistribute it and/or modify it under the terms of the |
| 392 |
the Unlicense. See the LICENSE file for more details. |