Skip to content

foize/go.sgr

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

56 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

About

Terminal/console colors and text decoration (bold,underlined,etc). Uses ANSI SGR: Select Graphic Rendition

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages