gocolorize

package
v0.0.0-...-c7bc9a1 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2015 License: MIT, MIT Imports: 2 Imported by: 0

README

#Gocolorize Gocolorize is a package that allows Go programs to provide ANSI coloring in a stateful manner. Gocolorize is ideal for logging or cli applications.

colored tests passing

wercker status

##Features

  • Stateful ANSI coloring
  • Supports Foreground and background colors
  • Supports a nuber of text properties such as bold or underline
  • Color multiple arguments
  • Color multiple interfaces, including complex types
  • Tests with 100% coverage
  • Working examples
  • Disable ability for portability

##Install Gocolorize To install:

$ go get github.com/agtorre/gocolorize

##Usage Ways to initialize a Colorize object:

    //It can be done like this
    var c gocolorize.Colorize
    c.SetFg(gocolorize.Red)
    c.SetBg(gocolorize.Black)
    
    //Or this
    c := gocolorize.Colorize{Fg: gocolorize.Red, Bg: gocolorize.Black}
    
    //Or this
    c := gocolorize.NewColor("red:black")
    

Once you have an object:

    //Call Paint to take inputs and return a colored string
    c.Paint("This", "accepts", "multiple", "arguments", "and", "types:", 1, 1.25, "etc")

    //If you want a short-hand closure
    p = c.Paint
    p("Neat")

    //To print it:
    Fmt.Println(p("test"))
    
    //It can also be appended to other strings, used in logging to stdout, etc.
    a := "test " + p("case")

    //The closure allows you to reuse the original object, for example
    p = c.Paint
    c.SetFg(gocolorize.Green)
    p2 = c.Paint
    Fmt.Println(p("different" + " " + p2("colors")))

Object Properties:

    //These will only apply if there is a Fg and Bg respectively
    c.ToggleFgIntensity()
    c.ToggleBgIntensity()
    
    //Set additional attributes
    c.ToggleBold()
    c.ToggleBlink()
    c.ToggleUnderLine()
    c.ToggleInverse()

    //To disable or renable everything color (for example on Windows)
    //the other functions will still work, they'll just return plain
    //text for portability
    gocolorize.SetPlain(true)

##NewColor String Format

"foregroundColor+attributes:backgroundColor+attributes"

Colors:

  • black
  • red
  • green
  • yellow
  • blue
  • magenta
  • cyan
  • white

Attributes:

  • b = bold foreground
  • B = blink foreground
  • u = underline foreground
  • h = high intensity (bright) foreground, background
  • i = inverse

##Examples See examples directory for examples:

$ cd examples/
$ go run song.go
$ go run logging.go

##Tests Tests are another good place to see examples. In order to run tests:

$ go test -cover

##Portability ANSI coloring will not work in default Windows environments and may not work in other environments correctly. In order to allow compatibility with these environments, you can call:

gocolorize.SetPlain(true)

Once toggled, the library will still function, but it will not color the output.

References

Wikipedia ANSI escape codes Colors

A stylesheet author's guide to terminal colors

Special Thanks

https://github.com/mgutz/ansi was an inspiration for the latest version. I did a lot of rewriting, removed the 'paints' module, and did a lot of general cleanup. I learned a lot reading this and using this code.

Documentation

Index

Constants

View Source
const (
	ColorNone = iota
	//placeholder here so we don't confuse with ColorNone
	Red
	Green
	Yellow
	Blue
	Magenta
	Cyan
	White
	Black Color = -1
)

Variables

This section is empty.

Functions

func SetPlain

func SetPlain(p bool)

Types

type Color

type Color int

Color can be used for Fg and Bg

type Colorize

type Colorize struct {
	Value []interface{}
	Fg    Color
	Bg    Color
	Prop  Property
}

Where the magic happens

func NewColor

func NewColor(style string) Colorize

func (Colorize) Format

func (ct Colorize) Format(fs fmt.State, c rune)

Format allows ColorText to satisfy the fmt.Formatter interface. The format behaviour is the same as for fmt.Print.

func (Colorize) Paint

func (c Colorize) Paint(v ...interface{}) string

returns a value you can stick into print, of type string

func (*Colorize) SetBgColor

func (C *Colorize) SetBgColor(b Color)

func (*Colorize) SetColor

func (C *Colorize) SetColor(c Color)

func (*Colorize) ToggleBgIntensity

func (C *Colorize) ToggleBgIntensity()
func (C *Colorize) ToggleBlink()

func (*Colorize) ToggleBold

func (C *Colorize) ToggleBold()

func (*Colorize) ToggleFgIntensity

func (C *Colorize) ToggleFgIntensity()

func (*Colorize) ToggleInverse

func (C *Colorize) ToggleInverse()

func (*Colorize) ToggleUnderline

func (C *Colorize) ToggleUnderline()

type Property

type Property struct {
	Bold      bool
	Blink     bool
	Underline bool
	Inverse   bool
	Fgi       bool
	Bgi       bool
}

Can set 1 or more of these properties This struct holds the state

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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