aurora

package module
v4.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 8, 2022 License: Unlicense Imports: 5 Imported by: 89

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 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/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

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.

Documentation

Overview

Package aurora implements ANSI-colors

Example (Printf)
fmt.Printf("%d %s", Blue(100), BgBlue("cats"))
Output:

�[34m100�[0m �[44mcats�[0m

Index

Examples

Constants

View Source
const (
	HyperlinkIDKey = "id" // hyperlink id parameter key
)

Hyperlinks related constants

Variables

View Source
var DefaultColorizer = New(WithColors(true), WithHyperlinks(true))

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

Functions

func HyperlinkEscape

func HyperlinkEscape(s string) string

HyperlinkEscape escapes all symbols of given string out of [32; 126] range using URL-encoding. Used to escape a hyperlink target.

func HyperlinkTarget

func HyperlinkTarget(arg interface{}) (target string)

HyperlinkTarget of the argument if it's a Value.

func HyperlinkUnescape

func HyperlinkUnescape(s string) (raw string, err error)

HyperlinkUnescape reverts a string escaped by the HyperlinkEscape.

func IsValidHyperlinkParam

func IsValidHyperlinkParam(param string) (valid bool)

IsValidHyperlinkParam returns true for given string, if the string is valid hyperlink target (see IsValidHyperlinkTarget) and doesn't contains ':', ';' and '='.

func IsValidHyperlinkTarget

func IsValidHyperlinkTarget(target string) (valid bool)

IsValidHyperlinkTarget returns true if the target contains symbols only in 32-126 ASCII range. All symbols outside this range should be URL-escaped.

func Sprintf

func Sprintf(format interface{}, args ...interface{}) string

Sprintf allows to use Value as format. For example

var 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

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

It applies own configurations to all given Values.

Example
fmt.Print(
	Sprintf(
		Blue("we've got %d cats, but want %d"), // <- blue format
		Cyan(5),
		Bold(Magenta(25)),
	),
)
Output:

�[34mwe've got �[0;36m5�[0;34m cats, but want �[0;1;35m25�[0;34m�[0m

Types

type Aurora

type Aurora struct {
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) (a *Aurora)

New returns new colorizer by given Options.

Example (Colors)
var a = New()
fmt.Println(a.Red("Red"))
Output:

�[31mRed�[0m
Example (No_colors)
var a = New(WithColors(false), WithHyperlinks(false))
fmt.Println(a.Red("Not red"))
Output:

Not red

func (*Aurora) BgBlack

func (a *Aurora) BgBlack(arg interface{}) Value

Background colors.

BgBlack background color (40).

func (*Aurora) BgBlue

func (a *Aurora) BgBlue(arg interface{}) Value

BgBlue background color (44).

func (*Aurora) BgBrightBlack

func (a *Aurora) BgBrightBlack(arg interface{}) Value

Bright background colors.

BgBrightBlack background color (100).

func (*Aurora) BgBrightBlue

func (a *Aurora) BgBrightBlue(arg interface{}) Value

BgBrightBlue background color (104).

func (*Aurora) BgBrightCyan

func (a *Aurora) BgBrightCyan(arg interface{}) Value

BgBrightCyan background color (106).

func (*Aurora) BgBrightGreen

func (a *Aurora) BgBrightGreen(arg interface{}) Value

BgBrightGreen background color (102).

func (*Aurora) BgBrightMagenta

func (a *Aurora) BgBrightMagenta(arg interface{}) Value

BgBrightMagenta background color (105).

func (*Aurora) BgBrightRed

func (a *Aurora) BgBrightRed(arg interface{}) Value

BgBrightRed background color (101).

func (*Aurora) BgBrightWhite

func (a *Aurora) BgBrightWhite(arg interface{}) Value

BgBrightWhite background color (107).

func (*Aurora) BgBrightYellow

func (a *Aurora) BgBrightYellow(arg interface{}) Value

BgBrightYellow background color (103).

func (*Aurora) BgCyan

func (a *Aurora) BgCyan(arg interface{}) Value

BgCyan background color (46).

func (*Aurora) BgGray

func (a *Aurora) BgGray(n GrayIndex, arg interface{}) Value

BgGray from 0 to 23.

func (*Aurora) BgGreen

func (a *Aurora) BgGreen(arg interface{}) Value

BgGreen background color (42).

func (*Aurora) BgIndex

func (a *Aurora) BgIndex(n ColorIndex, arg interface{}) Value

Other background colors.

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 (*Aurora) BgMagenta

func (a *Aurora) BgMagenta(arg interface{}) Value

BgMagenta background color (45).

func (*Aurora) BgRed

func (a *Aurora) BgRed(arg interface{}) Value

BgRed background color (41).

func (*Aurora) BgWhite

func (a *Aurora) BgWhite(arg interface{}) Value

BgWhite background color (47).

func (*Aurora) BgYellow

func (a *Aurora) BgYellow(arg interface{}) Value

BgYellow background color (43).

func (*Aurora) Black

func (a *Aurora) Black(arg interface{}) Value

Foreground colors

Black foreground color (30).

func (a *Aurora) Blink(arg interface{}) Value

Blink is alias for the SlowBlink.

func (*Aurora) Blue

func (a *Aurora) Blue(arg interface{}) Value

Blue foreground color (34).

func (*Aurora) Bold

func (a *Aurora) Bold(arg interface{}) Value

Formats

Bold or increased intensity (1).

func (*Aurora) BrightBlack

func (a *Aurora) BrightBlack(arg interface{}) Value

Bright foreground colors.

BrightBlack foreground color (90).

func (*Aurora) BrightBlue

func (a *Aurora) BrightBlue(arg interface{}) Value

BrightBlue foreground color (94).

func (*Aurora) BrightCyan

func (a *Aurora) BrightCyan(arg interface{}) Value

BrightCyan foreground color (96).

func (*Aurora) BrightGreen

func (a *Aurora) BrightGreen(arg interface{}) Value

BrightGreen foreground color (92).

func (*Aurora) BrightMagenta

func (a *Aurora) BrightMagenta(arg interface{}) Value

BrightMagenta foreground color (95).

func (*Aurora) BrightRed

func (a *Aurora) BrightRed(arg interface{}) Value

BrightRed foreground color (91).

func (*Aurora) BrightWhite

func (a *Aurora) BrightWhite(arg interface{}) Value

BrightWhite foreground color (97).

func (*Aurora) BrightYellow

func (a *Aurora) BrightYellow(arg interface{}) Value

BrightYellow foreground color (93).

func (*Aurora) Clear

func (a *Aurora) Clear(arg interface{}) Value

Clear wraps given argument returning Value without formats and colors. But preserving links.

func (*Aurora) Colorize

func (a *Aurora) Colorize(arg interface{}, color Color) Value

Special color functions.

Colorize removes existing colors and formats of the argument and applies given.

func (*Aurora) Conceal

func (a *Aurora) Conceal(arg interface{}) Value

Conceal, hidden, not widely supported (8).

func (*Aurora) Config

func (a *Aurora) Config() Config

Config of the colorizer. It returns copy of the configurations.

func (*Aurora) CrossedOut

func (a *Aurora) CrossedOut(arg interface{}) Value

CrossedOut, characters legible, but marked for deletion (9).

func (*Aurora) Cyan

func (a *Aurora) Cyan(arg interface{}) Value

Cyan foreground color (36).

func (*Aurora) DoublyUnderline

func (a *Aurora) DoublyUnderline(arg interface{}) Value

DoublyUnderline or Bold off, double-underline per ECMA-48 (21).

func (*Aurora) Encircled

func (a *Aurora) Encircled(arg interface{}) Value

Encircled (52).

func (*Aurora) Faint

func (a *Aurora) Faint(arg interface{}) Value

Faint, decreased intensity (2).

func (*Aurora) Fraktur

func (a *Aurora) Fraktur(arg interface{}) Value

Fraktur, rarely supported (20).

func (*Aurora) Framed

func (a *Aurora) Framed(arg interface{}) Value

Framed (51).

func (*Aurora) Gray

func (a *Aurora) Gray(n GrayIndex, arg interface{}) Value

Gray from 0 to 23.

func (*Aurora) Green

func (a *Aurora) Green(arg interface{}) Value

Green foreground color (32).

func (*Aurora) Hidden

func (a *Aurora) Hidden(arg interface{}) Value

Hidden is alias for the Conceal.

func (a *Aurora) Hyperlink(arg interface{}, target string,
	params ...HyperlinkParam) Value

Hyperlinks feature

Hyperlink with given target and parameters. If hyperlinks feature is disabled, then the 'arg' argument dropped and the 'target' used instead inheriting all colors and format from the 'arg' (if it's a Colored).

See https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda for details about the hyperlinks feature.

The Hyperlink doesn't escape the target and the params. They should be checked and escaped before.

See also HyperlinkID function.

For a simple example

au.Hyperlink("Example", "http://example.com")

and an example with ID

au.Hyperlink("Example", "http://example.com", aurora.HyperlinkID("10"))

func (*Aurora) HyperlinkParams

func (a *Aurora) HyperlinkParams(arg interface{}) (params []HyperlinkParam)

HyperlinkParams of the argument if it's a Value.

func (*Aurora) HyperlinkTarget

func (a *Aurora) HyperlinkTarget(arg interface{}) (target string)

HyperlinkTarget of the argument if it's a Value.

func (*Aurora) Index

func (a *Aurora) Index(n ColorIndex, arg interface{}) Value

Other colors.

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 (*Aurora) Inverse

func (a *Aurora) Inverse(arg interface{}) Value

Inverse is alias for the Reverse

func (*Aurora) Italic

func (a *Aurora) Italic(arg interface{}) Value

Italic, not widely supported, sometimes treated as inverse (3).

func (*Aurora) Magenta

func (a *Aurora) Magenta(arg interface{}) Value

Magenta foreground color (35).

func (*Aurora) Overlined

func (a *Aurora) Overlined(arg interface{}) Value

Overlined (53).

func (a *Aurora) RapidBlink(arg interface{}) Value

RapidBlink, blinking 150+ per minute, not widely supported (6).

func (*Aurora) Red

func (a *Aurora) Red(arg interface{}) Value

Red foreground color (31).

func (*Aurora) Reset

func (a *Aurora) Reset(arg interface{}) Value

Reset wraps given argument returning Value without formats, colors and links.

func (*Aurora) Reverse

func (a *Aurora) Reverse(arg interface{}) Value

Reverse video, swap foreground and background colors (7).

func (a *Aurora) SlowBlink(arg interface{}) Value

SlowBlink, blinking less than 150 per minute (5).

func (*Aurora) Sprintf

func (a *Aurora) Sprintf(format interface{}, args ...interface{}) string

Sprintf allows to use Value as format. For example

var 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

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

It applies own configurations to all given Values.

func (*Aurora) StrikeThrough

func (a *Aurora) StrikeThrough(arg interface{}) Value

StrikeThrough is alias for the CrossedOut.

func (*Aurora) Underline

func (a *Aurora) Underline(arg interface{}) Value

Underline (4).

func (*Aurora) White

func (a *Aurora) White(arg interface{}) Value

White foreground color (37).

func (*Aurora) Yellow

func (a *Aurora) Yellow(arg interface{}) Value

Yellow foreground color (33).

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

)

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

)

Background colors and related formats

func (Color) BgBlack

func (c Color) BgBlack() Color

Background colors

BgBlack background color (40)

func (Color) BgBlue

func (c Color) BgBlue() Color

BgBlue background color (44)

func (Color) BgBrightBlack

func (c Color) BgBrightBlack() Color

Bright background colors

BgBrightBlack background color (100)

func (Color) BgBrightBlue

func (c Color) BgBrightBlue() Color

BgBrightBlue background color (104)

func (Color) BgBrightCyan

func (c Color) BgBrightCyan() Color

BgBrightCyan background color (106)

func (Color) BgBrightGreen

func (c Color) BgBrightGreen() Color

BgBrightGreen background color (102)

func (Color) BgBrightMagenta

func (c Color) BgBrightMagenta() Color

BgBrightMagenta background color (105)

func (Color) BgBrightRed

func (c Color) BgBrightRed() Color

BgBrightRed background color (101)

func (Color) BgBrightWhite

func (c Color) BgBrightWhite() Color

BgBrightWhite background color (107)

func (Color) BgBrightYellow

func (c Color) BgBrightYellow() Color

BgBrightYellow background color (103)

func (Color) BgCyan

func (c Color) BgCyan() Color

BgCyan background color (46)

func (Color) BgGray

func (c Color) BgGray(n GrayIndex) Color

BgGray from 0 to 23.

func (Color) BgGreen

func (c Color) BgGreen() Color

BgGreen background color (42)

func (Color) BgIndex

func (c Color) BgIndex(n ColorIndex) Color

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

func (Color) BgMagenta

func (c Color) BgMagenta() Color

BgMagenta background color (45)

func (Color) BgRed

func (c Color) BgRed() Color

BgRed background color (41)

func (Color) BgWhite

func (c Color) BgWhite() Color

BgWhite background color (47)

func (Color) BgYellow

func (c Color) BgYellow() Color

BgYellow background color (43)

func (Color) Black

func (c Color) Black() Color

Foreground colors

Black foreground color (30)

func (c Color) Blink() Color

Blink is alias for the SlowBlink.

func (Color) Blue

func (c Color) Blue() Color

Blue foreground color (34)

func (Color) Bold

func (c Color) Bold() Color

Bold or increased intensity (1).

func (Color) BrightBlack

func (c Color) BrightBlack() Color

Bright foreground colors

BrightBlack foreground color (90)

func (Color) BrightBlue

func (c Color) BrightBlue() Color

BrightBlue foreground color (94)

func (Color) BrightCyan

func (c Color) BrightCyan() Color

BrightCyan foreground color (96)

func (Color) BrightGreen

func (c Color) BrightGreen() Color

BrightGreen foreground color (92)

func (Color) BrightMagenta

func (c Color) BrightMagenta() Color

BrightMagenta foreground color (95)

func (Color) BrightRed

func (c Color) BrightRed() Color

BrightRed foreground color (91)

func (Color) BrightWhite

func (c Color) BrightWhite() Color

BrightWhite foreground color (97)

func (Color) BrightYellow

func (c Color) BrightYellow() Color

BrightYellow foreground color (93)

func (Color) Conceal

func (c Color) Conceal() Color

Conceal, hidden, not widely supported (8).

func (Color) CrossedOut

func (c Color) CrossedOut() Color

CrossedOut, characters legible, but marked for deletion (9).

func (Color) Cyan

func (c Color) Cyan() Color

Cyan foreground color (36)

func (Color) DoublyUnderline

func (c Color) DoublyUnderline() Color

DoublyUnderline or Bold off, double-underline per ECMA-48 (21).

func (Color) Encircled

func (c Color) Encircled() Color

Encircled (52).

func (Color) Faint

func (c Color) Faint() Color

Faint, decreased intensity (2).

func (Color) Fraktur

func (c Color) Fraktur() Color

Fraktur, rarely supported (20).

func (Color) Framed

func (c Color) Framed() Color

Framed (51).

func (Color) Gray

func (c Color) Gray(n GrayIndex) Color

Gray from 0 to 23.

func (Color) Green

func (c Color) Green() Color

Green foreground color (32)

func (Color) Hidden

func (c Color) Hidden() Color

Hidden is alias for the Conceal

func (Color) Index

func (c Color) Index(ci ColorIndex) Color

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

func (Color) Inverse

func (c Color) Inverse() Color

Inverse is alias for the Reverse

func (Color) Italic

func (c Color) Italic() Color

Italic, not widely supported, sometimes treated as inverse (3).

func (Color) Magenta

func (c Color) Magenta() Color

Magenta foreground color (35)

func (Color) Nos

func (c Color) Nos(zero bool) string

Nos returns string like 1;7;31;45. It may be an empty string for empty color. If the zero is true, then the string is prepended with 0;

func (Color) Overlined

func (c Color) Overlined() Color

Overlined (53).

func (c Color) RapidBlink() Color

RapidBlink, blinking 150+ per minute, not widely supported (6).

func (Color) Red

func (c Color) Red() Color

Red foreground color (31)

func (Color) Reset

func (c Color) Reset() Color

Reset returns Color without a color and formats.

func (Color) Reverse

func (c Color) Reverse() Color

Reverse video, swap foreground and background colors (7).

func (c Color) SlowBlink() Color

SlowBlink, blinking less than 150 per minute (5).

func (Color) StrikeThrough

func (c Color) StrikeThrough() Color

StrikeThrough is alias for the CrossedOut.

func (Color) Underline

func (c Color) Underline() Color

Underline (4).

func (Color) White

func (c Color) White() Color

White foreground color (37)

func (Color) Yellow

func (c Color) Yellow() Color

Yellow foreground color (33)

type ColorIndex

type ColorIndex uint8

ColorIndex is index of pre-defined 8-bit foreground or background colors 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

type Colored

type Colored interface {
	Color() Color // color of the value
}

The Colored interface represents a value with a Color.

type Config

type Config struct {
	// Colors feature. Enable colors if true.
	Colors bool `json:"colors" yaml:"colors" toml:"colors" mapstructure:"colors"`
	// Hyperlinks feature. Enable hyperlinks if true.
	Hyperlinks bool `json:"hyperlinks" yaml:"hyperlinks" toml:"hyperlinks" mapstructure:"hyperlinks"`
}

Config represents configurations of a colorizer.

func NewConfig

func NewConfig() (conf Config)

NewConfig returns new default Config.

func (*Config) AddFlags

func (c *Config) AddFlags(fset *flag.FlagSet, prefix string)

AddFlags to given *flag.FlagSet. The prefix used as prefix for flags. It may be used to parse commandline flags. For example

var conf Config
conf.AddFlags(flag.CommandLine, "colors.")
flag.Parse()

for a main package, and use with flags commandline flags,

go run main.go -colors.colors -colors.hyperlinks

to enable or disable features. A colorizer can be created, for example,

var colorizer = New(conf.Options()...)

func (*Config) Apply

func (c *Config) Apply(opts ...Option)

Apply given options for the Config.

func (*Config) Options

func (c *Config) Options() (opts []Option)

Options by the Config.

type GrayIndex

type GrayIndex uint8

GrayIndex from 0 to 23.

type HyperlinkParam

type HyperlinkParam struct {
	Key   string // parameter name
	Value string // parameter value
}

The HyperlinkParam represents a hyperlink parameter.

func HyperlinkID

func HyperlinkID(id string) HyperlinkParam

HyperlinkID returns list of HyperlinkParams that contains only id parameter with given value of the id parameter.

func HyperlinkParams

func HyperlinkParams(arg interface{}) (params []HyperlinkParam)

HyperlinkParams of the argument if it's a Value.

func (HyperlinkParam) String

func (hp HyperlinkParam) String() string

String represents the HyperlinkParam as string, e.g. in key=value form.

type Option

type Option func(*Config)

An Option function.

func WithColors

func WithColors(t bool) Option

WithColors is an Option that used to enable or disable colors.

func WithHyperlinks(t bool) Option

WithHyperlinks is an Option that used to enable or disable links.

type Value

type Value struct {
	// contains filtered or unexported fields
}

A Value represents any printable value with or without colors, formats and a link.

func BgBlack

func BgBlack(arg interface{}) Value

BgBlack background color (40)

func BgBlue

func BgBlue(arg interface{}) Value

BgBlue background color (44)

func BgBrightBlack

func BgBrightBlack(arg interface{}) Value

BgBrightBlack background color (100)

func BgBrightBlue

func BgBrightBlue(arg interface{}) Value

BgBrightBlue background color (104)

func BgBrightCyan

func BgBrightCyan(arg interface{}) Value

BgBrightCyan background color (106)

func BgBrightGreen

func BgBrightGreen(arg interface{}) Value

BgBrightGreen background color (102)

func BgBrightMagenta

func BgBrightMagenta(arg interface{}) Value

BgBrightMagenta background color (105)

func BgBrightRed

func BgBrightRed(arg interface{}) Value

BgBrightRed background color (101)

func BgBrightWhite

func BgBrightWhite(arg interface{}) Value

BgBrightWhite background color (107)

func BgBrightYellow

func BgBrightYellow(arg interface{}) Value

BgBrightYellow background color (103)

func BgCyan

func BgCyan(arg interface{}) Value

BgCyan background color (46)

func BgGray

func BgGray(n GrayIndex, arg interface{}) Value

BgGray from 0 to 24.

func BgGreen

func BgGreen(arg interface{}) Value

BgGreen background color (42)

func BgIndex

func BgIndex(n ColorIndex, arg interface{}) Value

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 BgMagenta

func BgMagenta(arg interface{}) Value

BgMagenta background color (45)

func BgRed

func BgRed(arg interface{}) Value

BgRed background color (41)

func BgWhite

func BgWhite(arg interface{}) Value

BgWhite background color (47)

func BgYellow

func BgYellow(arg interface{}) Value

BgYellow background color (43)

func Black

func Black(arg interface{}) Value

Black foreground color (30)

func Blink(arg interface{}) Value

Blink is alias for the SlowBlink.

func Blue

func Blue(arg interface{}) Value

Blue foreground color (34)

func Bold

func Bold(arg interface{}) Value

Bold or increased intensity (1).

Example
fmt.Println("value:", Bold(Green(99)))
Output:

value: �[1;32m99�[0m

func BrightBlack

func BrightBlack(arg interface{}) Value

BrightBlack foreground color (90)

func BrightBlue

func BrightBlue(arg interface{}) Value

BrightBlue foreground color (94)

func BrightCyan

func BrightCyan(arg interface{}) Value

BrightCyan foreground color (96)

func BrightGreen

func BrightGreen(arg interface{}) Value

BrightGreen foreground color (92)

func BrightMagenta

func BrightMagenta(arg interface{}) Value

BrightMagenta foreground color (95)

func BrightRed

func BrightRed(arg interface{}) Value

BrightRed foreground color (91)

func BrightWhite

func BrightWhite(arg interface{}) Value

BrightWhite foreground color (97)

func BrightYellow

func BrightYellow(arg interface{}) Value

BrightYellow foreground color (93)

func Clear

func Clear(arg interface{}) Value

Clear wraps given argument returning Value without formats and colors. But preserving links.

func Colorize

func Colorize(arg interface{}, color Color) Value

Colorize wraps given value into Value with given colors. For example

var 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

var 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 Cyan

func Cyan(arg interface{}) Value

Cyan foreground color (36)

func DoublyUnderline

func DoublyUnderline(arg interface{}) Value

DoublyUnderline or Bold off, double-underline per ECMA-48 (21).

func Encircled

func Encircled(arg interface{}) Value

Encircled (52).

func Faint

func Faint(arg interface{}) Value

Faint decreases intensity (2). The Faint rejects the Bold.

func Fraktur

func Fraktur(arg interface{}) Value

Fraktur is rarely supported (20).

func Framed

func Framed(arg interface{}) Value

Framed (51).

func Gray

func Gray(n GrayIndex, arg interface{}) Value

Gray from 0 to 24.

func Green

func Green(arg interface{}) Value

Green foreground color (32)

func Hidden

func Hidden(arg interface{}) Value

Hidden is alias for the Conceal

func Hyperlink(arg interface{}, target string, params ...HyperlinkParam) Value

Hyperlink with given target and parameters. If hyperlinks feature is disabled, then the 'arg' argument dropped and the 'target' used instead inheriting all colors and format from the 'arg' (if it's a Colored).

See https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda for details about the hyperlinks feature.

The Hyperlink doesn't escape the target and the params. They should be checked and escaped before.

See also HyperlinkID function.

For a simple example

au.Hyperlink("Example", "http://example.com")

and an example with ID

au.Hyperlink("Example", "http://example.com", aurora.HyperlinkID("10"))

func Index

func Index(n ColorIndex, arg interface{}) Value

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 Inverse

func Inverse(arg interface{}) Value

Inverse is alias for the Reverse

func Italic

func Italic(arg interface{}) Value

Italic is not widely supported, sometimes treated as inverse (3).

func Magenta

func Magenta(arg interface{}) Value

Magenta foreground color (35)

func Overlined

func Overlined(arg interface{}) Value

Overlined (53).

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
fmt.Println("value exceeds min-threshold:", Red(3.14))
Output:

value exceeds min-threshold: �[31m3.14�[0m

func Reset

func Reset(arg interface{}) Value

Reset wraps given argument returning Value without formats, colors and links.

func Reverse

func Reverse(arg interface{}) Value

Reverse video, swap foreground and background colors (7).

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.

func Underline

func Underline(arg interface{}) Value

Underline (4).

func White

func White(arg interface{}) Value

White foreground color (37)

func Yellow

func Yellow(arg interface{}) Value

Yellow foreground color (33)

func (Value) BgBlack

func (v Value) BgBlack() Value

Background colors

BgBlack background color (40).

func (Value) BgBlue

func (v Value) BgBlue() Value

BgBlue background color (44).

func (Value) BgBrightBlack

func (v Value) BgBrightBlack() Value

Bright background colors.

BgBrightBlack background color (100).

func (Value) BgBrightBlue

func (v Value) BgBrightBlue() Value

BgBrightBlue background color (104).

func (Value) BgBrightCyan

func (v Value) BgBrightCyan() Value

BgBrightCyan background color (106).

func (Value) BgBrightGreen

func (v Value) BgBrightGreen() Value

BgBrightGreen background color (102).

func (Value) BgBrightMagenta

func (v Value) BgBrightMagenta() Value

BgBrightMagenta background color (105).

func (Value) BgBrightRed

func (v Value) BgBrightRed() Value

BgBrightRed background color (101).

func (Value) BgBrightWhite

func (v Value) BgBrightWhite() Value

BgBrightWhite background color (107).

func (Value) BgBrightYellow

func (v Value) BgBrightYellow() Value

BgBrightYellow background color (103).

func (Value) BgCyan

func (v Value) BgCyan() Value

BgCyan background color (46).

func (Value) BgGray

func (v Value) BgGray(n GrayIndex) Value

BgGray from 0 to 24.

func (Value) BgGreen

func (v Value) BgGreen() Value

BgGreen background color (42).

func (Value) BgIndex

func (v Value) BgIndex(n ColorIndex) Value

Other background colors.

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 (Value) BgMagenta

func (v Value) BgMagenta() Value

BgMagenta background color (45).

func (Value) BgRed

func (v Value) BgRed() Value

BgRed background color (41).

func (Value) BgWhite

func (v Value) BgWhite() Value

BgWhite background color (47).

func (Value) BgYellow

func (v Value) BgYellow() Value

BgYellow background color (43).

func (Value) Black

func (v Value) Black() Value

Foreground colors.

Black foreground color (30).

func (v Value) Blink() Value

Blink is alias for the SlowBlink.

func (Value) Blue

func (v Value) Blue() Value

Blue foreground color (34).

func (Value) Bold

func (v Value) Bold() Value

Formats

Bold or increased intensity (1).

func (Value) BrightBlack

func (v Value) BrightBlack() Value

Bright foreground colors.

BrightBlack foreground color (90).

func (Value) BrightBlue

func (v Value) BrightBlue() Value

BrightBlue foreground color (94).

func (Value) BrightCyan

func (v Value) BrightCyan() Value

BrightCyan foreground color (96).

func (Value) BrightGreen

func (v Value) BrightGreen() Value

BrightGreen foreground color (92).

func (Value) BrightMagenta

func (v Value) BrightMagenta() Value

BrightMagenta foreground color (95).

func (Value) BrightRed

func (v Value) BrightRed() Value

BrightRed foreground color (91).

func (Value) BrightWhite

func (v Value) BrightWhite() Value

BrightWhite foreground color (97).

func (Value) BrightYellow

func (v Value) BrightYellow() Value

BrightYellow foreground color (93).

func (Value) Clear

func (v Value) Clear() Value

Clear colors and formats, preserving links.

func (Value) Color

func (v Value) Color() Color

Color returns colors and formats of the Value.

func (Value) Colorize

func (v Value) Colorize(color Color) Value

Special colorization method.

Colorize removes existing colors and formats of the argument and applies given.

func (Value) Conceal

func (v Value) Conceal() Value

Conceal, hidden, not widely supported (8).

func (Value) CrossedOut

func (v Value) CrossedOut() Value

CrossedOut, characters legible, but marked for deletion (9).

func (Value) Cyan

func (v Value) Cyan() Value

Cyan foreground color (36).

func (Value) DoublyUnderline

func (v Value) DoublyUnderline() Value

DoublyUnderline or Bold off, double-underline per ECMA-48 (21). It depends.

func (Value) Encircled

func (v Value) Encircled() Value

Encircled (52).

func (Value) Faint

func (v Value) Faint() Value

Faint, decreased intensity, reset the Bold (2).

func (Value) Format

func (v Value) Format(s fmt.State, verb rune)

Format implements standard fmt.Formatter interface.

func (Value) Fraktur

func (v Value) Fraktur() Value

Fraktur, rarely supported (20).

func (Value) Framed

func (v Value) Framed() Value

Framed (51).

func (Value) Gray

func (v Value) Gray(n GrayIndex) Value

Gray from 0 to 24.

func (Value) Green

func (v Value) Green() Value

Green foreground color (32).

func (Value) Hidden

func (v Value) Hidden() Value

Hidden is alias for the Conceal.

func (v Value) Hyperlink(target string, params ...HyperlinkParam) Value

Hyperlinks feature

Hyperlink with given target and parameters. If hyperlinks feature is disabled, then the 'arg' argument dropped and the 'target' used instead, inheriting all colors and format from the 'arg' (if it's a Colored).

See https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda for details about the hyperlinks feature.

The Hyperlink doesn't escape the target and the parameters. They should be checked and escaped before. See HyperlinkEscape function.

See also HyperlinkID function.

For a simple example

val.Hyperlink("http://example.com")

and an example with ID

val.Hyperlink("http://example.com", aurora.HyperlinkID("10"))

Successive calls replace previously set target and parameters.

func (Value) HyperlinkParams

func (v Value) HyperlinkParams() (params []HyperlinkParam)

HyperlinkParams if any.

func (Value) HyperlinkTarget

func (v Value) HyperlinkTarget() (target string)

HyperlinkTarget if any.

func (Value) Index

func (v Value) Index(n ColorIndex) Value

Other colors.

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 (Value) Inverse

func (v Value) Inverse() Value

Inverse is alias for the Reverse.

func (Value) Italic

func (v Value) Italic() Value

Italic, not widely supported, sometimes treated as inverse (3).

func (Value) Magenta

func (v Value) Magenta() Value

Magenta foreground color (35).

func (Value) Overlined

func (v Value) Overlined() Value

Overlined (53).

func (v Value) RapidBlink() Value

RapidBlink, blinking 150+ per minute, not widely supported (6).

func (Value) Red

func (v Value) Red() Value

Red foreground color (31).

func (Value) Reset

func (v Value) Reset() Value

Reset colors, formats and links.

func (Value) Reverse

func (v Value) Reverse() Value

Reverse video, swap foreground and background colors (7).

func (v Value) SlowBlink() Value

SlowBlink, blinking less than 150 per minute (5).

func (Value) StrikeThrough

func (v Value) StrikeThrough() Value

StrikeThrough is alias for the CrossedOut.

func (Value) String

func (v Value) String() string

String implements standard fmt.Stringer interface.

func (Value) Underline

func (v Value) Underline() Value

Underline (4).

func (Value) Value

func (v Value) Value() interface{}

Value returns value's value (welcome to the tautology club)

func (Value) White

func (v Value) White() Value

White foreground color (37).

func (Value) Yellow

func (v Value) Yellow() Value

Yellow foreground color (33).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL