lux

package module
v0.0.0-...-ffb5297 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2016 License: MIT Imports: 6 Imported by: 0

README

lux GoDoc Travis-CI Report card

Package lux provides a fmt.Formatter interface for outputting ANSI escape codes on the terminal.

Install

go get github.com/moorereason/lux

Usage

import (
  "fmt"

  "github.com/moorereason/lux"
)

func main() {
	// No Windows support
	fmt.Printf("%s\n", lux.Red("Red!"))

	// Windows supported - see explanation in README
	lux.Printf("%s\n", lux.Red("Red!"))
}

Snarky, Kitchen Sink Example

package main

import (
	"fmt"

	"github.com/mattn/go-colorable"
	"github.com/moorereason/lux"
)

func main() {

Start:
	// Don't care about Windows?
	fmt.Printf("%s\n", lux.Red("Red!"))
	fmt.Printf("%s\n", lux.Red(lux.BgWhite("Red on White")))

	// Oh, you do care about Windows. Good call...
	Stdout := colorable.NewColorableStdout()
	fmt.Fprintf(Stdout, "%s\n", lux.Red("Red!")) // *sigh*

	// Want lux to handle Stdout? Use built-in Print functions (Print, Println, Printf)
	lux.Printf("%s\n", lux.Red("Red!"))

	// Hang on to your styles
	p := lux.New(lux.Red, lux.BgWhite)
	p.Println("Big Red")

	// First-Class Prints!
	bigred := lux.New(lux.Bold, lux.Red).Println
	bigred("Notice!")

	// Make Terminals Great Again
	r := lux.HiRed
	w := lux.HiWhite
	b := lux.HiBlue
	lux.Printf("%s%s%s! %s%s%s!\n", r("U"), w("S"), b("A"), r("U"), w("S"), b("A"))

	if lux.NoColor {
		goto Exit
	} else {
		// Let's do it again without colors.
		// I know. Crazy, right?
		lux.NoColor = true
		goto Start
	}
Exit:
}

Windows Support

TL;DR: If you want cross-platform support, use lux.Print/Println/Printf instead of the fmt package.

Two things make Windows support difficult:

  1. Windows doesn't use escape codes but uses console drawing API calls
  2. Go's io.Stdout is a *io.File type, so you can't simply set it to an io.Writer. See golang/go#13473

Because of these limitations, creating cross-platform console colors requires writing wrappers around fmt.Fprintf to write to an io.Writer provided by the colorable package.

Acknowledgments

  • @mattn for the colorable package.
  • @fatih for the color package, which is a more widely used package and accomplishes many of the same goals as lux. The main difference is that lux is a fmt.Formatter implementation.

Documentation

Overview

Example
// Don't care about Windows?
fmt.Printf("%s\n", lux.Red("Red!"))
fmt.Printf("%s\n", lux.Red(lux.BgWhite("Red on White")))

// Oh, you do care about Windows. Good call...
Stdout := colorable.NewColorableStdout()
fmt.Fprintf(Stdout, "%s\n", lux.Red("Red!")) // *sigh*

// Want lux to handle Stdout? Use built-in Print functions (Print, Println, Printf)
lux.Printf("%s\n", lux.Red("Red!"))

// Hang on to your styles
p := lux.New(lux.Red, lux.BgWhite)
p.Println("Big Red")

// First-Class Prints!
bigred := lux.New(lux.Bold, lux.Red).Println
bigred("Notice!")

// Make Terminals Great Again
r := lux.HiRed
w := lux.HiWhite
b := lux.HiBlue
lux.Printf("%s%s%s! %s%s%s!\n", r("U"), w("S"), b("A"), r("U"), w("S"), b("A"))
Output:

Index

Examples

Constants

This section is empty.

Variables

View Source
var (
	// NoColor defines whether to use escape sequences or not.  By default,
	// escape sequences are enabled when running on a TTY.
	NoColor = !isatty.IsTerminal(os.Stdout.Fd())
)
View Source
var Output = colorable.NewColorableStdout()

Output is the io.Writer used by Print, Printf, and Println.

Functions

func Print

func Print(a ...interface{}) (n int, err error)

Print is a colorable frontend to fmt.Print.

Example
package main

import (
	"github.com/moorereason/lux"
)

func main() {
	lux.Print(lux.Blue("Skies"))
}
Output:

func Printf

func Printf(format string, a ...interface{}) (n int, err error)

Printf is a colorable frontend to fmt.Printf.

Example
package main

import (
	"github.com/moorereason/lux"
)

func main() {
	lux.Printf("%d\n", lux.HiCyan(20))
}
Output:

func Println

func Println(a ...interface{}) (n int, err error)

Println is a colorable frontend to fmt.Println.

Example
package main

import (
	"github.com/moorereason/lux"
)

func main() {
	lux.Println(lux.Blue("Skies"))
}
Output:

Types

type CodeFn

type CodeFn func(interface{}) *Lux

CodeFn represents a style code function.

type Lux

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

Lux represents the state of a particular stylized string.

func BgBlack

func BgBlack(v interface{}) *Lux

BgBlack writes the black background code.

func BgBlue

func BgBlue(v interface{}) *Lux

BgBlue writes the blue background code.

func BgCyan

func BgCyan(v interface{}) *Lux

BgCyan writes the cyan background code.

func BgGreen

func BgGreen(v interface{}) *Lux

BgGreen writes the green background code.

func BgHiBlack

