sgr

package module
v0.0.0-...-40bdfc9 Latest Latest
Warning

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

Go to latest
Published: Feb 20, 2014 License: BSD-3-Clause Imports: 5 Imported by: 18

README

SGR (Select Graphic Rendition) for Go

The package sgr provides methods to use colors and text-decoration in ANSI-escape-sequence compatible terminals. It has support for 256-colors and makes available text-decorations such as bold, underline, negative and blink.

Installation

Please use go-get to install this package.

go get github.com/foize/go.sgr

Usage

Typical usage is to use Parseln() or MustParseln(), and use the resulting string in succeeding fmt or log calls.

package main

import (
	"fmt"
	"github.com/foize/go.sgr"
)

func main() {
	sgr.Println("This is an example: [fg-red bold] important text [reset] normal text again.")

	// use sgr.Printf like you're used to with fmt
	// note: you should use `[reset]`  before the newline `\n`
	sgr.Printf("The secret number is [fg-17]%d [reset]\n", 42)

	// you can also parse once and re-use the parsed format string
	// using Parseln eliminates the need for a `[reset]\n` at the end of line
	secretNumberFormat := sgr.MustParseln("The secret number is [bg-17 blink]%d")
	fmt.Printf(secretNumberFormat, 42)

	fmt.Println("This text is normal again, MustParseln puts a reset at the end of the line.")
}

Examples

For more examples, see the example/example.go file. You can also run the example.

Escaping

If you want to use an opening square bracket you should escape it like this:

sgr.MustParseln("foo [[ bar")

This will print "foo [ bar".

TODO

Features like the stdlib log package (date time) might be added in a subpackage.

License

This package is licensed under a modified BSD 3-clause license.

Godoc

For more information visit godoc.org/github.com/foize/go.sgr

xterm color codes

ansi sgr color codes

Documentation

Overview

Typical usage is to use Parseln() or MustParseln(), and use the resulting string in succeeding `fmt` or `log` calls.

package main

import (
	"fmt"
	"github.com/foize/go.sgr"
)

func main() {
	exampleString := sgr.MustParseln("This is an example: [fg-red bold] important text [reset] normal text again.")
	fmt.Print(exampleString)

	secretNumberFormat := sgr.MustParseln("The secret number is [bg-17 blink]%d")
	fmt.Printf(secretNumberFormat, 42)

	fmt.Println("This text is normal again, MustParseln puts a reset at the end of the line.")
}

If you want to use an opening square bracket you should escape it like this:

sgr.MustParseln("foo [[ bar")

This will print "foo [ bar".

Index

Constants

View Source
const (
	FgBlack   = "\x1b[38;05;0m"
	FgRed     = "\x1b[38;05;1m"
	FgGreen   = "\x1b[38;05;2m"
	FgYellow  = "\x1b[38;05;3m"
	FgBlue    = "\x1b[38;05;4m"
	FgMagenta = "\x1b[38;05;5m"
	FgCyan    = "\x1b[38;05;6m"
	FgGrey    = "\x1b[38;05;7m"
	FgWhite   = "\x1b[38;05;255m"

	BgBlack   = "\x1b[48;05;0m"
	BgRed     = "\x1b[48;05;1m"
	BgGreen   = "\x1b[48;05;2m"
	BgYellow  = "\x1b[48;05;3m"
	BgBlue    = "\x1b[48;05;4m"
	BgMagenta = "\x1b[48;05;5m"
	BgCyan    = "\x1b[48;05;6m"
	BgGrey    = "\x1b[48;05;7m"
	BgWhite   = "\x1b[48;05;255m"
)

Color strings

View Source
const (
	Reset                = "\x1b[0m"  // Reset all SGR options.
	ResetForegroundColor = "\x1b[39m" // Reset foreground color.
	ResetBackgroundColor = "\x1b[49m" // Reset background color.

	Bold    = "\x1b[1m"
	BoldOff = "\x1b[22m"

	Underline    = "\x1b[4m"
	UnderlineOff = "\x1b[24m"

	Blink    = "\x1b[5m" // Less then 150 per minute.
	BlinkOff = "\x1b[25m"

	ImageNegative = "\x1b[7m"  // Set reversed-video active (foreground and background negative).
	ImagePositive = "\x1b[27m" // Reset reversed-video to normal.

	Framed             = "\x1b[51m"
	Encircled          = "\x1b[52m"
	FramedEncircledOff = "\x1b[54m"

	Overlined    = "\x1b[53m"
	OverlinedOff = "\x1b[55m"
)

Option strings

Variables

View Source
var ExistingNewline = []byte(FgWhite + BgGreen + `↵ ` + Reset)

ExistingNewline is written to the Writer set to ColorWriter when the written bytes ended with a newline

View Source
var ForcedNewline = []byte(FgWhite + BgRed + `↵ ` + Reset)

ForcedNewline is written to the Writer set to ColorWriter when the written bytes didn't end with a newline

Functions

func BgColor

func BgColor(num uint8) string

func FgColor

func FgColor(num uint8) string

func MustParse

func MustParse(format string) string

func MustParseWithoutReset

func MustParseWithoutReset(format string) string

func MustParseln

func MustParseln(format string) string

func Parse

func Parse(format string) (string, error)

func ParseWithoutReset

func ParseWithoutReset(format string) (string, error)

func Parseln

func Parseln(format string) (string, error)

func Print

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

Print wraps fmt.Print All arguments with type string are parsed before calling fmt.Print

func Printf

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

Printf wraps fmt.Printf The format string is parsed before fmt.Printf is called

func Println

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

Println wraps fmt.Println All arguments with type string are parsed before calling fmt.Println

Types

type ColorWriter

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

ColorWriter implements io.Writer

func NewColorWriter

func NewColorWriter(w io.Writer, color string, forceNewline bool) *ColorWriter

NewColorWriter creates a new ColorWriter. example: NewColorWriter(os.Stdout, sgr.FgBlue, true)

func (*ColorWriter) Write

func (cw *ColorWriter) Write(p []byte) (n int, err error)

Write writes the bytes to underlying writer, but adds coloring

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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