ansi

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2024 License: Apache-2.0, MIT Imports: 8 Imported by: 0

Documentation

Overview

Package ansi provides a light abstraction around ANSI color codes.

Index

Constants

View Source
const (
	// Reset is an ANSI attribute for resetting formatting
	Reset attribute = 0

	// Bold is an ANSI attribute for setting a bold format
	Bold attribute = 1

	// Faint is an ANSI attribute for setting a faint format
	Faint attribute = 2

	// Italice is an ANSI attribute for setting an italic format
	Italic attribute = 3

	// Underline is an ANSI attribute for setting an underline format
	Underline attribute = 4

	// DefaultFont is an ANSI attribute for setting the default font
	DefaultFont attribute = 10

	// FGBlack is an ANSI attribute for the foreground color black
	FGBlack attribute = 30

	// FGRed is an ANSI attribute for the foreground color red
	FGRed attribute = 31

	// FGGreen is an ANSI attribute for the foreground color green
	FGGreen attribute = 32

	// FGYellow is an ANSI attribute for the foreground color yellow
	FGYellow attribute = 33

	// FGBlue is an ANSI attribute for the foreground color blue
	FGBlue attribute = 34

	// FGMagenta is an ANSI attribute for the foreground color magenta
	FGMagenta attribute = 35

	// FGCyan is an ANSI attribute for the foreground color cyan
	FGCyan attribute = 36

	// FGWhite is an ANSI attribute for the foreground color white
	FGWhite attribute = 37

	// FGGray is an ANSI attribute for the foreground color gray
	FGGray attribute = 90

	// FGBrightRed is an ANSI attribute for the foreground color brightred
	FGBrightRed attribute = 91

	// FGBrightGreen is an ANSI attribute for the foreground color brightgreen
	FGBrightGreen attribute = 92

	// FGBrightYellow is an ANSI attribute for the foreground color brightyellow
	FGBrightYellow attribute = 93

	// FGBrightBlue is an ANSI attribute for the foreground color brightblue
	FGBrightBlue attribute = 94

	// FGBrightMagenta is an ANSI attribute for the foreground color brightmagenta
	FGBrightMagenta attribute = 95

	// FGBrightCyan is an ANSI attribute for the foreground color brightcyan
	FGBrightCyan attribute = 96

	// FGBrightWhite is an ANSI attribute for the foreground color brightwhite
	FGBrightWhite attribute = 97

	// FGDefault is an ANSI attribute for setting the default foreground color
	FGDefault attribute = 39

	// BGBlack is an ANSI attribute for the background color black
	BGBlack attribute = 40

	// BGRed is an ANSI attribute for the background color red
	BGRed attribute = 41

	// BGGreen is an ANSI attribute for the background color green
	BGGreen attribute = 42

	// BGYellow is an ANSI attribute for the background color yellow
	BGYellow attribute = 43

	// BGBlue is an ANSI attribute for the background color blue
	BGBlue attribute = 44

	// BGMagenta is an ANSI attribute for the background color magenta
	BGMagenta attribute = 45

	// BGCyan is an ANSI attribute for the background color cyan
	BGCyan attribute = 46

	// BGWhite is an ANSI attribute for the background color white
	BGWhite attribute = 47

	// BGGray is an ANSI attribute for the background color gray
	BGGray attribute = 100

	// BGBrightRed is an ANSI attribute for the background color brightred
	BGBrightRed attribute = 101

	// BGBrightGreen is an ANSI attribute for the background color brightgreen
	BGBrightGreen attribute = 102

	// BGBrightYellow is an ANSI attribute for the background color brightyellow
	BGBrightYellow attribute = 103

	// BGBrightBlue is an ANSI attribute for the background color brightblue
	BGBrightBlue attribute = 104

	// BGBrightMagenta is an ANSI attribute for the background color brightmagenta
	BGBrightMagenta attribute = 105

	// BGBrightCyan is an ANSI attribute for the background color brightcyan
	BGBrightCyan attribute = 106

	// BGBrightWhite is an ANSI attribute for the background color brightwhite
	BGBrightWhite attribute = 107

	// BGDefault is an ANSI attribute for setting the default background color
	BGDefault attribute = 49
)
View Source
const (
	// ControlSequenceIntroducer is the prefix for ANSI escape commands.
	ControlSequenceIntroducer = "\033["

	// ControlCode is a rune representing the ANSI control code (0x1B, or 033).
	ControlCode rune = 033

	// Separator is the separator between different control codes.
	Separator = ';'

	// SGR is the suffix code for the Select Graphics Renderer control codes.
	SGRSuffix = 'm'
)

