mirror of
https://github.com/muun/recovery.git
synced 2025-02-23 11:32:33 -05:00
315 lines
7.3 KiB
Markdown
315 lines
7.3 KiB
Markdown
|
Aurora
|
||
|
======
|
||
|
|
||
|
[](https://pkg.go.dev/github.com/logrusorgru/aurora?tab=doc)
|
||
|
[](http://unlicense.org/)
|
||
|
[](https://travis-ci.org/logrusorgru/aurora)
|
||
|
[](https://coveralls.io/r/logrusorgru/aurora?branch=master)
|
||
|
[](https://goreportcard.com/report/logrusorgru/aurora)
|
||
|
[](https://gitter.im/logrusorgru/aurora)
|
||
|
|
||
|
Ultimate ANSI colors for Golang. The package supports Printf/Sprintf etc.
|
||
|
|
||
|
|
||
|

|
||
|
|
||
|
# TOC
|
||
|
|
||
|
- [Installation](#installation)
|
||
|
- [Usage](#usage)
|
||
|
+ [Simple](#simple)
|
||
|
+ [Printf](#printf)
|
||
|
+ [aurora.Sprintf](#aurorasprintf)
|
||
|
+ [Enable/Disable colors](#enabledisable-colors)
|
||
|
- [Chains](#chains)
|
||
|
- [Colorize](#colorize)
|
||
|
- [Grayscale](#grayscale)
|
||
|
- [8-bit colors](#8-bit-colors)
|
||
|
- [Supported Colors & Formats](#supported-colors--formats)
|
||
|
+ [All colors](#all-colors)
|
||
|
+ [Standard and bright colors](#standard-and-bright-colors)
|
||
|
+ [Formats are likely supported](#formats-are-likely-supported)
|
||
|
+ [Formats are likely unsupported](#formats-are-likely-unsupported)
|
||
|
- [Limitations](#limitations)
|
||
|
+ [Windows](#windows)
|
||
|
+ [TTY](#tty)
|
||
|
- [Licensing](#licensing)
|
||
|
|
||
|
# Installation
|
||
|
|
||
|
Get
|
||
|
```
|
||
|
go get -u github.com/logrusorgru/aurora
|
||
|
```
|
||
|
Test
|
||
|
```
|
||
|
go test -cover github.com/logrusorgru/aurora
|
||
|
```
|
||
|
|
||
|
# Usage
|
||
|
|
||
|
### Simple
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
. "github.com/logrusorgru/aurora"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
fmt.Println("Hello,", Magenta("Aurora"))
|
||
|
fmt.Println(Bold(Cyan("Cya!")))
|
||
|
}
|
||
|
|
||
|
```
|
||
|
|
||
|

|
||
|
|
||
|
### Printf
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
. "github.com/logrusorgru/aurora"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
fmt.Printf("Got it %d times\n", Green(1240))
|
||
|
fmt.Printf("PI is %+1.2e\n", Cyan(3.14))
|
||
|
}
|
||
|
|
||
|
```
|
||
|
|
||
|

|
||
|
|
||
|
### aurora.Sprintf
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
. "github.com/logrusorgru/aurora"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
fmt.Println(Sprintf(Magenta("Got it %d times"), Green(1240)))
|
||
|
}
|
||
|
|
||
|
```
|
||
|
|
||
|

|
||
|
|
||
|
### Enable/Disable colors
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"flag"
|
||
|
|
||
|
"github.com/logrusorgru/aurora"
|
||
|
)
|
||
|
|
||
|
// colorizer
|
||
|
var au aurora.Aurora
|
||
|
|
||
|
var colors = flag.Bool("colors", false, "enable or disable colors")
|
||
|
|
||
|
func init() {
|
||
|
flag.Parse()
|
||
|
au = aurora.NewAurora(*colors)
|
||
|
}
|
||
|
|
||
|
func main() {
|
||
|
// use colorizer
|
||
|
fmt.Println(au.Green("Hello"))
|
||
|
}
|
||
|
|
||
|
```
|
||
|
Without flags:
|
||
|

|
||
|
|
||
|
With `-colors` flag:
|
||
|

|
||
|
|
||
|
# Chains
|
||
|
|
||
|
The following samples are equal
|
||
|
|
||
|
```go
|
||
|
x := BgMagenta(Bold(Red("x")))
|
||
|
```
|
||
|
|
||
|
```go
|
||
|
x := Red("x").Bold().BgMagenta()
|
||
|
```
|
||
|
|
||
|
The second is more readable
|
||
|
|
||
|
# Colorize
|
||
|
|
||
|
There is `Colorize` function that allows to choose some colors and
|
||
|
format from a side
|
||
|
|
||
|
```go
|
||
|
|
||
|
func getColors() Color {
|
||
|
// some stuff that returns appropriate colors and format
|
||
|
}
|
||
|
|
||
|
// [...]
|
||
|
|
||
|
func main() {
|
||
|
fmt.Println(Colorize("Greeting", getColors()))
|
||
|
}
|
||
|
|
||
|
```
|
||
|
Less complicated example
|
||
|
|
||
|
```go
|
||
|
x := Colorize("Greeting", GreenFg|GrayBg|BoldFm)
|
||
|
```
|
||
|
|
||
|
Unlike other color functions and methods (such as Red/BgBlue etc)
|
||
|
a `Colorize` clears previous colors
|
||
|
|
||
|
```go
|
||
|
x := Red("x").Colorize(BgGreen) // will be with green background only
|
||
|
```
|
||
|
|
||
|
# Grayscale
|
||
|
|
||
|
```go
|
||
|
fmt.Println(" ",
|
||
|
Gray(1-1, " 00-23 ").BgGray(24-1),
|
||
|
Gray(4-1, " 03-19 ").BgGray(20-1),
|
||
|
Gray(8-1, " 07-15 ").BgGray(16-1),
|
||
|
Gray(12-1, " 11-11 ").BgGray(12-1),
|
||
|
Gray(16-1, " 15-07 ").BgGray(8-1),
|
||
|
Gray(20-1, " 19-03 ").BgGray(4-1),
|
||
|
Gray(24-1, " 23-00 ").BgGray(1-1),
|
||
|
)
|
||
|
```
|
||
|
|
||
|

|
||
|
|
||
|
# 8-bit colors
|
||
|
|
||
|
Methods `Index` and `BgIndex` implements 8-bit colors.
|
||
|
|
||
|
| Index/BgIndex | Meaning | Foreground | Background |
|
||
|
| -------------- | --------------- | ---------- | ---------- |
|
||
|
| 0- 7 | standard colors | 30- 37 | 40- 47 |
|
||
|
| 8- 15 | bright colors | 90- 97 | 100-107 |
|
||
|
| 16-231 | 216 colors | 38;5;n | 48;5;n |
|
||
|
| 232-255 | 24 grayscale | 38;5;n | 48;5;n |
|
||
|
|
||
|
|
||
|
# Supported colors & formats
|
||
|
|
||
|
- formats
|
||
|
+ bold (1)
|
||
|
+ faint (2)
|
||
|
+ doubly-underline (21)
|
||
|
+ fraktur (20)
|
||
|
+ italic (3)
|
||
|
+ underline (4)
|
||
|
+ slow blink (5)
|
||
|
+ rapid blink (6)
|
||
|
+ reverse video (7)
|
||
|
+ conceal (8)
|
||
|
+ crossed out (9)
|
||
|
+ framed (51)
|
||
|
+ encircled (52)
|
||
|
+ overlined (53)
|
||
|
- background and foreground colors, including bright
|
||
|
+ black
|
||
|
+ red
|
||
|
+ green
|
||
|
+ yellow (brown)
|
||
|
+ blue
|
||
|
+ magenta
|
||
|
+ cyan
|
||
|
+ white
|
||
|
+ 24 grayscale colors
|
||
|
+ 216 8-bit colors
|
||
|
|
||
|
### All colors
|
||
|
|
||
|

|
||
|

|
||
|
|
||
|
### Standard and bright colors
|
||
|
|
||
|

|
||
|

|
||
|
|
||
|
### Formats are likely supported
|
||
|
|
||
|

|
||
|
|
||
|
### Formats are likely unsupported
|
||
|
|
||
|

|
||
|
|
||
|
# Limitations
|
||
|
|
||
|
There is no way to represent `%T` and `%p` with colors using
|
||
|
a standard approach
|
||
|
|
||
|
```go
|
||
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
|
||
|
. "github.com/logrusorgru/aurora"
|
||
|
)
|
||
|
|
||
|
func main() {
|
||
|
r := Red("red")
|
||
|
var i int
|
||
|
fmt.Printf("%T %p\n", r, Green(&i))
|
||
|
}
|
||
|
```
|
||
|
|
||
|
Output will be without colors
|
||
|
|
||
|
```
|
||
|
aurora.value %!p(aurora.value={0xc42000a310 768 0})
|
||
|
```
|
||
|
|
||
|
The obvious workaround is `Red(fmt.Sprintf("%T", some))`
|
||
|
|
||
|
### Windows
|
||
|
|
||
|
The Aurora provides ANSI colors only, so there is no support for Windows. That said, there are workarounds available.
|
||
|
Check out these comments to learn more:
|
||
|
|
||
|
- [Using go-colorable](https://github.com/logrusorgru/aurora/issues/2#issuecomment-299014211).
|
||
|
- [Using registry for Windows 10](https://github.com/logrusorgru/aurora/issues/10#issue-476361247).
|
||
|
|
||
|
### TTY
|
||
|
|
||
|
The Aurora has no internal TTY detectors by design. Take a look
|
||
|
[this comment](https://github.com/logrusorgru/aurora/issues/2#issuecomment-299030108) if you want turn
|
||
|
on colors for a terminal only, and turn them off for a file.
|
||
|
|
||
|
### Licensing
|
||
|
|
||
|
Copyright © 2016-2020 The Aurora Authors. This work is free.
|
||
|
It comes without any warranty, to the extent permitted by applicable
|
||
|
law. You can redistribute it and/or modify it under the terms of the
|
||
|
the Unlicense. See the LICENSE file for more details.
|
||
|
|
||
|
|