ansi

package
v0.0.0-...-9969144 Latest Latest
Warning

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

Go to latest
Published: Feb 13, 2025 License: Apache-2.0, MIT Imports: 5 Imported by: 0

Documentation

Overview

Package ansi provides a light abstraction around ANSI escape codes.

The primary mechanism for using this package is to create an SGRControlSequence object, and then use the `fmt` package to format the object an ANSI-formatted string.

Index

Constants

View Source
const (
	// SGRPrefix is the ANSI SGR escape sequence prefix.
	SGRPrefix = "\033["

	// SGRSuffix is the ANSI SGR escape sequence suffix.
	SGRSuffix = "m"
)

Variables

This section is empty.

Functions

func CanFormat

func CanFormat(w io.Writer) bool

CanFormat returns true if the writer is a terminal that supports ANSI escape codes.

If the environment variable NO_COLOR or NOCOLOR is set to a boolean-truthy value (e.g. a value for which strconv.ParseBool returns true), then this function will return false.

To force this function to return true for a non-terminal writer, or when NO_COLOR/NOCOLOR is set, use EnableFormat.

func DisableFormat

func DisableFormat(w io.Writer) io.Writer

DisableFormat returns a writer that strips ANSI escape codes from the output.

func EnableFormat

func EnableFormat(w io.Writer) io.Writer

EnableFormat returns a writer that formats the output with ANSI escape codes, even if the underlying writer is not a terminal.

This will ensure that the writer, when used with NewWriter or CanFormat, will always be treated as a terminal that supports ANSI escape codes.

func NewWriter

func NewWriter(w io.Writer) io.Writer

NewWriter returns a writer that formats the output with ANSI escape codes if the underlying writer is a terminal that supports them; otherwise, it returns a writer that strips the codes.

This is commonly used to enable common codepaths to be ansi-marked-up, but still work correctly when the output is redirected to a file or another non-terminal destination.

Types

type SGRControlSequence

type SGRControlSequence []SGRParam

SGRControlSequence represents a collection of ANSI SGR attributes.

func Format

func Format(params ...SGRParam) SGRControlSequence

Format is a convenience function to create an SGRControlSequence from a list of SGRParam attributes.

func (*SGRControlSequence) Append

func (cs *SGRControlSequence) Append(other SGRControlSequence)

Append the 'other' control sequence to the end of this control sequence.

func (SGRControlSequence) Format

func (cs SGRControlSequence) Format(w fmt.State, verb rune)

Format returns the ANSI SGR escape sequence for the format.

func (SGRControlSequence) String

func (cs SGRControlSequence) String() string

String returns the ANSI SGR escape sequence for the format.

type SGRParam

type SGRParam uint8

SGRParam represents an ANSI SGR attribute.

const (
	// Reset resets all attributes.
	Reset SGRParam = 0

	// Bold makes text bold.
	Bold SGRParam = 1

	// Dim makes text dim.
	Dim SGRParam = 2

	// Italic makes text italic.
	Italic SGRParam = 3

	// Underline makes text underlined.
	Underline SGRParam = 4

	// FGBlack sets the foreground color to black.
	FGBlack SGRParam = 30

	// FGRed sets the foreground color to red.
	FGRed SGRParam = 31

	// FGGreen sets the foreground color to green.
	FGGreen SGRParam = 32

	// FGYellow sets the foreground color to yellow.
	FGYellow SGRParam = 33

	// FGBlue sets the foreground color to blue.
	FGBlue SGRParam = 34

	// FGMagenta sets the foreground color to magenta.
	FGMagenta SGRParam = 35

	// FGCyan sets the foreground color to cyan.
	FGCyan SGRParam = 36

	// FGWhite sets the foreground color to white.
	FGWhite SGRParam = 37

	// FGDefault resets the foreground color to the default.
	FGDefault SGRParam = 39

	// BGBlack sets the background color to black.
	BGBlack SGRParam = 40

	// BGRed sets the background color to red.
	BGRed SGRParam = 41

	// BGGreen sets the background color to green.
	BGGreen SGRParam = 42

	// BGYellow sets the background color to yellow.
	BGYellow SGRParam = 43

	// BGBlue sets the background color to blue.
	BGBlue SGRParam = 44

	// BGMagenta sets the background color to magenta.
	BGMagenta SGRParam = 45

	// BGCyan sets the background color to cyan.
	BGCyan SGRParam = 46

	// BGWhite sets the background color to white.
	BGWhite SGRParam = 47

	// BGDefault resets the background color to the default.
	BGDefault SGRParam = 49

	// FGBrightBlack sets the foreground color to bright black.
	FGBrightBlack SGRParam = 90

	// FGBrightRed sets the foreground color to bright red.
	FGBrightRed SGRParam = 91

	// FGBrightGreen sets the foreground color to bright green.
	FGBrightGreen SGRParam = 92

	// FGBrightYellow sets the foreground color to bright yellow.
	FGBrightYellow SGRParam = 93

	// FGBrightBlue sets the foreground color to bright blue.
	FGBrightBlue SGRParam = 94

	// FGBrightMagenta sets the foreground color to bright magenta.
	FGBrightMagenta SGRParam = 95

	// FGBrightCyan sets the foreground color to bright cyan.
	FGBrightCyan SGRParam = 96

	// FGBrightWhite sets the foreground color to bright white.
	FGBrightWhite SGRParam = 97

	// BGBrightBlack sets the background color to bright black.
	BGBrightBlack SGRParam = 100

	// BGBrightRed sets the background color to bright red.
	BGBrightRed SGRParam = 101

	// BGBrightGreen sets the background color to bright green.
	BGBrightGreen SGRParam = 102

	// BGBrightYellow sets the background color to bright yellow.
	BGBrightYellow SGRParam = 103

	// BGBrightBlue sets the background color to bright blue.
	BGBrightBlue SGRParam = 104

	// BGBrightMagenta sets the background color to bright magenta.
	BGBrightMagenta SGRParam = 105

	// BGBrightCyan sets the background color to bright cyan.
	BGBrightCyan SGRParam = 106

	// BGBrightWhite sets the background color to bright white.
	BGBrightWhite SGRParam = 107
)

func (SGRParam) Format

func (p SGRParam) Format(w fmt.State, verb rune)

Format returns the ANSI SGR escape sequence for the attribute.

func (SGRParam) String

func (p SGRParam) String() string

String returns the ANSI SGR escape sequence for the attribute.

Jump to

Keyboard shortcuts

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