color

package
v1.3.2 Latest Latest
Warning

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

Go to latest
Published: Jan 8, 2021 License: Apache-2.0 Imports: 4 Imported by: 2

Documentation

Index

Constants

View Source
const (
	Red     = FgRed
	Cyan    = FgCyan
	Gray    = FgDarkGray // is light Black
	Blue    = FgBlue
	Black   = FgBlack
	Green   = FgGreen
	White   = FgWhite
	Yellow  = FgYellow
	Magenta = FgMagenta

	Bold   = OpBold
	Normal = FgDefault

	LightRed     = FgLightRed
	LightCyan    = FgLightCyan
	LightBlue    = FgLightBlue
	LightGreen   = FgLightGreen
	LightWhite   = FgLightWhite
	LightYellow  = FgLightYellow
	LightMagenta = FgLightMagenta
)

There are basic and light foreground color aliases

View Source
const (
	TplShowColor = "\x1b[%sm%s\x1b[0m" // 颜色模板
	TplColorSet  = "\x1b[%sm"
	TplResetSet  = "\x1b[0m"        // 重置/正常 关闭所有属性。
	CodeExpr     = `\033\[[\d;?]+m` // 清理颜色代码正则, 例如 "\033[1;36mText\x1b[0m"
)

Variables

View Source
var (
	// Trace color style
	Trace = &Theme{"trace", Style{OpReset, FgBlue}}
	// Debug color style
	Debug = &Theme{"debug", Style{OpReset, FgDefault}}
	// Info color style
	Info = &Theme{"info", Style{OpReset, FgCyan}}
	// Warn color style
	Warn = &Theme{"warn", Style{OpBold, FgYellow}}
	// Error color style
	Error = &Theme{"error", Style{OpBold, FgRed}}
	// Question color style
	Question = &Theme{"question", Style{OpBold, FgMagenta}}
	// Success color style
	Success = &Theme{"success", Style{OpBold, FgGreen}}
	// Danger color style
	Fatal = &Theme{"danger", Style{FgLightWhite, BgRed}}

	// Primary color style
	Primary = &Theme{"primary", Style{OpReset, FgLightBlue}}
	// Secondary color style
	Comment = &Theme{"secondary", Style{OpReset, FgDarkGray}}
	// Note color style
	Note = &Theme{"note", Style{OpBold, FgLightCyan}}
)

internal themes(like bootstrap style) Usage:

color.Info.Print("message")
color.Info.Printf("a %s message", "test")
color.Warn.Println("message")
color.Error.Println("message")
View Source
var BgColors = map[string]Color{
	"black":   BgBlack,
	"red":     BgRed,
	"green":   BgGreen,
	"yellow":  BgYellow,
	"blue":    BgBlue,
	"magenta": BgMagenta,
	"cyan":    BgCyan,
	"white":   BgWhite,
	"default": BgDefault,
}

BgColors background colors map

View Source
var ExBgColors = map[string]Color{
	"darkGray":     BgDarkGray,
	"lightRed":     BgLightRed,
	"lightGreen":   BgLightGreen,
	"lightYellow":  BgLightYellow,
	"lightBlue":    BgLightBlue,
	"lightMagenta": BgLightMagenta,
	"lightCyan":    BgLightCyan,
	"lightWhite":   BgLightWhite,
}

ExBgColors extra background colors map

View Source
var ExFgColors = map[string]Color{
	"darkGray":     FgDarkGray,
	"lightRed":     FgLightRed,
	"lightGreen":   FgLightGreen,
	"lightYellow":  FgLightYellow,
	"lightBlue":    FgLightBlue,
	"lightMagenta": FgLightMagenta,
	"lightCyan":    FgLightCyan,
	"lightWhite":   FgLightWhite,
}

ExFgColors extra foreground colors map

View Source
var FgColors = map[string]Color{
	"black":   FgBlack,
	"red":     FgRed,
	"green":   FgGreen,
	"yellow":  FgYellow,
	"blue":    FgBlue,
	"magenta": FgMagenta,
	"cyan":    FgCyan,
	"white":   FgWhite,
	"default": FgDefault,
}

FgColors foreground colors map

View Source
var Options = map[string]Color{
	"reset":      OpReset,
	"bold":       OpBold,
	"fuzzy":      OpFuzzy,
	"italic":     OpItalic,
	"underscore": OpUnderscore,
	"blink":      OpBlink,
	"reverse":    OpReverse,
	"concealed":  OpConcealed,
}

Options color options map

View Source
var Styles = map[string]Style{
	"info":  {OpReset, FgGreen},
	"note":  {OpBold, FgLightCyan},
	"light": {FgLightWhite, BgRed},
	"error": {OpBold, FgRed},

	"danger":  {OpBold, FgRed},
	"notice":  {OpBold, FgCyan},
	"success": {OpBold, FgGreen},
	"comment": {OpReset, FgMagenta},
	"primary": {OpReset, FgBlue},
	"warning": {OpBold, FgYellow},

	"question":  {OpReset, FgMagenta},
	"secondary": {FgDarkGray},
}

