colorstring

package module
v0.0.0-...-9440f19 Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2015 License: MIT Imports: 3 Imported by: 3

README

colorstring Build Status

colorstring is a Go library for outputting colored strings to a console using a simple inline syntax in your string to specify the color to print as.

For example, the string [blue]hello [red]world would output the text "hello world" in two colors. The API of colorstring allows for easily disabling colors, adding aliases, etc.

Installation

Standard go get:

$ go get github.com/mitchellh/colorstring

Usage & Example

For usage and examples see the Godoc.

Usage is easy enough:

fmt.Println(colorstring.Color("[blue]Hello [red]World!"))

Additionally, the Colorize struct can be used to set options such as custom colors, color disabling, etc.

Documentation

Overview

colorstring provides functions for colorizing strings for terminal output.

Index

Constants

This section is empty.

Variables

View Source
var DefaultColors map[string]string

DefaultColors are the default colors used when colorizing.

If the color is surrounded in underscores, such as "_blue_", then that color will be used for the background color.

Functions

func Color

func Color(v string) string

Color colorizes your strings using the default settings.

Strings given to Color should use the syntax `[color]` to specify the color for text following. For example: `[blue]Hello` will return "Hello" in blue. See DefaultColors for all the supported colors and attributes.

If an unrecognized color is given, it is ignored and assumed to be part of the string. For example: `[hi]world` will result in "[hi]world".

A color reset is appended to the end of every string. This will reset the color of following strings when you output this text to the same terminal session.

If you want to customize any of this behavior, use the Colorize struct.

Types

type Colorize

type Colorize struct {
	// Colors maps a color string to the code for that color. The code
	// is a string so that you can use more complex colors to set foreground,
	// background, attributes, etc. For example, "boldblue" might be
	// "1;34"
	Colors map[string]string

	// If true, color attributes will be ignored. This is useful if you're
	// outputting to a location that doesn't support colors and you just
	// want the strings returned.
	Disable bool

	// Reset, if true, will reset the color after each colorization by
	// adding a reset code at the end.
	Reset bool
}

Colorize colorizes your strings, giving you the ability to customize some of the colorization process.

The options in Colorize can be set to customize colorization. If you're only interested in the defaults, just use the top Color function directly, which creates a default Colorize.

func (*Colorize) Color

func (c *Colorize) Color(v string) string

Color colorizes a string according to the settings setup in the struct.

For more details on the syntax, see the top-level Color function.

Jump to

Keyboard shortcuts

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