termenv

package module
v0.5.1-0...-8c176d7 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2020 License: MIT Imports: 11 Imported by: 0

README

termenv

Latest Release GoDoc Build Status Coverage Status Go ReportCard

termenv lets you safely use advanced styling options on the terminal. It gathers information about the terminal environment in terms of its ANSI & color support and offers you convenient methods to colorize and style your output, without you having to deal with all kinds of weird ANSI escape sequences and color conversions.

Example output

Installation

go get github.com/muesli/termenv

Query Terminal Status

// returns supported color profile: Ascii, ANSI, ANSI256, or TrueColor
termenv.ColorProfile()

// returns default foreground color
termenv.ForegroundColor()

// returns default background color
termenv.BackgroundColor()

// returns whether terminal uses a dark-ish background
termenv.HasDarkBackground()

Colors

termenv supports multiple color profiles: ANSI (16 colors), ANSI Extended (256 colors), and TrueColor (24-bit RGB). Colors will automatically be degraded to the best matching available color in the desired profile:

TrueColor => ANSI 256 Colors => ANSI 16 Colors => Ascii

out := termenv.String("Hello World")

// retrieve color profile supported by terminal
p := termenv.ColorProfile()

// supports hex values
// will automatically degrade colors on terminals not supporting RGB
out = out.Foreground(p.Color("#abcdef"))
// but also supports ANSI colors (0-255)
out = out.Background(p.Color("69"))

fmt.Println(out)

Styles

out := termenv.String("foobar")

// text styles
out.Bold()
out.Faint()
out.Italic()
out.CrossOut()
out.Underline()
out.Overline()

// reverse swaps current fore- & background colors
out.Reverse()

// blinking text
out.Blink()

// combine multiple options
out.Bold().Underline()

Template Helpers

// load template helpers
f := termenv.TemplateFuncs(termenv.ColorProfile())
tpl := template.New("tpl").Funcs(f)

// apply bold style in a template
bold := `{{ Bold "Hello World" }}`

// examples for colorized templates
col := `{{ Color "#ff0000" "#0000ff" "Red on Blue" }}`
fg := `{{ Foreground "#ff0000" "Red Foreground" }}`
bg := `{{ Background "#0000ff" "Blue Background" }}`

// wrap styles
wrap := `{{ Bold (Underline "Hello World") }}`

// parse and render
tpl = tpl.Parse(bold)

var buf bytes.Buffer
tpl.Execute(&buf, nil)
fmt.Println(buf)

Other available helper functions are: Faint, Italic, CrossOut, Underline, Overline, Reverse, and Blink.

Color Chart

ANSI color chart

You can find the source code used to create this chart in termenv's examples.

Check out Glow, a markdown renderer for the command-line, which uses termenv.

License

MIT

Documentation

Index

Constants

View Source
const (
	Foreground = "38"
	Background = "48"
)
View Source
const (
	AltScreenSeq          = "?1049h"
	ExitAltScreenSeq      = "?1049l"
	CursorUpSeq           = "%dA"
	CursorDownSeq         = "%dB"
	CursorForwardSeq      = "%dC"
	CursorBackSeq         = "%dD"
	CursorNextLineSeq     = "%dE"
	CursorPreviousLineSeq = "%dF"
	CursorHorizontalSeq   = "%dG"
	CursorPositionSeq     = "%d;%dH"
	EraseDisplaySeq       = "%dJ"
	EraseLineSeq          = "%dK"
	ScrollUpSeq           = "%dS"
	ScrollDownSeq         = "%dT"
	ShowCursorSeq         = "?25h"
	HideCursorSeq         = "?25l"
)
View Source
const (
	ResetSeq     = "0"
	BoldSeq      = "1"
	FaintSeq     = "2"
	ItalicSeq    = "3"
	UnderlineSeq = "4"
	BlinkSeq     = "5"
	ReverseSeq   = "7"
	CrossOutSeq  = "9"
	OverlineSeq  = "53"
)
View Source
const (
	CSI = "\x1b["

	Ascii = Profile(iota)
	ANSI
	ANSI256
	TrueColor
)

