Skip to content

leobcn/aurora

 
 

Repository files navigation

Aurora

GoDoc WTFPL License Build Status Coverage Status GoReportCard Gitter | paypal gratuity

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

aurora logo

Installation

Get

go get -u github.com/logrusorgru/aurora

Test

go test github.com/logrusorgru/aurora

Usage

Simple

package main

import (
	"fmt"

	. "github.com/logrusorgru/aurora"
)

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

simple png

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

printf png

aurora.Sprintf

package main

import (
	"fmt"

	. "github.com/logrusorgru/aurora"
)

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

sprintf png

Enable/disabel 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: disable png

With -colors flag: enable png

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

Supported colors & formats

  • background and foreground colors
    • black
    • red
    • green
    • brown
    • blue
    • magenta
    • cyan
    • gray
  • formats
    • bold
    • inversed

linux png
white 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() {
	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))

Licensing

Copyright © 2015 Konstantin Ivanov ivanov.konstantin@logrus.org.ru
This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the LICENSE.md file for more details.

About

Golang ultimate ANSI-colors that supports Printf/Sprintf methods

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 53.9%
  • HTML 46.1%