style

package
v0.0.0-...-de190fe Latest Latest
Warning

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

Go to latest
Published: Oct 28, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// AttributeMap contains a mapping between names and attributes. This is
	// used by StyleString to search an replace attribute names with their ANSI
	// color escape sequences. It is exported so that users can add more
	// mappings if desired.
	AttributeMap = map[string]Attribute{

		"reset":        Reset,
		"bold":         Bold,
		"faint":        Faint,
		"italic":       Italic,
		"underline":    Underline,
		"blinkslow":    BlinkSlow,
		"blinkrapid":   BlinkRapid,
		"reversevideo": ReverseVideo,
		"concealed":    Concealed,
		"crossedout":   CrossedOut,

		"fraktur":             Fraktur,
		"doubleunderline":     DoubleUnderline,
		"normal":              Normal,
		"nofraktur":           NoFraktur,
		"nounderline":         NoUnderline,
		"noblink":             NoBlink,
		"proportionalspacing": ProportionalSpacing,
		"noreverse":           NoReverse,
		"reveal":              Reveal,
		"nocrossedout":        NoCrossedOut,

		"fgblack":   FgBlack,
		"fgred":     FgRed,
		"fggreen":   FgGreen,
		"fgyellow":  FgYellow,
		"fgblue":    FgBlue,
		"fgmagenta": FgMagenta,
		"fgcyan":    FgCyan,
		"fgwhite":   FgWhite,
		"fgcolor":   FgColor,
		"fgdefault": FgDefault,

		"bgblack":   BgBlack,
		"bgred":     BgRed,
		"bggreen":   BgGreen,
		"bgyellow":  BgYellow,
		"bgblue":    BgBlue,
		"bgmagenta": BgMagenta,
		"bgcyan":    BgCyan,
		"bgwhite":   BgWhite,
		"bgcolor":   BgColor,
		"bgdefault": BgDefault,

		"fghiblack":   FgHiBlack,
		"fghired":     FgHiRed,
		"fghigreen":   FgHiGreen,
		"fghiyellow":  FgHiYellow,
		"fghiblue":    FgHiBlue,
		"fghimagenta": FgHiMagenta,
		"fghicyan":    FgHiCyan,
		"fghiwhite":   FgHiWhite,

		"bghiblack":   BgHiBlack,
		"bghired":     BgHiRed,
		"bghigreen":   BgHiGreen,
		"bghiyellow":  BgHiYellow,
		"bghiblue":    BgHiBlue,
		"bghimagenta": BgHiMagenta,
		"bghicyan":    BgHiCyan,
		"bghiwhite":   BgHiWhite,

		"black":   FgBlack,
		"red":     FgRed,
		"green":   FgGreen,
		"yellow":  FgYellow,
		"blue":    FgBlue,
		"magenta": FgMagenta,
		"cyan":    FgCyan,
		"white":   FgWhite,
		"default": FgDefault,

		"rst":           Reset,
		"blink":         BlinkSlow,
		"strike":        CrossedOut,
		"strikethrough": CrossedOut,
	}
)
View Source
var (
	// Stdout is an io.Writer for stdout which properly handles escape
	// sequences.
	Stdout = colorable.NewColorableStdout()
)

Functions

func Disable

func Disable() func()

Disable disables output coloring. The returned func can be used in combination with defer to restore the previous state.

func Enable

func Enable() func()

Enable enables output coloring. The returned func can be used in combination with defer to restore the previous state.

func Enabled

func Enabled() bool

Enabled returns true if colors are enabled.

func EscapeString

func EscapeString(attr Attribute) string

EscapeString creates the escape sequence for given attribute and returns it. If coloring is disabled this returns an empty string.

func EscapeWriter

func EscapeWriter(w io.Writer, attr Attribute) (n int, err error)

EscapeWriter creates the escape sequence for given attribute and writes to w. It returns the number of bytes written and any write error encountered. If coloring is disabled this is a no-op.

func ResetString

func ResetString() string

ResetString creates the escape sequence for resetting all style attributes and returns it. If coloring is disabled this returns an empty string.

func ResetWriter

func ResetWriter(w io.Writer) (n int, err error)

ResetWriter creates the escape sequence for resetting all style attributes and writes to w. It returns the number of bytes written and any write error encountered. If coloring is disabled this is a no-op.

func StyleString

func StyleString(s string) string

StyleString replaces all supported style attributes of the from "{attr1,attr2}" in s with the corresponding ANSI escape sequences. If an attribute is not recognized it is not replaced. If styles are disabled, style attributes are replaced with empty strings. This supports all attribute names defined in AttributeMap.

Types

type Attribute

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

Attribute is an attribute of an ANSI escape sequence.