func BgHiBlack(v interface{}) *Lux

BgHiBlack writes the high-intensity, black background code.

func BgHiBlue

func BgHiBlue(v interface{}) *Lux

BgHiBlue writes the high-intensity, blue background code.

func BgHiCyan

func BgHiCyan(v interface{}) *Lux

BgHiCyan writes the high-intensity, cyan background code.

func BgHiGreen

func BgHiGreen(v interface{}) *Lux

BgHiGreen writes the high-intensity, green background code.

func BgHiMagenta

func BgHiMagenta(v interface{}) *Lux

BgHiMagenta writes the high-intensity, magenta background code.

func BgHiRed

func BgHiRed(v interface{}) *Lux

BgHiRed writes the high-intensity, red background code.

func BgHiWhite

func BgHiWhite(v interface{}) *Lux

BgHiWhite writes the high-intensity, white background code.

func BgHiYellow

func BgHiYellow(v interface{}) *Lux

BgHiYellow writes the high-intensity, yellow background code.

func BgMagenta

func BgMagenta(v interface{}) *Lux

BgMagenta writes the magenta background code.

func BgRed

func BgRed(v interface{}) *Lux

BgRed writes the red background code.

func BgWhite

func BgWhite(v interface{}) *Lux

BgWhite writes the white background code.

func BgYellow

func BgYellow(v interface{}) *Lux

BgYellow writes the yellow background code.

func Black

func Black(v interface{}) *Lux

Black writes the black foreground code.

func BlinkRapid

func BlinkRapid(v interface{}) *Lux

BlinkRapid writes the rapid blink code.

func BlinkSlow

func BlinkSlow(v interface{}) *Lux

BlinkSlow writes the blink code.

func Blue

func Blue(v interface{}) *Lux

Blue writes the blue foreground code.

func Bold

func Bold(v interface{}) *Lux

Bold writes the bold code.

func Conceal

func Conceal(v interface{}) *Lux

Conceal writes the conceal code.

func CrossedOut

func CrossedOut(v interface{}) *Lux

CrossedOut writes the crossed-out code.

func Cyan

func Cyan(v interface{}) *Lux

Cyan writes the cyan foreground code.

func Faint

func Faint(v interface{}) *Lux

Faint writes the faint code.

func Green

func Green(v interface{}) *Lux

Green writes the green foreground code.

func HiBlack

func HiBlack(v interface{}) *Lux

HiBlack writes the high-intensity, black foreground code.

func HiBlue

func HiBlue(v interface{}) *Lux

HiBlue writes the high-intensity, blue foreground code.

func HiCyan

func HiCyan(v interface{}) *Lux

HiCyan writes the high-intensity, cyan foreground code.

func HiGreen

func HiGreen(v interface{}) *Lux

HiGreen writes the high-intensity, green foreground code.

func HiMagenta

func HiMagenta(v interface{}) *Lux

HiMagenta writes the high-intensity, magenta foreground code.

func HiRed

func HiRed(v interface{}) *Lux

HiRed writes the high-intensity, red foreground code.

func HiWhite

func HiWhite(v interface{}) *Lux

HiWhite writes the high-intensity, white foreground code.

func HiYellow

func HiYellow(v interface{}) *Lux

HiYellow writes the high-intensity, yellow foreground code.

func Italic

func Italic(v interface{}) *Lux

Italic writes the italic code.

func Magenta

func Magenta(v interface{}) *Lux

Magenta writes the magenta foreground code.

func New

func New(fns ...CodeFn) *Lux

New creates a new Lux instances with a series of CodeFn styles.

func Red

func Red(v interface{}) *Lux

Red writes the red foreground code.

func Reset

func Reset(v interface{}) *Lux

Reset writes the reset / normal escape code.

func ReverseVideo

func ReverseVideo(v interface{}) *Lux

ReverseVideo writes the reverse video code.

func Underline

func Underline(v interface{}) *Lux

Underline writes the underline code.

func White

func White(v interface{}) *Lux

White writes the white foreground code.

func Yellow

func Yellow(v interface{}) *Lux

Yellow writes the yellow foreground code.

func (*Lux) Add

func (x *Lux) Add(f CodeFn) *Lux

Add appends a style to the list of styles in a Lux.

func (*Lux) Format

func (x *Lux) Format(f fmt.State, format rune)

Format implements the fmt.Formatter interface. Not to be used directly. Instead, pass the Lux as a parameter to fmt format functions.

func (*Lux) Print

func (x *Lux) Print(a ...interface{}) (n int, err error)

Print is a colorable frontend to fmt.Print.

Example
package main

import (
	"github.com/moorereason/lux"
)

func main() {
	c := lux.New(lux.Red)
	c.Print("ERROR")
}
Output:

func (*Lux) Printf

func (x *Lux) Printf(format string, a ...interface{}) (n int, err error)

Printf is a colorable frontend to fmt.Printf.

Example
package main

import (
	"github.com/moorereason/lux"
)

var err error

func main() {
	c := lux.New(lux.BgRed)
	c.Printf("CRITICAL: %s\n", err)
}
Output:

func (*Lux) Println

func (x *Lux) Println(a ...interface{}) (n int, err error)

Println is a colorable frontend to fmt.Println.

Example
package main

import (
	"github.com/moorereason/lux"
)

func main() {
	c := lux.New(lux.Yellow)
	c.Println("WARNING")
}
Output:

Jump to

Keyboard shortcuts

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