Categorygithub.com/logrusorgru/aurora/v4
modulepackage
4.0.0
Repository: https://github.com/logrusorgru/aurora.git
Documentation: pkg.go.dev

# README

Aurora

go.dev reference Unlicense Build Status Coverage Status GoReportCard

Ultimate ANSI colors for Golang. The package supports Printf/Sprintf etc.

aurora logo

TOC

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).

The latest version
go get -u github.com/logrusorgru/aurora/v4

With hyperlinks.

Test

go test -cover -race github.com/logrusorgru/aurora/v4

Replace the import path with your, if it's different.

Usage

Simple

package main

import (
	"fmt"

	"github.com/logrusorgru/aurora/v4"
)

func main() {
	fmt.Println("Hello,", aurora.Magenta("Aurora"))
	fmt.Println(aurora.Bold(aurora.Cyan("Cya!")))
}

simple png

Printf

package main

import (
	"fmt"

	"github.com/logrusorgru/aurora/v4"
)

func main() {
	fmt.Printf("Got it %d times\n", aurora.Green(1240))
	fmt.Printf("PI is %+1.2e\n", aurora.Cyan(3.14))
}

printf png

aurora.Sprintf

package main

import (
	"fmt"

	"github.com/logrusorgru/aurora/v4"
)

func main() {
	fmt.Println(aurora.Sprintf(aurora.Magenta("Got it %d times"), aurora.Green(1240)))
}

sprintf png

Enable/Disable colors

package main

import (
	"fmt"
	"flag"

	"github.com/logrusorgru/aurora/v4"
)

// colorizer
var au *aurora.Aurora

var colors = flag.Bool("colors", false, "enable or disable colors")

func init() {
	flag.Parse()
	au = aurora.New(WithColors(*colors))
}

func main() {
	// use colorizer
	fmt.Println(au.Green("Hello"))
}

Without flags: disable png

With -colors flag: enable png

Hyperlinks, default colorizer, and configurations

Hyperlinks feature description.

Add a red hyperlinks with text "Example" that is referencing to http://example.com.

package main

import (
	"flag"
	"fmt"

	"github.com/logrusorgru/aurora/v4"
)

func main() {
	var conf = aurora.NewConfig()
	conf.AddFlags(flag.CommandLine, "prefix.")
	flag.Parse()

	aurora.DefaultColorizer = aurora.New(conf.Options()...) // set global

	fmt.Println(aurora.Red("Example").Hyperlink("http://example.com/"))
}

Depending flags: depending flags png depending flags gif

Chains

The following samples are equal

x := aurora.BgMagenta(aurora.Bold(aurora.Red("x")))
x := aurora.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(aurora.Colorize("Greeting", getColors()))
}

Less complicated example

x := aurora.Colorize("Greeting", GreenFg|GrayBg|BoldFm)

Unlike other color functions and methods (such as Red/BgBlue etc) a Colorize clears previous colors

x := aurora.Red("x").Colorize(BgGreen) // will be with green background only

Grayscale

fmt.Println("  ",
	aurora.Gray(1-1, " 00-23 ").BgGray(24-1),
	aurora.Gray(4-1, " 03-19 ").BgGray(20-1),
	aurora.Gray(8-1, " 07-15 ").BgGray(16-1),
	aurora.Gray(12-1, " 11-11 ").BgGray(12-1),
	aurora.Gray(16-1, " 15-07 ").BgGray(8-1),
	aurora.Gray(20-1, " 19-03 ").BgGray(4-1),
	aurora.Gray(24-1, " 23-00 ").BgGray(1-1),
)

grayscale png

8-bit colors

Methods Index and BgIndex implements 8-bit colors.

Index/BgIndexMeaningForegroundBackground
0- 7standard colors30- 3740- 47
8- 15bright colors90- 97100-107
16-231216 colors38;5;n48;5;n
232-25524 grayscale38;5;n48;5;n

Example

package main

import (
	"fmt"
	"github.com/logrusorgru/aurora"
)

func main() {
	for i := uint8(16); i <= 231; i++ {
		fmt.Println(i, aurora.Index(i, "pew-pew"), aurora.BgIndex(i, "pew-pew"))
	}
}

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

linux png
white png

Standard and bright colors

linux black standard png linux white standard png

Formats are likely supported

formats supported gif

