Golang ultimate ANSI-colors that supports Printf/Sprintf methods

Related tags

golang printf ansi-colors
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.

Issues
  • 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
  • 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
  • Why does aurora.Gray() take 2 args

    Why does aurora.Gray() take 2 args

    This is confusing:

    Screen Shot 2020-02-07 at 10 27 50 AM
    opened by ORESoftware 6
  • 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
  • should not use dot imports

    should not use dot imports

    Got this lint error, how to fix it?

    opened by w4-hanggi 3
  • Fix spelling mistake in README

    Fix spelling mistake in README

    Change 'disabel' to 'Disable'

    opened by otraore 3
  • Minor grammer improvments

    Minor grammer improvments

    Made some small changes to the grammar of this sentence.

    Great project!

    opened by danawoodman 2
  • 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 3
  • 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
  • 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
  • Backward compatibility

    Backward compatibility

    The v1.1 should be backward compatible with previous Aurora implementation. Please, let me know if the v1.1 breaks something.

    question 
    opened by logrusorgru 7
Owner
Konstantin Ivanov
Golang and WTFPL gospeller. Network programming.
Konstantin Ivanov
Terminal string styling for go done right, with full and painless Windows 10 support.

GChalk GChalk is a library heavily inspired by chalk, the popular Node.js terminal color library, and using go ports of supports-color and ansi-styles

Jason Walton 273 Jul 14, 2021
Small library for simple and convenient formatted stylized output to the console.

cfmt cfmt is a small library for simple and convenient formatted stylized output to the console, providing an interface that is exactly the same as th

Makhnev Petr 28 Jul 17, 2021
✨ #PTerm is a modern go module to beautify console output. Featuring charts, progressbars, tables, trees, and many more 🚀 It's completely configurable and 100% cross-platform compatible.

?? PTerm | Pretty Terminal Printer A golang module to print pretty text Show Demo Code PTerm.sh | Installation | Documentation | Quick Start | Example

null 1.7k Jul 25, 2021
Intuitive package for prettifying terminal/console output. http://godoc.org/github.com/ttacon/chalk

chalk Chalk is a go package for styling console/terminal output. Check out godoc for some example usage: http://godoc.org/github.com/ttacon/chalk The

Trey Tacon 376 Jul 15, 2021
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 33 Jun 6, 2021
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 24 Mar 30, 2021
Yet Another CLi Spinner; providing over 70 easy to use and customizable terminal spinners for multiple OSes

Yet Another CLi Spinner (for Go) Package yacspin provides yet another CLi spinner for Go, taking inspiration (and some utility code) from the https://

Tim Heckman 186 Jul 17, 2021
go-colorable - Colorable writer for windows.

go-colorable Colorable writer for windows. For example, most of logger packages doesn't show colors on windows. (I know we can do it with ansicon. But

mattn 537 Jul 10, 2021
Simple tables in terminal with Go

Simple tables in terminal with Go This package allows to generate and display ascii tables in the terminal, f.e.: +----+------------------+-----------

Alexey Popov 320 Jul 20, 2021
Tabular simplifies printing ASCII tables from command line utilities

tabular Tabular simplifies printing ASCII tables from command line utilities without the need to pass large sets of data to it's API. Simply define th

InVision 55 May 18, 2021
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 2k Jul 20, 2021
Change the color of console text.

go-colortext package This is a package to change the color of the text and background in the console, working both under Windows and other systems. Un

Yi Deng 207 Jul 1, 2021
Terminal based dashboard.

Termdash is a cross-platform customizable terminal based dashboard. The feature set is inspired by the gizak/termui project, which in turn was inspire

Jakub Sobon 1.8k Jul 23, 2021