Styles internal defined styles, like bootstrap styles. Usage:

color.Styles["info"].Println("message")
View Source
var Themes = map[string]*Theme{
	"trace": Trace,
	"debug": Debug,
	"info":  Info,
	"warn":  Warn,
	"error": Error,
	"fatal": Fatal,

	"note":     Note,
	"success":  Success,
	"comment":  Comment,
	"primary":  Primary,
	"question": Question,
}

Themes internal defined themes. Usage:

color.Themes["info"].Println("message")

Functions

func AddStyle

func AddStyle(name string, s Style)

AddStyle add a style

func AddTheme

func AddTheme(name string, style Style)

AddTheme add a theme and style

func ClearCode

func ClearCode(str string) string

ClearCode 清除色彩. eg: "\033[36;1mText\x1b[0m" -> "Text"

func RenderCode

func RenderCode(code string, args ...interface{}) string

RenderCode render message by color code.

func RenderStr

func RenderStr(code string, str string) string

RenderStr 渲染字符串输出颜色

func Reset

func Reset() (int, error)

Reset reset console color attributes

func Set

func Set(colors ...Color) (int, error)

Set set console color attributes

Types

type Color

type Color uint8

Color Color16, 16 color value type 3(2^3=8) OR 4(2^4=16) bite color.

const (
	FgBlack Color = iota + 30
	FgRed
	FgGreen
	FgYellow
	FgBlue
	FgMagenta // 品红
	FgCyan    // 青色
	FgWhite
	// FgDefault revert default FG
	FgDefault Color = 39
)

Foreground colors. basic foreground colors 30 - 37

const (
	FgDarkGray Color = iota + 90 // 亮黑(灰)
	FgLightRed
	FgLightGreen
	FgLightYellow
	FgLightBlue
	FgLightMagenta
	FgLightCyan
	FgLightWhite
	// FgGray is alias of FgDarkGray
	FgGray Color = 90 // 亮黑(灰)
)

Extra foreground color 90 - 97(非标准)

const (
	BgBlack Color = iota + 40
	BgRed
	BgGreen
	BgYellow // BgBrown like yellow
	BgBlue
	BgMagenta
	BgCyan
	BgWhite
	// BgDefault revert default BG
	BgDefault Color = 49
)

Background colors. basic background colors 40 - 47

const (
	BgDarkGray Color = iota + 99
	BgLightRed
	BgLightGreen
	BgLightYellow
	BgLightBlue
	BgLightMagenta
	BgLightCyan
	BgLightWhite
	// BgGray is alias of BgDarkGray
	BgGray Color = 100
)

Extra background color 100 - 107(非标准)

const (
	OpReset         Color = iota // 0 重置所有设置
	OpBold                       // 1 加粗
	OpFuzzy                      // 2 模糊(不是所有的终端仿真器都支持)
	OpItalic                     // 3 斜体(不是所有的终端仿真器都支持)
	OpUnderscore                 // 4 下划线
	OpBlink                      // 5 闪烁
	OpFastBlink                  // 5 快速闪烁(未广泛支持)
	OpReverse                    // 7 颠倒的 交换背景色与前景色
	OpConcealed                  // 8 隐匿的
	OpStrikethrough              // 9 删除的,删除线(未广泛支持)
)

Option settings

func (Color) Code

func (c Color) Code() string

Code 转换颜色编码为字符串

func (Color) Darken

func (c Color) Darken() Color

Darken 更改为暗色

func (Color) IsValid

func (c Color) IsValid() bool

IsValid 颜色值是否有效

func (Color) Light

func (c Color) Light() Color

Light 更改为亮色

func (Color) Render

func (c Color) Render(a ...interface{}) string

Render messages by color setting

func (Color) Text

func (c Color) Text(message string) string

Text render a text message

type Style

type Style []Color

func GetStyle

func GetStyle(name string) Style

GetStyle get defined style by name

func New

func New(colors ...Color) Style

func (Style) Code

func (s Style) Code() string

Code convert to code string. returns like "32;45;3"

func (Style) IsEmpty

func (s Style) IsEmpty() bool

IsEmpty style

func (Style) Render

func (s Style) Render(a ...interface{}) string

Render messages by color setting

func (Style) Save

func (s Style) Save(name string)

Save to styles map

func (Style) Text

func (s Style) Text(format string, a ...interface{}) string

Text render a text message

type Theme

type Theme struct {
	// Name theme name
	Name string
	// Style for the theme
	Style
}

Theme definition. extends from Style

func GetTheme

func GetTheme(name string) *Theme

GetTheme get defined theme by name

func NewTheme

func NewTheme(name string, style Style) *Theme

NewTheme instance

func (*Theme) Save

func (t *Theme) Save()

Save to themes map

Jump to

Keyboard shortcuts

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