Variables

View Source
var None = Format()

Functions

func ColorWriter

func ColorWriter(w io.Writer) io.Writer

ColorWriter creates an io.Writer that forces writing of colors, even if it's disabled or not currently allowed.

func DisableColors

func DisableColors()

DisableColors will disable all color formatting globally

func Eprint

func Eprint(args ...any) (int, error)

Eprint behaves like fmt.Print, except that it will conditionally support color formatting depending if os.Stderr is colorable.

func Eprintf

func Eprintf(format string, args ...any) (int, error)

Eprintf behaves like fmt.Printf, except that it will conditionally support color formatting depending if os.Stderr is colorable.

func Eprintln

func Eprintln(args ...any) (int, error)

Eprintln behaves like fmt.Println, except that it will conditionally support color formatting depending if os.Stderr is colorable.

func Fprint

func Fprint(w io.Writer, args ...any) (int, error)

Fprint behaves like fmt.Fprint, except that it will conditionally support color formatting depending on whether the Writer is colorable.

func Fprintf

func Fprintf(w io.Writer, format string, args ...any) (int, error)

Fprintf behaves like fmt.Fprintf, except that it will conditionally support color formatting depending on whether the Writer is colorable.

func Fprintln

func Fprintln(w io.Writer, args ...any) (int, error)

Fprintln behaves like fmt.Fprintln, except that it will conditionally support color formatting depending on whether the Writer is colorable.

func Fsprintf

func Fsprintf(w io.Writer, format string, args ...any) string

Fsprintf behaves like fmt.Sprintf, except that it will conditionally support color formatting depending on whether the Writer is colorable.

func IsColorable

func IsColorable(w io.Writer) bool

IsColorable checks whether the specified Writer is a colorable output destination.

This will return true either if the Writer is a TTY with colors enabled, or if the writer is an explicit colorable writer.

func NoColorWriter

func NoColorWriter(w io.Writer) io.Writer

NoColorWriter creates an io.Writer that forces writing of colors to be disabled.

func Print

func Print(args ...any) (int, error)

print behaves like fmt.Print, except that it will conditionally support color formatting depending if os.Stdout is colorable.

func Printf

func Printf(format string, args ...any) (int, error)

Printf behaves like fmt.Printf, except that it will conditionally support color formatting depending if os.Stdout is colorable.

func Println

func Println(args ...any) (int, error)

Println behaves like fmt.Println, except that it will conditionally support color formatting depending if os.Stdout is colorable.

func Sprintf

func Sprintf(format string, args ...any) string

Sprintf behaves exactly like fmt.Sprintf, except it will disable color codes if NOCOLOR or NO_COLOR are set.

Note: ansi.Sprintf cannot obey explicit colored writers since it coalesces down to a single string type. If NOCOLOR is defined, then Sprintf behaves identically to fmt.Sprintf.

func StripFormat

func StripFormat(format string) string

StripFormat will remove all ANSI escape codes from the input format string.

Types

type Attribute

type Attribute interface {
	Display
	// contains filtered or unexported methods
}

Attribute is a unit type which represents individual ANSI formatting attributes.

type Display

type Display interface {
	Formatter
	// contains filtered or unexported methods
}

Display represents a displayable format element.

func Format

func Format(displays ...Display) Display

Format is a collection of ANSI displayable formatting attributes.

type Formatter

type Formatter interface {
	Format(format string, args ...any) string
}

Formatter is an interface for representing things capable of formatting strings with ANSI control codes.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
JackTT - Gopher 🇻🇳