crayons

package module
v0.0.0-...-33930bf Latest Latest
Warning

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

Go to latest
Published: Aug 22, 2015 License: MIT Imports: 7 Imported by: 0

README

crayons

GoDoc Build Status Coverage MIT License

There are alot of ANSI color libraries but none of them were useful for larger implementations. It is heavily modeled after fatih/color but exposes things slightly differently and adds a concept of grouping different styles.

This is very conceptual right now and in alpha so note, there might be some changes and is not stable just yet and is totally just a POC repo for me at this point.

Installing

go get github.com/apriendeau/crayons

Basic Usage

package main

import(
	"fmt"
	"github.com/apriendeau/crayons"
)

func main() {
	c := crayons.New(crayons.FgBrightCyan, crayons.BgBlack)

	c.Println("blizzard blue")

	str := c.Sprintf("blizzard %s", "blue")
	fmt.Println(str)
}

Boxes for Branding

Who wants just one crayon? I want a whole freaking box!

So as I was building a CLI tool, I always had to write another package just to have to use for styles for certain things such as errors, headers and just regular text. I found this annoying. As developer, we tend to forget about branding and UX so I wanted a way to define a style that would be constantly used throughout my CLI.

The value is consistency.

package main

import(
	"fmt"
	"log"

	"github.com/apriendeau/crayons"
)

func main() {
	redCrayon := crayons.New(crayons.FgBrightRed)
	greenCrayon := crayons.New(crayons.FgBrightGreen)

	box := crayons.NewBox(nil) // nil will default to the defaultFg, defaultBg
	err := box.Store("error", redCrayon)
	if err != nil {
		redCrayon.Fatal(err)
	}
	err = box.Store("success", greenCrayon)
	if err != nil {
		redCrayon.Fatal(err)
	}

	c := box.Pick("success")
	c.Println("Yay, I worked this time")

	c = box.Pick("error")
	c.Println("There was an error! Oh snap!")
	os.Exit(1)
}

Documentation

Index

Constants

View Source
const (
	FgBrightBlack = iota + 90
	FgBrightRed
	FgBrightGreen
	FgBrightYellow
	FgBrightBlue
	FgBrightMagenta
	FgBrightCyan
	FgWhite
)

Bright Foreground Colors

Variables

View Source
var (
	// ErrRemoveBase is when you try to remove the base crayon
	ErrRemoveBase = errors.New("Cannot remove base crayon")
	// ErrNilCrayon occurs when you attempt to store a nil crayon
	ErrNilCrayon = errors.New("Crayon cannot be a nil reference")
)
View Source
var (
	// Writer is a where crayons will draw too
	Writer = ansicolor.NewAnsiColorWriter(os.Stdout)
	// Monochrome checkts if it is tty
	Monochrome = !isatty.IsTerminal(os.Stdout.Fd())
)

Functions

func Colorize

func Colorize(str string, styles ...Style) string

Colorize is a shortcut for styling.

Types

type Box

type Box struct {
	Crayons map[string]*Crayon
}

Box is collection of crayons.

func NewBox

func NewBox(base *Crayon) *Box

NewBox creates a box of crayons and applys a default setting. If Base is nil, it will default to white text on a black background.

func (*Box) Names

func (b *Box) Names() []string

Names returns the names of all the stored crayons

func (*Box) Pick

func (b *Box) Pick(name string) *Crayon

Pick retrives a crayon for your coloring pleasure.

func (*Box) Remove

func (b *Box) Remove(name string) error

Remove destroys a crayon from your box.

func (*Box) Store

func (b *Box) Store(name string, crayon *Crayon) error

Store adds a crayon to the box for later retrieval.

type Crayon

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

Crayon is the structure for a crayon. It contains unexported fields

func New

func New(styles ...Style) *Crayon

New returns a pointer to an instance of a crayon. You can add several styles and it will add them all.

func (*Crayon) Append

func (c *Crayon) Append(styles ...Style) *Crayon

Append is in case you forgot to add a style

func (*Crayon) Apply

func (c *Crayon) Apply() *Crayon

Apply is the manual way of enabling the style for your string but will remain in effect unless you call Reset.

func (*Crayon) Fmt

func (c *Crayon) Fmt() string

Fmt is the start of the ANSI color string

func (*Crayon) Monochrome

func (c *Crayon) Monochrome(m bool)

Monochrome lets you set an individual crayon

func (*Crayon) Prepend

func (c *Crayon) Prepend(s Style) *Crayon

Prepend is for when you want to add a style to the beginning

func (*Crayon) Print

func (c *Crayon) Print(a ...interface{}) (int, error)

Print will print a string with the styles applied.

func (*Crayon) Printf

func (c *Crayon) Printf(base string, a ...interface{}) (int, error)

Printf acts same as fmt.Printf but will apply the styles

func (*Crayon) Println

func (c *Crayon) Println(a ...interface{}) (int, error)

Println acts the same fmt.Println but will apply the styles to each line

func (*Crayon) Reset

func (c *Crayon) Reset() *Crayon

Reset starts clears the styles that are enabled in the writer.

func (*Crayon) Sprint

func (c *Crayon) Sprint(a ...interface{}) string

Sprint applies the styles and acts as fmt.Sprint

func (*Crayon) Sprintf

func (c *Crayon) Sprintf(base string, a ...interface{}) string

Sprintf acts as fmt.Sprintf

func (*Crayon) Sprintln

func (c *Crayon) Sprintln(a ...interface{}) string

Sprintln wraps color around a fmt.Sprintln.

func (*Crayon) Styles

func (c *Crayon) Styles() []Style

Styles returns all the stored styles for a crayon

func (*Crayon) Unfmt

func (c *Crayon) Unfmt() string

Unfmt is an end of the ANSI color string

type Style

type Style int

Style is alias type for int

const (
	Clear Style = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)

Core Styles

const (
	FgBlack Style = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgBrightGrey

	DefaultFg
)

Foreground Colors

const (
	BgBlack Style = iota + 40
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgLightGrey

	DefaultBg
)

Background Colors

const (
	BgBrightBlack Style = iota + 100
	BgBrightRed
	BgBrightGreen
	BgBrightYellow
	BgBrightBlue
	BgBrightMagenta
	BgBrightCyan
	BgWhite
)

Bright Background Colors

Jump to

Keyboard shortcuts

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