Variables

View Source
var (
	ErrInvalidColor = errors.New("invalid color")
)
View Source
var (
	ErrStatusReport = errors.New("unable to retrieve status report")
)
View Source
var ForceColor bool = false

ForceColor overwrites the tty check, e.g. useful when generating shell prompts.

Functions

func AltScreen

func AltScreen()

AltScreen switches to the altscreen. The former view can be restored with ExitAltScreen().

func ClearLine

func ClearLine()

ClearLine clears the current line.

func ClearLines

func ClearLines(n int)

ClearLines clears a given number of lines.

func ClearScreen

func ClearScreen()

ClearScreen clears the visible portion of the terminal.

func ConvertToRGB

func ConvertToRGB(c Color) colorful.Color

func CursorNextLine

func CursorNextLine(n int)

CursorNextLine moves the cursor down a given number of lines and places it at the beginning of the line.

func CursorPrevLine

func CursorPrevLine(n int)

CursorPrevLine moves the cursor up a given number of lines and places it at the beginning of the line.

func ExitAltScreen

func ExitAltScreen()

ExitAltScreen exits the altscreen and returns to the former terminal view.

func HasDarkBackground

func HasDarkBackground() bool

HasDarkBackground returns whether terminal uses a dark-ish background

func HideCursor

func HideCursor()

HideCursor hides the cursor.

func MoveCursor

func MoveCursor(row int, column int)

MoveCursor moves the cursor to a given position.

func ShowCursor

func ShowCursor()

ShowCursor shows the cursor.

func TemplateFuncs

func TemplateFuncs(p Profile) template.FuncMap

TemplateFuncs contains a few useful template helpers

Types

type ANSI256Color

type ANSI256Color int

ANSI256Color is a color (16-255) as defined by the ANSI Standard

func (ANSI256Color) Sequence

func (c ANSI256Color) Sequence(bg bool) string

type ANSIColor

type ANSIColor int

ANSIColor is a color (0-15) as defined by the ANSI Standard

func (ANSIColor) Sequence

func (c ANSIColor) Sequence(bg bool) string

type Color

type Color interface {
	Sequence(bg bool) string
}

func BackgroundColor

func BackgroundColor() Color

BackgroundColor returns the terminal's default background color

func ForegroundColor

func ForegroundColor() Color

ForegroundColor returns the terminal's default foreground color

type NoColor

type NoColor struct{}

func (NoColor) Sequence

func (c NoColor) Sequence(bg bool) string

type Profile

type Profile int

func ColorProfile

func ColorProfile() Profile

ColorProfile returns the supported color profile: Ascii, ANSI, ANSI256, or TrueColor

func (Profile) Color

func (p Profile) Color(s string) Color

func (Profile) Convert

func (p Profile) Convert(c Color) Color

type RGBColor

type RGBColor string

RGBColor is a hex-encoded color, e.g. "#abcdef"

func (RGBColor) Sequence

func (c RGBColor) Sequence(bg bool) string

type Style

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

Style is a string that various rendering styles can be applied to.

func String

func String(s ...string) Style

String returns a new Style

func (Style) Background

func (t Style) Background(c Color) Style

Background sets a background color

func (t Style) Blink() Style

Blink enables blink mode

func (Style) Bold

func (t Style) Bold() Style

Bold enables bold rendering

func (Style) CrossOut

func (t Style) CrossOut() Style

CrossOut enables crossed-out rendering

func (Style) Faint

func (t Style) Faint() Style

Faint enables faint rendering

func (Style) Foreground

func (t Style) Foreground(c Color) Style

Foreground sets a foreground color

func (Style) Italic

func (t Style) Italic() Style

Italic enables italic rendering

func (Style) Overline

func (t Style) Overline() Style

Overline enables overline rendering

func (Style) Reverse

func (t Style) Reverse() Style

Reverse enables reverse color mode

func (Style) String

func (t Style) String() string

func (Style) Styled

func (t Style) Styled(s string) string

Styled renders s with all applied styles

func (Style) Underline

func (t Style) Underline() Style

Underline enables underline rendering

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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