color

package
v0.6.1 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2024 License: Apache-2.0 Imports: 12 Imported by: 8

Documentation

Overview

Package color provides a wrapped standard output device like printf but with colored enhancements.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Colored

func Colored(clr Color, format string, args ...any)

Colored outputs formatted message to stdout while logger level less than level.WarnLevel. For level.SetLevel(level.ErrorLevel), the text will be discarded.

func Coloredf

func Coloredf(clr Color, format string, args ...any)

Coloredf outputs formatted message to stdout while logger level less than level.WarnLevel and cmdr is in VERBOSE mode.

func Dim

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

Dim outputs formatted message to stdout while logger level less than level.WarnLevel.

For example, after level.SetLevel(level.ErrorLevel), the text via Dim will be discarded.

func Dimf

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

Dimf outputs formatted message to stdout while logger level less than level.WarnLevel and cmdr is in verbose mode.

For example, after level.SetLevel(level.ErrorLevel), the text via Dimf will be discarded.

While env-var VERBOSE=1, the text via Dimf will be shown.

func Highlight

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

Highlight outputs formatted message to stdout while logger level less than level.WarnLevel. For level.SetLevel(level.ErrorLevel), the text will be discarded.

func ResetColor

func ResetColor(c Color)

func StripHTMLTags

func StripHTMLTags(s string) string

StripHTMLTags aggressively strips HTML tags from a string. It will only keep anything between `>` and `<`.

func StripLeftTabs

func StripLeftTabs(s string) string

StripLeftTabs strips the least left side tab chars from lines. StripLeftTabs strips html tags too.

func StripLeftTabsC

func StripLeftTabsC(s string) string

StripLeftTabsC strips the least left side tab chars from lines. StripLeftTabsC strips html tags too. At the end, StripLeftTabsC try translate color code in string.

func StripLeftTabsOnly

func StripLeftTabsOnly(s string) string

StripLeftTabsOnly strips the least left side tab chars from lines.

func Text

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

Text prints formatted message without any predefined ansi escaping.

func ToColor

func ToColor(clr Color, format string, args ...any) (str string)

func ToColorString added in v0.5.5

func ToColorString(clr Color) string

func ToDim

func ToDim(format string, args ...any) (str string)

func ToHighlight

func ToHighlight(format string, args ...any) (str string)

func WrapColorAndBgTo

func WrapColorAndBgTo(out io.Writer, clr, bg Color, text string)

func WrapColorTo

func WrapColorTo(out io.Writer, clr Color, text string)

func WrapDimTo

func WrapDimTo(out io.Writer, text string)

func WrapDimToLite

func WrapDimToLite(out io.Writer, text string)

func WrapHighlightTo

func WrapHighlightTo(out io.Writer, text string)

Types

type Color

