Golang ultimate ANSI-colors that supports Printf/Sprintf methods

Overview

Aurora

go.dev reference Unlicense Build Status Coverage Status GoReportCard Gitter

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

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

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

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

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

Comments
  • Support for Windows cmd?

    Support for Windows cmd?

    I tried to use it in my project. The code is like:

    log.Println(Cyan("Broken Pantsu"), Bold(VERSION))
    

    but when I run it on my Windows cmd shell it prints:

    00:39:45.419785 main.go:85: ←[36mBroken Pantsu←[0m ←[1mtest-build←[0m
    

    GOARCH=amd64 GOOS=windows

    opened by Equim-chan 15
  • Custom module requires manual setup

    Custom module requires manual setup

    If I import the module as usual

    
    import (
      "github.com/logrusorgru/aurora"
    )
    
    func init(){
      fmt.Println(aurora.Bold("foo"))
    }
    

    in other words I do not have to do:

    func init(){
      fmt.Println(aurora.colors.Bold("foo"))
    }
    

    But if I want to turn off colors and use a custom package to do so:

    package aw
    
    import (
      "github.com/logrusorgru/aurora"
      "os"
    )
    
    var hasColors = os.Getenv("cm_no_colors") != "nope"
    var x = aurora.NewAurora(hasColors)
    
    var Red = x.Red         // manual
    var Faint = x.Faint      // manual
    var Bold = x.Bold       // manual .. etc
    
    

    only using the local assigment of the method names can I get the same behavior. Now I can do:

    import (
      "github.com/channelmeter/aurora-wrapper/aw"
      "os"
    )
    
    func init(){
      fmt.Println(aw.Bold("foo"))
    }
    
    

    so what I am trying to say is - is there some easier way to export all the methods instead of having to manually do it?

    opened by ORESoftware 9
  • Add support for Light colors and Bright modifier

    Add support for Light colors and Bright modifier

    This adds support for more ansi colors, the light ones. However, it made things a little strange so I have done some adjustments. The previous Gray was really Dark White, so now with addition of light colors we also have Light Black which is a darker gray than dark white. So I have changed them for so the dark white is now Light Gray and dark black is gray. So basically, this means that for existing applications using Gray, it just got darker. Also, there is no LightBrown, since Brown really was dark yellow. So the Light Brown is just called Yellow.

    opened by snaggen 9
  • "%T" returns 'aurora.value'

    Hi,

    cool package!

    This command:

    fmt.Println(Sprintf(Green("We can see that the type of x is: %T"), Brown(x)))

    Returns:

    We can see that the type of x is: aurora.value

    I saw the workaround at in the readme for %T and %p, but the solution to print the type of x is still eluding me.

    opened by kimfucious 6
  • Adds support for Hyperlinks

    Adds support for Hyperlinks

    Adds support for Hyperlinks in many modern terminal emulators, as described in this gist.

    package main
    
    import (
      "fmt"
      "github.com/logrusorgru/aurora"
    )
    
    func main() {
      fmt.Println("Now with ", aurora.Link("links", "https://github.com/logrusorgru/aurora"))
    }
    
    opened by jphastings 5
  • add go.mod file

    add go.mod file

    This PR adds a go.mod file declaring the module as github.com/logrusorgru/aurora/v2 to match the latest v2.x.x tags.

    The intent is for this package to not appear marked as "incompatible" in projects that have a dependency on it, for example:

    	github.com/logrusorgru/aurora v2.0.3+incompatible
    

    Let me know what you think about this change.

    opened by achille-roussel 5
  • disable colors with an env var

    disable colors with an env var

    I saw this section on how to disable colors:

    https://github.com/logrusorgru/aurora#enabledisable-colors

    is there a way to disable colors with an env variable?

    opened by ORESoftware 4
  • offer global disable?

    offer global disable?

    Can we support disable global color? In some platforms (like Windows) not support ANSI color, and I have to disable the color feature.

    Currently, I can use aurora.NewAurora() to new a global variable. But it would be painful since the code no longer simplify.

    # Global variable
    color.Aurora.Green()
    
    # Package
    aurora.Green()
    
    opened by XSAM 4
  • Question: Example for getting custom colors

    Question: Example for getting custom colors

    Hey, thank you for this awesome package.

    I am looking for examples of how to use custom colors that are not predefined in aurora, (orange, turquoise, etc...), could you please provide some example?.

    Thanks!

    opened by vzamanillo 2
  • explanation of how code works :)

    explanation of how code works :)

    So I can import this library like so:

    import "github.com/logrusorgru/aurora"
    

    and then use it like:

    fmt.Println(aurora.Bold("foo"))
    

    but I don't see Bold exported, I only see:

    func (a aurora) Bold(arg interface{}) Value {
    
    }
    
    

    maybe I don't know enough about Go but how does that work?

    opened by ORESoftware 1
  • Tiny update in the documentation for Windows users

    Tiny update in the documentation for Windows users

    On Windows 10 the new ANSI Terminal Control is turned OFF by default, so users will not see any colors in the output if they run console Go program by just double clicking it.

    But this setting could be easily turned on by simple change in registry:

    [HKEY_CURRENT_USER\Console] "VirtualTerminalLevel"=dword:00000001

    More details about this issue in the answer from Glenn Slayden here - Windows console with ANSI colors handling

    So maybe mentioning about this nuance somewhere in the project README.md will save some time for Windows users.

    opened by sevenate 1
  • Non consuming escapes

    Non consuming escapes

    For manipulating prompts, it is necessary to mark characters as non-greedy (not consuming a space in the column calculation of the tty).

    Here is an example how this is done for bash and zsh. Maybe this is in scope for the ultimate color library?

    https://github.com/jonmosco/kube-ps1/blob/master/kube-ps1.sh#L57-L73

    opened by blaggacao 2
Owner
Konstantin Ivanov
Golang and WTFPL gospeller. Network programming.
Konstantin Ivanov
An ANSI colour terminal package for Go

colourize An ANSI colour terminal package for Go. Supports all ANSI colours and emphasis. Not compatible with Windows systems. Installation go get gi

Trey Bastian 25 Aug 10, 2022
Console Text Colors - The non-invasive cross-platform terminal color library does not need to modify the Print method

ctc - Console Text Colors The non-invasive cross-platform terminal color library does not need to modify the Print method Virtual unix-like environmen

null 40 Sep 14, 2022
go-isatty - isatty for golang.

go-isatty isatty for golang Usage package main import ( "fmt" "github.com/mattn/go-isatty" "os" ) func main() { if isatty.IsTerminal(os.Stdout.F

mattn 641 Sep 17, 2022
A really basic thread-safe progress bar for Golang applications

progressbar A very simple thread-safe progress bar which should work on every OS without problems. I needed a progressbar for croc and everything I tr

Zack 2.8k Sep 13, 2022
A tiny library for super simple Golang tables

Tabby A tiny library for super simple Golang tables Get Tabby go get github.com/cheynewallace/tabby Import Tabby import "github.com/cheynewallace/tabb

Cheyne Wallace 320 Sep 2, 2022
Golang terminal dashboard

termui termui is a cross-platform and fully-customizable terminal dashboard and widget library built on top of termbox-go. It is inspired by blessed-c

Zack Guo 12.1k Sep 22, 2022
🎨 Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows.

?? Terminal color rendering library, support 8/16 colors, 256 colors, RGB color rendering output, support Print/Sprintf methods, compatible with Windows. GO CLI 控制台颜色渲染工具库,支持16色,256色,RGB色彩渲染输出,使用类似于 Print/Sprintf,兼容并支持 Windows 环境的色彩渲染

Gookit 1.2k Sep 14, 2022
Chalk is a Go Package which can be used for making terminal output more vibrant with text colors, text styles and background colors.

Chalk Chalk is a Go Package which can be used for making terminal output more vibrant with text colors, text styles and background colors. Documentati

null 5 Jul 22, 2022
Translate your Go program into multiple languages with similar fmt.Sprintf format syntax.

Loafer-i18n Loafer-i18n is a Go package and a command that helps you translate Go programs into multiple languages. Supports pluralized strings with =

EasyGram 0 Dec 22, 2021
Detects whether a terminal supports colors

Termcolor Detects what level of color support your terminal has. This package is heavily inspired by chalk's support-color module. Install go get gith

Efe Karakus 26 Aug 4, 2022
The Ultimate Workshop Track for #golang Developer

layout title nav_order description permalink default An Ultimate GopherLabs Hands-on Labs 1 An Ultimate GopherLabs Hands-on Labs / Join GopherLabs Com

Sangam Biradar 146 May 19, 2022
The Ultimate Engineer Toolbox YouTube 🔨 🔧

The Ultimate Engineer Toolbox YouTube ?? ??

Marcel Dempers 3.4k Sep 25, 2022
The ultimate CLI tool for TiKV

The ultimate CLI tool for TiKV

dongxu 37 Sep 22, 2022
The ultimate way to move between folders in the CLI

goto-command The ultimate way to move between folders in the command line Goto is a command that can be used like cd, and also allows you to add speci

null 0 Mar 21, 2022
Fluxcdproj - The Ultimate Swiss Army knife for DevOps, Developers and Platform Engineers

Fluxcdproj - The Ultimate Swiss Army knife for DevOps, Developers and Platform Engineers

null 0 Feb 1, 2022
Go structure annotations that supports encoding and decoding; similar to C-style bitfields. Supports bitfield packing, self-describing layout parameters, and alignment.

STRUCTure EXtensions structex provides annotation rules that extend Go structures for implementation of encoding and decoding of byte backed data fram

Hewlett Packard Enterprise 53 Jul 26, 2022
Small, fast library to create ANSI colored strings and codes. [go, golang]

ansi Package ansi is a small, fast library to create ANSI colored strings and codes. Install Get it go get -u github.com/mgutz/ansi Example import "gi

Mario Gutierrez 370 Sep 22, 2022
Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang)

Color lets you use colorized outputs in terms of ANSI Escape Codes in Go (Golang). It has support for Windows too! The API can be used in several ways, pick one that suits you.

Sensio 0 Feb 5, 2022
Golang terminal ANSI OSC52 wrapper. Copy text to clipboard from anywhere.

go-osc52 A terminal Go library to copy text to clipboard from anywhere. It does so using ANSI OSC52. The Copy() function defaults to copying text from

Ayman Bagabas 15 Sep 22, 2022