type SimpleAttribute

type SimpleAttribute uint8

SimpleAttribute is an attribute that is just one uint8 value.

const (
	Reset SimpleAttribute = iota
	Bold
	Faint
	Italic
	Underline
	BlinkSlow
	BlinkRapid
	ReverseVideo
	Concealed
	CrossedOut
)

Base style attributes

const (
	DefaultFont SimpleAttribute = iota + 10
	AltFont1
	AltFont2
	AltFont3
	AltFont4
	AltFont5
	AltFont6
	AltFont7
	AltFont8
	AltFont9
)

Font attributes

const (
	Fraktur SimpleAttribute = iota + 20
	DoubleUnderline
	Normal
	NoFraktur
	NoUnderline
	NoBlink
	ProportionalSpacing
	NoReverse
	Reveal
	NoCrossedOut
)

Style reset attributes

const (
	FgBlack SimpleAttribute = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta
	FgCyan
	FgWhite
	FgColor
	FgDefault
)

Foreground text colors

const (
	BgBlack SimpleAttribute = iota + 40
	BgRed
	BgGreen
	BgYellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
	BgColor
	BgDefault
)

Background text colors

const (
	FgHiBlack SimpleAttribute = iota + 90
	FgHiRed
	FgHiGreen
	FgHiYellow
	FgHiBlue
	FgHiMagenta
	FgHiCyan
	FgHiWhite
)

Foreground Hi-Intensity text colors

const (
	BgHiBlack SimpleAttribute = iota + 100
	BgHiRed
	BgHiGreen
	BgHiYellow
	BgHiBlue
	BgHiMagenta
	BgHiCyan
	BgHiWhite
)

Background Hi-Intensity text colors

type Style

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

Style can style and color text.

func Bg256

func Bg256(color uint8) *Style

Bg256 creates a background 256color attribute.

func BgHex

func BgHex(v uint32) *Style

BgHex creates a foreground RGB color attribute from a hex value.

func BgRGB

func BgRGB(r, g, b uint8) *Style

BgRGB creates a background RGB color attribute.

func Fg256

func Fg256(color uint8) *Style

Fg256 creates a foreground 256color attribute.

func FgHex

func FgHex(v uint32) *Style

FgHex creates a foreground RGB color attribute from a hex value.

func FgRGB

func FgRGB(r, g, b uint8) *Style

FgRGB creates a foreground RGB color attribute.

func New

func New(attrs ...Attribute) *Style

New creates a new *Style from given attributes.

func (*Style) Add

func (s *Style) Add(attrs ...Attribute) *Style

Add adds attributes to an existing style.

func (*Style) Copy

func (s *Style) Copy() *Style

func (*Style) Fprint

func (s *Style) Fprint(w io.Writer, args ...interface{}) (n int, err error)

Fprint formats using the default formats for its operands and writes to w. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

func (*Style) Fprintf

func (s *Style) Fprintf(w io.Writer, format string, args ...interface{}) (n int, err error)

Fprintf formats according to a format specifier and writes to w. It returns the number of bytes written and any write error encountered.

func (*Style) Fprintln

func (s *Style) Fprintln(w io.Writer, args ...interface{}) (n int, err error)

Fprintln formats using the default formats for its operands and writes to w. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

func (*Style) NewWith

func (s *Style) NewWith(attrs ...Attribute) *Style

NewWith creates a new *Style from s with additional attributes. Style s is not altered.

func (*Style) Print

func (s *Style) Print(args ...interface{}) (n int, err error)

Print formats using the default formats for its operands and writes to standard output. Spaces are added between operands when neither is a string. It returns the number of bytes written and any write error encountered.

func (*Style) Printf

func (s *Style) Printf(format string, args ...interface{}) (n int, err error)

Printf formats according to a format specifier and writes to standard output. It returns the number of bytes written and any write error encountered.

func (*Style) Println

func (s *Style) Println(args ...interface{}) (n int, err error)

Println formats using the default formats for its operands and writes to standard output. Spaces are always added between operands and a newline is appended. It returns the number of bytes written and any write error encountered.

func (*Style) Sprint

func (s *Style) Sprint(args ...interface{}) string

Sprint formats using the default formats for its operands and returns the resulting string. Spaces are added between operands when neither is a string.

func (*Style) Sprintf

func (s *Style) Sprintf(format string, args ...interface{}) string

Sprintf formats according to a format specifier and returns the resulting string.

func (*Style) Sprintln

func (s *Style) Sprintln(args ...interface{}) string

Sprintln formats using the default formats for its operands and returns the resulting string. Spaces are always added between operands and a newline is appended.

Jump to

Keyboard shortcuts

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