Formats are likely unsupported

formats rarely supported png

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() {
	var (
		r = aurora.Red("red")
		i int
	)
	fmt.Printf("%T %p\n", r, aurora.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-2022 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.

# Functions

BgBlack background color (40).
BgBlue background color (44).
BgBrightBlack background color (100).
BgBrightBlue background color (104).
BgBrightCyan background color (106).
BgBrightGreen background color (102).
BgBrightMagenta background color (105).
BgBrightRed background color (101).
BgBrightWhite background color (107).
BgBrightYellow background color (103).
BgCyan background color (46).
BgGray from 0 to 24.
BgGreen background color (42).
BgIndex of 8-bit pre-defined background color from 0 to 255 (48;5;n).
BgMagenta background color (45).
BgRed background color (41).
BgWhite background color (47).
BgYellow background color (43).
Black foreground color (30).
Blink is alias for the SlowBlink.
Blue foreground color (34).
Bold or increased intensity (1).
BrightBlack foreground color (90).
BrightBlue foreground color (94).
BrightCyan foreground color (96).
BrightGreen foreground color (92).
BrightMagenta foreground color (95).
BrightRed foreground color (91).
BrightWhite foreground color (97).
BrightYellow foreground color (93).
Clear wraps given argument returning Value without formats and colors.
Colorize wraps given value into Value with given colors.
Conceal hides text, preserving an ability to select the text and copy it.
CrossedOut makes characters legible, but marked for deletion (9).
Cyan foreground color (36).
DoublyUnderline or Bold off, double-underline per ECMA-48 (21).
Encircled (52).
Faint decreases intensity (2).
Fraktur is rarely supported (20).
Framed (51).
Gray from 0 to 24.
Green foreground color (32).
Hidden is alias for the Conceal.
Hyperlink with given target and parameters.
HyperlinkEscape escapes all symbols of given string out of [32; 126] range using URL-encoding.
HyperlinkID returns list of HyperlinkParams that contains only id parameter with given value of the id parameter.
HyperlinkParams of the argument if it's a Value.
HyperlinkTarget of the argument if it's a Value.
HyperlinkUnescape reverts a string escaped by the HyperlinkEscape.
Index of pre-defined 8-bit foreground color from 0 to 255 (38;5;n).
Inverse is alias for the Reverse.
IsValidHyperlinkParam returns true for given string, if the string is valid hyperlink target (see IsValidHyperlinkTarget) and doesn't contains ':', ';' and '='.
IsValidHyperlinkTarget returns true if the target contains symbols only in 32-126 ASCII range.
Italic is not widely supported, sometimes treated as inverse (3).
Magenta foreground color (35).
New returns new colorizer by given Options.
NewConfig returns new default Config.
Overlined (53).
RapidBlink makes text blink 150+ per minute.
Red foreground color (31).
Reset wraps given argument returning Value without formats, colors and links.
Reverse video, swap foreground and background colors (7).
SlowBlink makes text blink less than 150 per minute (5).
Sprintf allows to use Value as format.
StrikeThrough is alias for the CrossedOut.
Underline (4).
White foreground color (37).
WithColors is an Option that used to enable or disable colors.
WithHyperlinks is an Option that used to enable or disable links.
Yellow foreground color (33).

# Constants

40, 100.
30, 90.
alias to SlowBlinkFm.
44, 104.
34, 94.
1.
-> 100.
-> 90.
8.
9.
46, 106.
36, 96.
21 or bold off for some systems.
52.
2.
20.
51.
42, 102.
32, 92.
alias to ConcealFm.
hyperlink id parameter key.
alias to ReverseFm.
3.
45, 105.
35, 95.
53.
6.
41, 101.
31, 91.
7.
5.
alias to CrossedOutFm.
4.
47, 107.
37, 97.
43, 103.
33, 93.

# Variables

DefaultColorizer is global colorizer that used for package root color methods.

# Structs

Config represents configurations of a colorizer.
The HyperlinkParam represents a hyperlink parameter.
A Value represents any printable value with or without colors, formats and a link.

# Interfaces

The Colored interface represents a value with a Color.

# Type aliases

A Color type is a color.
ColorIndex is index of pre-defined 8-bit foreground or background colors from 0 to 255 (38;5;n).
GrayIndex from 0 to 23.
An Option function.