README
Aurora
Ultimate ANSI colors for Golang. The package supports Printf/Sprintf etc.
TOC
- Installation
- Usage
- Chains
- Colorize
- Grayscale
- 8-bit colors
- Supported Colors & Formats
- Limitations
- Licensing
Installation
Version 1.x
Using gopkg.in.
go get -u gopkg.in/logrusorgru/aurora.v1
Version 2.x
go get -u github.com/logrusorgru/aurora
Go modules support, version v3+
Get
go get -u github.com/logrusorgru/aurora/v3
The v3 was introduced to support go.mod
and leave previous import paths as is.
Currently, there is no changes between them (excluding the importpath's /v3 tail).
Test
go test -cover github.com/logrusorgru/aurora/v3
Replace the import path with your, if it's different.
Usage
Simple
package main
import (
"fmt"
. "github.com/logrusorgru/aurora"
)
func main() {
fmt.Println("Hello,", Magenta("Aurora"))
fmt.Println(Bold(Cyan("Cya!")))
}
Printf
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
package main
import (
"fmt"
. "github.com/logrusorgru/aurora"
)
func main() {
fmt.Println(Sprintf(Magenta("Got it %d times"), Green(1240)))
}
Enable/Disable colors
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
x := BgMagenta(Bold(Red("x")))
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
func getColors() Color {
// some stuff that returns appropriate colors and format
}
// [...]
func main() {
fmt.Println(Colorize("Greeting", getColors()))
}
Less complicated example
x := Colorize("Greeting", GreenFg|GrayBg|BoldFm)
Unlike other color functions and methods (such as Red/BgBlue etc)
a Colorize
clears previous colors
x := Red("x").Colorize(BgGreen) // will be with green background only
Grayscale
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
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:
TTY
The Aurora has no internal TTY detectors by design. Take a look this comment 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.
Documentation
Overview ¶
Package aurora implements ANSI-colors
Example (Printf) ¶
Code:
fmt.Printf("%d %s", Blue(100), BgBlue("cats"))
�[34m100�[0m �[44mcats�[0m
Index ¶
- func Sprintf(format interface{}, args ...interface{}) string
- type Aurora
- type Color
- type Value
- func BgBlack(arg interface{}) Value
- func BgBlue(arg interface{}) Value
- func BgBrightBlack(arg interface{}) Value
- func BgBrightBlue(arg interface{}) Value
- func BgBrightCyan(arg interface{}) Value
- func BgBrightGreen(arg interface{}) Value
- func BgBrightMagenta(arg interface{}) Value
- func BgBrightRed(arg interface{}) Value
- func BgBrightWhite(arg interface{}) Value
- func BgBrightYellow(arg interface{}) Value
- func BgBrown(arg interface{}) Value
- func BgCyan(arg interface{}) Value
- func BgGray(n uint8, arg interface{}) Value
- func BgGreen(arg interface{}) Value
- func BgIndex(n uint8, arg interface{}) Value
- func BgMagenta(arg interface{}) Value
- func BgRed(arg interface{}) Value
- func BgWhite(arg interface{}) Value
- func BgYellow(arg interface{}) Value
- func Black(arg interface{}) Value
- func Blink(arg interface{}) Value
- func Blue(arg interface{}) Value
- func Bold(arg interface{}) Value
- func BrightBlack(arg interface{}) Value
- func BrightBlue(arg interface{}) Value
- func BrightCyan(arg interface{}) Value
- func BrightGreen(arg interface{}) Value
- func BrightMagenta(arg interface{}) Value
- func BrightRed(arg interface{}) Value
- func BrightWhite(arg interface{}) Value
- func BrightYellow(arg interface{}) Value
- func Brown(arg interface{}) Value
- func Colorize(arg interface{}, color Color) Value
- func Conceal(arg interface{}) Value
- func CrossedOut(arg interface{}) Value
- func Cyan(arg interface{}) Value
- func DoublyUnderline(arg interface{}) Value
- func Encircled(arg interface{}) Value
- func Faint(arg interface{}) Value
- func Fraktur(arg interface{}) Value
- func Framed(arg interface{}) Value
- func Gray(n uint8, arg interface{}) Value
- func Green(arg interface{}) Value
- func Hidden(arg interface{}) Value
- func Index(n uint8, arg interface{}) Value
- func Inverse(arg interface{}) Value
- func Italic(arg interface{}) Value
- func Magenta(arg interface{}) Value
- func Overlined(arg interface{}) Value
- func RapidBlink(arg interface{}) Value
- func Red(arg interface{}) Value
- func Reset(arg interface{}) Value
- func Reverse(arg interface{}) Value
- func SlowBlink(arg interface{}) Value
- func StrikeThrough(arg interface{}) Value
- func Underline(arg interface{}) Value
- func White(arg interface{}) Value
- func Yellow(arg interface{}) Value
Examples ¶
Constants ¶
Variables ¶
Functions ¶
func Sprintf ¶
func Sprintf(format interface{}, args ...interface{}) string
Sprintf allows to use Value as format. For example
v := Sprintf(Red("total: +3.5f points"), Blue(3.14))
In this case "total:" and "points" will be red, but 3.14 will be blue. But, in another example
v := Sprintf(Red("total: +3.5f points"), 3.14)
full string will be red. And no way to clear 3.14 to default format and color
Example ¶
Code:
fmt.Print(
Sprintf(
Blue("we've got %d cats, but want %d"), // <- blue format
Cyan(5),
Bold(Magenta(25)),
),
)
�[34mwe've got �[0;36m5�[0;34m cats, but want �[0;1;35m25�[0;34m�[0m
Types ¶
type Aurora ¶
type Aurora interface { // Reset wraps given argument returning Value // without formats and colors. Reset(arg interface{}) Value // // Formats // // // Bold or increased intensity (1). Bold(arg interface{}) Value // Faint, decreased intensity (2). Faint(arg interface{}) Value // // DoublyUnderline or Bold off, double-underline // per ECMA-48 (21). DoublyUnderline(arg interface{}) Value // Fraktur, rarely supported (20). Fraktur(arg interface{}) Value // // Italic, not widely supported, sometimes // treated as inverse (3). Italic(arg interface{}) Value // Underline (4). Underline(arg interface{}) Value // // SlowBlink, blinking less than 150 // per minute (5). SlowBlink(arg interface{}) Value // RapidBlink, blinking 150+ per minute, // not widely supported (6). RapidBlink(arg interface{}) Value // Blink is alias for the SlowBlink. Blink(arg interface{}) Value // // Reverse video, swap foreground and // background colors (7). Reverse(arg interface{}) Value // Inverse is alias for the Reverse Inverse(arg interface{}) Value // // Conceal, hidden, not widely supported (8). Conceal(arg interface{}) Value // Hidden is alias for the Conceal Hidden(arg interface{}) Value // // CrossedOut, characters legible, but // marked for deletion (9). CrossedOut(arg interface{}) Value // StrikeThrough is alias for the CrossedOut. StrikeThrough(arg interface{}) Value // // Framed (51). Framed(arg interface{}) Value // Encircled (52). Encircled(arg interface{}) Value // // Overlined (53). Overlined(arg interface{}) Value // // Foreground colors // // // Black foreground color (30) Black(arg interface{}) Value // Red foreground color (31) Red(arg interface{}) Value // Green foreground color (32) Green(arg interface{}) Value // Yellow foreground color (33) Yellow(arg interface{}) Value // Brown foreground color (33) // // Deprecated: use Yellow instead, following specification Brown(arg interface{}) Value // Blue foreground color (34) Blue(arg interface{}) Value // Magenta foreground color (35) Magenta(arg interface{}) Value // Cyan foreground color (36) Cyan(arg interface{}) Value // White foreground color (37) White(arg interface{}) Value // // Bright foreground colors // // BrightBlack foreground color (90) BrightBlack(arg interface{}) Value // BrightRed foreground color (91) BrightRed(arg interface{}) Value // BrightGreen foreground color (92) BrightGreen(arg interface{}) Value // BrightYellow foreground color (93) BrightYellow(arg interface{}) Value // BrightBlue foreground color (94) BrightBlue(arg interface{}) Value // BrightMagenta foreground color (95) BrightMagenta(arg interface{}) Value // BrightCyan foreground color (96) BrightCyan(arg interface{}) Value // BrightWhite foreground color (97) BrightWhite(arg interface{}) Value // // Other // // Index of pre-defined 8-bit foreground color // from 0 to 255 (38;5;n). // // 0- 7: standard colors (as in ESC [ 30–37 m) // 8- 15: high intensity colors (as in ESC [ 90–97 m) // 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) // 232-255: grayscale from black to white in 24 steps // Index(n uint8, arg interface{}) Value // Gray from 0 to 23. Gray(n uint8, arg interface{}) Value // // Background colors // // // BgBlack background color (40) BgBlack(arg interface{}) Value // BgRed background color (41) BgRed(arg interface{}) Value // BgGreen background color (42) BgGreen(arg interface{}) Value // BgYellow background color (43) BgYellow(arg interface{}) Value // BgBrown background color (43) // // Deprecated: use BgYellow instead, following specification BgBrown(arg interface{}) Value // BgBlue background color (44) BgBlue(arg interface{}) Value // BgMagenta background color (45) BgMagenta(arg interface{}) Value // BgCyan background color (46) BgCyan(arg interface{}) Value // BgWhite background color (47) BgWhite(arg interface{}) Value // // Bright background colors // // BgBrightBlack background color (100) BgBrightBlack(arg interface{}) Value // BgBrightRed background color (101) BgBrightRed(arg interface{}) Value // BgBrightGreen background color (102) BgBrightGreen(arg interface{}) Value // BgBrightYellow background color (103) BgBrightYellow(arg interface{}) Value // BgBrightBlue background color (104) BgBrightBlue(arg interface{}) Value // BgBrightMagenta background color (105) BgBrightMagenta(arg interface{}) Value // BgBrightCyan background color (106) BgBrightCyan(arg interface{}) Value // BgBrightWhite background color (107) BgBrightWhite(arg interface{}) Value // // Other // // BgIndex of 8-bit pre-defined background color // from 0 to 255 (48;5;n). // // 0- 7: standard colors (as in ESC [ 40–47 m) // 8- 15: high intensity colors (as in ESC [100–107 m) // 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) // 232-255: grayscale from black to white in 24 steps // BgIndex(n uint8, arg interface{}) Value // BgGray from 0 to 23. BgGray(n uint8, arg interface{}) Value // // Special // // Colorize removes existing colors and // formats of the argument and applies given. Colorize(arg interface{}, color Color) Value // // Support methods // // Sprintf allows to use colored format. Sprintf(format interface{}, args ...interface{}) string }
An Aurora implements colorizer interface. It also can be a non-colorizer
func NewAurora ¶
NewAurora returns a new Aurora interface that will support or not support colors depending the enableColors argument
Example (Colors) ¶
Code:
a := NewAurora(true) fmt.Println(a.Red("Red"))
�[31mRed�[0m
Example (No_colors) ¶
Code:
a := NewAurora(false) fmt.Println(a.Red("Not red"))
Not red
type Color ¶
type Color uint
A Color type is a color. It can contain one background color, one foreground color and a format, including ideogram related formats.
const ( BoldFm Color = 1 << iota // 1 FaintFm // 2 ItalicFm // 3 UnderlineFm // 4 SlowBlinkFm // 5 RapidBlinkFm // 6 ReverseFm // 7 ConcealFm // 8 CrossedOutFm // 9 FrakturFm // 20 DoublyUnderlineFm // 21 or bold off for some systems FramedFm // 51 EncircledFm // 52 OverlinedFm // 53 InverseFm = ReverseFm // alias to ReverseFm BlinkFm = SlowBlinkFm // alias to SlowBlinkFm HiddenFm = ConcealFm // alias to ConcealFm StrikeThroughFm = CrossedOutFm // alias to CrossedOutFm )
Special formats
const ( BlackFg Color = (iota << shiftFg) | flagFg // 30, 90 RedFg // 31, 91 GreenFg // 32, 92 YellowFg // 33, 93 BlueFg // 34, 94 MagentaFg // 35, 95 CyanFg // 36, 96 WhiteFg // 37, 97 BrightFg Color = ((1 << 3) << shiftFg) | flagFg // -> 90 // BrownFg represents brown foreground color. // // Deprecated: use YellowFg instead, following specifications BrownFg = YellowFg )
Foreground colors and related formats
const ( BlackBg Color = (iota << shiftBg) | flagBg // 40, 100 RedBg // 41, 101 GreenBg // 42, 102 YellowBg // 43, 103 BlueBg // 44, 104 MagentaBg // 45, 105 CyanBg // 46, 106 WhiteBg // 47, 107 BrightBg Color = ((1 << 3) << shiftBg) | flagBg // -> 100 // BrownBg represents brown foreground color. // // Deprecated: use YellowBg instead, following specifications BrownBg = YellowBg )
Background colors and related formats
type Value ¶
type Value interface { // String returns string with colors. If there are any color // or format the string will be terminated with \033[0m fmt.Stringer // Format implements fmt.Formatter interface fmt.Formatter // Color returns value's color Color() Color // Value returns value's value (welcome to the tautology club) Value() interface{} // Bleach returns copy of original value without colors // // Deprecated: use Reset instead. Bleach() Value // Reset colors and formats Reset() Value // // Formats // // // Bold or increased intensity (1). Bold() Value // Faint, decreased intensity, reset the Bold (2). Faint() Value // // DoublyUnderline or Bold off, double-underline // per ECMA-48 (21). It depends. DoublyUnderline() Value // Fraktur, rarely supported (20). Fraktur() Value // // Italic, not widely supported, sometimes // treated as inverse (3). Italic() Value // Underline (4). Underline() Value // // SlowBlink, blinking less than 150 // per minute (5). SlowBlink() Value // RapidBlink, blinking 150+ per minute, // not widely supported (6). RapidBlink() Value // Blink is alias for the SlowBlink. Blink() Value // // Reverse video, swap foreground and // background colors (7). Reverse() Value // Inverse is alias for the Reverse Inverse() Value // // Conceal, hidden, not widely supported (8). Conceal() Value // Hidden is alias for the Conceal Hidden() Value // // CrossedOut, characters legible, but // marked for deletion (9). CrossedOut() Value // StrikeThrough is alias for the CrossedOut. StrikeThrough() Value // // Framed (51). Framed() Value // Encircled (52). Encircled() Value // // Overlined (53). Overlined() Value // // Foreground colors // // // Black foreground color (30) Black() Value // Red foreground color (31) Red() Value // Green foreground color (32) Green() Value // Yellow foreground color (33) Yellow() Value // Brown foreground color (33) // // Deprecated: use Yellow instead, following specification Brown() Value // Blue foreground color (34) Blue() Value // Magenta foreground color (35) Magenta() Value // Cyan foreground color (36) Cyan() Value // White foreground color (37) White() Value // // Bright foreground colors // // BrightBlack foreground color (90) BrightBlack() Value // BrightRed foreground color (91) BrightRed() Value // BrightGreen foreground color (92) BrightGreen() Value // BrightYellow foreground color (93) BrightYellow() Value // BrightBlue foreground color (94) BrightBlue() Value // BrightMagenta foreground color (95) BrightMagenta() Value // BrightCyan foreground color (96) BrightCyan() Value // BrightWhite foreground color (97) BrightWhite() Value // // Other // // Index of pre-defined 8-bit foreground color // from 0 to 255 (38;5;n). // // 0- 7: standard colors (as in ESC [ 30–37 m) // 8- 15: high intensity colors (as in ESC [ 90–97 m) // 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) // 232-255: grayscale from black to white in 24 steps // Index(n uint8) Value // Gray from 0 to 24. Gray(n uint8) Value // // Background colors // // // BgBlack background color (40) BgBlack() Value // BgRed background color (41) BgRed() Value // BgGreen background color (42) BgGreen() Value // BgYellow background color (43) BgYellow() Value // BgBrown background color (43) // // Deprecated: use BgYellow instead, following specification BgBrown() Value // BgBlue background color (44) BgBlue() Value // BgMagenta background color (45) BgMagenta() Value // BgCyan background color (46) BgCyan() Value // BgWhite background color (47) BgWhite() Value // // Bright background colors // // BgBrightBlack background color (100) BgBrightBlack() Value // BgBrightRed background color (101) BgBrightRed() Value // BgBrightGreen background color (102) BgBrightGreen() Value // BgBrightYellow background color (103) BgBrightYellow() Value // BgBrightBlue background color (104) BgBrightBlue() Value // BgBrightMagenta background color (105) BgBrightMagenta() Value // BgBrightCyan background color (106) BgBrightCyan() Value // BgBrightWhite background color (107) BgBrightWhite() Value // // Other // // BgIndex of 8-bit pre-defined background color // from 0 to 255 (48;5;n). // // 0- 7: standard colors (as in ESC [ 40–47 m) // 8- 15: high intensity colors (as in ESC [100–107 m) // 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) // 232-255: grayscale from black to white in 24 steps // BgIndex(n uint8) Value // BgGray from 0 to 24. BgGray(n uint8) Value // // Special // // Colorize removes existing colors and // formats of the argument and applies given. Colorize(color Color) Value // contains filtered or unexported methods }
A Value represents any printable value with it's color
func BgBrightMagenta ¶
func BgBrightMagenta(arg interface{}) Value
BgBrightMagenta background color (105)
func BgBrightYellow ¶
func BgBrightYellow(arg interface{}) Value
BgBrightYellow background color (103)
func BgBrown ¶
func BgBrown(arg interface{}) Value
BgBrown background color (43)
Deprecated: use BgYellow instead, following specification
func BgIndex ¶
BgIndex of 8-bit pre-defined background color from 0 to 255 (48;5;n).
0- 7: standard colors (as in ESC [ 40–47 m) 8- 15: high intensity colors (as in ESC [100–107 m) 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) 232-255: grayscale from black to white in 24 steps
func Bold ¶
func Bold(arg interface{}) Value
Bold or increased intensity (1).
Example ¶
Code:
fmt.Println("value:", Bold(Green(99)))
value: �[1;32m99�[0m
func Brown ¶
func Brown(arg interface{}) Value
Brown foreground color (33)
Deprecated: use Yellow instead, following specification
func Colorize ¶
Colorize wraps given value into Value with given colors. For example
s := Colorize("some", BlueFg|GreenBg|BoldFm)
returns a Value with blue foreground, green background and bold. Unlike functions like Red/BgBlue/Bold etc. This function clears all previous colors and formats. Thus
s := Colorize(Red("some"), BgBlue)
clears red color from value
func Conceal ¶
func Conceal(arg interface{}) Value
Conceal hides text, preserving an ability to select the text and copy it. It is not widely supported (8).
func CrossedOut ¶
func CrossedOut(arg interface{}) Value
CrossedOut makes characters legible, but marked for deletion (9).
func DoublyUnderline ¶
func DoublyUnderline(arg interface{}) Value
DoublyUnderline or Bold off, double-underline per ECMA-48 (21).
func Faint ¶
func Faint(arg interface{}) Value
Faint decreases intensity (2). The Faint rejects the Bold.
func Index ¶
Index of pre-defined 8-bit foreground color from 0 to 255 (38;5;n).
0- 7: standard colors (as in ESC [ 30–37 m) 8- 15: high intensity colors (as in ESC [ 90–97 m) 16-231: 6 × 6 × 6 cube (216 colors): 16 + 36 × r + 6 × g + b (0 ≤ r, g, b ≤ 5) 232-255: grayscale from black to white in 24 steps
func Italic ¶
func Italic(arg interface{}) Value
Italic is not widely supported, sometimes treated as inverse (3).
func RapidBlink ¶
func RapidBlink(arg interface{}) Value
RapidBlink makes text blink 150+ per minute. It is not widely supported (6).
func Red ¶
func Red(arg interface{}) Value
Red foreground color (31)
Example ¶
Code:
fmt.Println("value exceeds min-threshold:", Red(3.14))
value exceeds min-threshold: �[31m3.14�[0m
func Reset ¶
func Reset(arg interface{}) Value
Reset wraps given argument returning Value without formats and colors.
func Reverse ¶
func Reverse(arg interface{}) Value
Reverse video, swap foreground and background colors (7).
func SlowBlink ¶
func SlowBlink(arg interface{}) Value
SlowBlink makes text blink less than 150 per minute (5).
func StrikeThrough ¶
func StrikeThrough(arg interface{}) Value
StrikeThrough is alias for the CrossedOut.