type Color int // ANSI Escaped Sequences here
const (

	// FgBlack terminal color code
	FgBlack Color = 30
	// FgRed terminal color code
	FgRed Color = 31
	// FgGreen terminal color code
	FgGreen Color = 32
	// FgYellow terminal color code
	FgYellow Color = 33
	// FgBlue terminal color code
	FgBlue Color = 34
	// FgMagenta terminal color code
	FgMagenta Color = 35
	// FgCyan terminal color code
	FgCyan Color = 36
	// FgLightGray terminal color code (White)
	FgLightGray Color = 37

	// FgDarkGray terminal color code (Gray, Light Black).
	//
	// A highlight/bright black color, maybe 50% gray. See FgLightBlack.
	FgDarkGray Color = 90
	// FgLightBlack terminal color code (Gray, Light Black).
	//
	// A highlight/bright black color, maybe 50% gray. See FgDarkGray.
	FgLightBlack Color = 90
	// FgLightRed terminal color code
	FgLightRed Color = 91
	// FgLightGreen terminal color code
	FgLightGreen Color = 92
	// FgLightYellow terminal color code
	FgLightYellow Color = 93
	// FgLightBlue terminal color code
	FgLightBlue Color = 94
	// FgLightMagenta terminal color code
	FgLightMagenta Color = 95
	// FgLightCyan terminal color code
	FgLightCyan Color = 96
	// FgWhite terminal color code (Light White)
	FgWhite Color = 97

	// BgBlack terminal color code
	BgBlack Color = 40
	// BgRed terminal color code
	BgRed Color = 41
	// BgGreen terminal color code
	BgGreen Color = 42
	// BgYellow terminal color code
	BgYellow Color = 43
	// BgBlue terminal color code
	BgBlue Color = 44
	// BgMagenta terminal color code
	BgMagenta Color = 45
	// BgCyan terminal color code
	BgCyan Color = 46
	// BgLightGray terminal color code
	BgLightGray Color = 47
	// BgDarkGray terminal color code
	BgDarkGray Color = 100
	// BgLightRed terminal color code
	BgLightRed Color = 101
	// BgLightGreen terminal color code
	BgLightGreen Color = 102
	// BgLightYellow terminal color code
	BgLightYellow Color = 103
	// BgLightBlue terminal color code
	BgLightBlue Color = 104
	// BgLightMagenta terminal color code
	BgLightMagenta Color = 105
	// BgLightCyan terminal color code
	BgLightCyan Color = 106
	// BgWhite terminal color code
	BgWhite Color = 107

	// BgNormal terminal color code.
	//
	// All attributes become turned off.
	BgNormal Color = 0
	// BgBoldOrBright terminal color code
	//
	// Bold or increased intensity
	BgBoldOrBright Color = 1
	// BgDim terminal color code.
	//
	// Faint, decreased intensity, or dim.
	// May be implemented as a light font weight like bold.
	BgDim Color = 2
	// BgItalic terminal color code.
	//
	// Not widely supported. Sometimes treated as inverse or blink
	BgItalic Color = 3
	// BgUnderline terminal color code.
	//
	// Style extensions exist for Kitty, VTE, mintty, iTerm2 and Konsole.
	BgUnderline Color = 4
	// BgBlink terminal color code.
	//
	// Slow blink, Sets blinking to less than 150 times per minute.
	// But in many tty it's no effect.
	//
	// Sometimes it can be used for switching to 'normal' bg state without
	// reset all fg and bg settings (if using bg code 0)
	BgBlink Color = 5
	// BgRapidBlink terminal color code.
	//
	// MS-DOS ANSI.SYS, 150+ per minute; not widely supported.
	//
	// Sometimes it can be used for switching to 'normal' bg state without
	// reset all fg and bg settings (if using bg code 0)
	BgRapidBlink Color = 6
	// BgInverse terminal color code.
	//
	// Swap foreground and background colors; inconsistent emulation
	BgInverse Color = 7
	// BgHidden terminal color code.
	//
	// not widely supported.
	BgHidden Color = 8
	// BgStrikeout terminal color code.
	//
	// marked as if for deletion.
	BgStrikeout Color = 9

	FgDarkColor = FgLightGray

	FgDefault Color = 39
	BgDefault Color = 49

	// NoColor is not a declared ansi code but we can use it for identifying
	// a variable isn't initializing yet.
	NoColor Color = -1
)

func ToColorInt added in v0.5.5

func ToColorInt(s string) Color

type Index added in v0.5.20

type Index struct{}

Index is an indexer for retrieving the entries in this subpackage.

func (Index) ColoredFast added in v0.5.20

func (Index) ColoredFast(out io.Writer, clr Color, text string)

func (Index) DimFast added in v0.5.20

func (Index) DimFast(out io.Writer, text string)

func (Index) GetColorTranslater added in v0.5.20

func (Index) GetColorTranslater() Translator

GetColorTranslater returns a translator about ANSI Escaped Code. It may or may not translating colored text depending on cabin.GetNoColorMode.

func (Index) GetColorTranslaterAlways added in v0.5.20

func (Index) GetColorTranslaterAlways() Translator

func (Index) GetColorTranslaterNever added in v0.5.20

func (Index) GetColorTranslaterNever() Translator

func (Index) GetDummyTranslater added in v0.5.20

func (Index) GetDummyTranslater() Translator

func (Index) HighlightFast added in v0.5.20

func (Index) HighlightFast(out io.Writer, text string)

func (Index) ToColorInt added in v0.5.20

func (Index) ToColorInt(s string) Color

func (Index) ToColorString added in v0.5.20

func (Index) ToColorString(clr Color) string

func (Index) WrapColorAndBgTo added in v0.5.20

func (Index) WrapColorAndBgTo(out io.Writer, clr, bg Color, text string)

func (Index) WrapDimToLite added in v0.5.20

func (Index) WrapDimToLite(out io.Writer, text string)

type Translator

type Translator interface {
	Translate(s string, initialFg Color) string

	ColoredFast(out io.Writer, clr Color, text string)
	DimFast(out io.Writer, text string)
	HighlightFast(out io.Writer, text string)

	WriteColor(out io.Writer, clr Color)   // echo ansi color bytes for foreground color
	WriteBgColor(out io.Writer, clr Color) // echo ansi color bytes for background color
	Reset(out io.Writer)                   // echo ansi color bytes for resetting color
}

Translator _

func GetCPT

func GetCPT() Translator

GetCPT returns a translator about ANSI Escaped Code. It may or may not translating colored text depending on cabin.GetNoColorMode.

func GetCPTC

func GetCPTC() Translator

func GetCPTNC

func GetCPTNC() Translator

func GetDummyTranslator

func GetDummyTranslator() Translator

Jump to

Keyboard shortcuts

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