color

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Nov 1, 2018 License: MIT Imports: 6 Imported by: 84

README

CLI Color

GoDoc Build Status Coverage Status Go Report Card

A command-line color library with true color support, universal API methods and Windows support.

中文说明

Basic color preview:

basic-color

Features

  • Simple to use, zero dependencies
  • Supports rich color output: 16-color, 256-color, true color (24-bit)
    • 16-color output is the most commonly used and most widely supported, working on any Windows version
    • See this gist for information on true color support
  • Generic API methods: Print, Printf, Println, Sprint, Sprintf
  • Supports HTML tag-style color rendering, such as <green>message</>
  • Basic colors: Bold, Black, White, Gray, Red, Green, Yellow, Blue, Magenta, Cyan
  • Additional styles: Info, Note, Light, Error, Danger, Notice, Success, Comment, Primary, Warning, Question, Secondary

GoDoc

Quick start

import "gopkg.in/gookit/color.v1" // is recommended
// or
import "github.com/gookit/color"
package main

import (
	"fmt"
	"github.com/gookit/color"
)

func main() {
	// simple usage
	color.Cyan.Printf("Simple to use %s\n", "color")

	// use like func
	red := color.FgRed.Render
	green := color.FgGreen.Render
	fmt.Printf("%s line %s library\n", red("Command"), green("color"))

	// custom color
	color.New(color.FgWhite, color.BgBlack).Println("custom color style")

	// can also:
	color.Style{color.FgCyan, color.OpBold}.Println("custom color style")

	// internal theme/style:
	color.Info.Tips("message")
	color.Info.Prompt("message")
	color.Info.Println("message")
	color.Warn.Println("message")
	color.Error.Println("message")

	// use style tag
	color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n")

	// apply a style tag
	color.Tag("info").Println("info style text")

	// prompt message
	color.Info.Prompt("prompt style message")
	color.Warn.Prompt("prompt style message")

	// tips message
	color.Info.Tips("tips style message")
	color.Warn.Tips("tips style message")
}

Run demo: go run ./_examples/demo.go

colored-out

Detailed usage

Basic color

Supported on any Windows version.

  • color.Bold
  • color.Black
  • color.White
  • color.Gray
  • color.Red
  • color.Green
  • color.Yellow
  • color.Blue
  • color.Magenta
  • color.Cyan
color.Bold.Println("bold message")
color.Yellow.Println("yellow message")

Run demo: go run ./_examples/basiccolor.go

basic-color

Additional styles

Supported on any Windows version.

  • color.Info
  • color.Note
  • color.Light
  • color.Error
  • color.Danger
  • color.Notice
  • color.Success
  • color.Comment
  • color.Primary
  • color.Warning
  • color.Question
  • color.Secondary
// print message
color.Info.Print("Info message")
color.Success.Print("Success message")

// prompt message
color.Info.Prompt("prompt style message")
color.Warn.Prompt("prompt style message")

// tips message
color.Info.Tips("tips style message")
color.Warn.Tips("tips style message")

Run demo: go run ./_examples/theme_style.go

theme-style

HTML-like tag usage

Not supported on Windows (tags will be stripped).

// use style tag
color.Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>")
color.Println("<suc>hello</>")
color.Println("<error>hello</>")
color.Println("<warning>hello</>")

// custom color attributes
color.Print("<fg=yellow;bg=black;op=underscore;>hello, welcome</>\n")
  • color.Tag
// set a style tag
color.Tag("info").Print("info style text")
color.Tag("info").Printf("%s style text", "info")
color.Tag("info").Println("info style text")

Run demo: go run ./_examples/colortag.go

color-tags

256-color usage

Set the foreground or background color
  • color.C256(val uint8, isBg ...bool) Color256
c := color.C256(132) // fg color
c.Println("message")
c.Printf("format %s", "message")

c := color.C256(132, true) // bg color
c.Println("message")
c.Printf("format %s", "message")
Use a 256-color style

Can be used to set foreground and background colors at the same time.

  • color.S256(fgAndBg ...uint8) *Style256
s := color.S256(32, 203)
s.Println("message")
s.Printf("format %s", "message")

Run demo: go run ./_examples/color256.go

color-tags

Use RGB color

Set the foreground or background color
  • color.RGB(r, g, b uint8, isBg ...bool) RGBColor
c := color.RGB(30,144,255) // fg color
c.Println("message")
c.Printf("format %s", "message")

c := color.RGB(30,144,255, true) // bg color
c.Println("message")
c.Printf("format %s", "message")

Create a style from an hexadecimal color string:

  • color.HEX(hex string, isBg ...bool) RGBColor
c := HEX("ccc") // can also: "cccccc" "#cccccc"
c.Println("message")
c.Printf("format %s", "message")

c = HEX("aabbcc", true) // as bg color
c.Println("message")
c.Printf("format %s", "message")
Use a RGB color style

Can be used to set the foreground and background colors at the same time.

  • color.NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyle
s := NewRGBStyle(RGB(20, 144, 234), RGB(234, 78, 23))
s.Println("message")
s.Printf("format %s", "message")

Create a style from an hexadecimal color string:

  • color.HEXStyle(fg string, bg ...string) *RGBStyle
s := HEXStyle("11aa23", "eee")
s.Println("message")
s.Printf("format %s", "message")

See also

License

MIT

Documentation

Overview

Package color is Command line color library. Support rich color rendering output, universal API method, compatible with Windows system

Source code and other details for the project are available at GitHub:

https://github.com/gookit/color

More usage please see README and tests.

Example
// simple usage
Cyan.Printf("Simple to use %s\n", "color")

// use like func
red := FgRed.Render
green := FgGreen.Render
fmt.Printf("%s line %s library\n", red("Command"), green("color"))

// custom color
New(FgWhite, BgBlack).Println("custom color style")

// can also:
Style{FgCyan, OpBold}.Println("custom color style")

// internal theme/style:
Info.Tips("message")
Info.Prompt("message")
Info.Println("info message")
Warn.Println("warning message")
Error.Println("error message")
Danger.Println("danger message")

// use style tag
Print("<suc>he</><comment>llo</>, <cyan>wel</><red>come</>\n")

// apply a style tag
Tag("info").Println("info style text")

// prompt message
Info.Prompt("prompt style message")
Warn.Prompt("prompt style message")

// tips message
Info.Tips("tips style message")
Warn.Tips("tips style message")
Output:

Index

Examples

Constants

View Source
const (
	SettingTpl   = "\x1b[%sm"
	FullColorTpl = "\x1b[%sm%s\x1b[0m"
)

color render templates ESC 操作的表示:

"\033"(Octal 8进制) = "\x1b"(Hexadecimal 16进制) = 27 (10进制)
View Source
const (
	Red     = FgRed
	Cyan    = FgCyan
	Gray    = FgDarkGray // is light Black
	Blue    = FgBlue
	Black   = FgBlack
	Green   = FgGreen
	White   = FgWhite
	Yellow  = FgYellow
	Magenta = FgMagenta
	// special
	Bold   = OpBold
	Normal = FgDefault
	// extra light
	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 (
	TplFg256 = "38;5;%d"
	TplBg256 = "48;5;%d"
)

tpl for 8 bit 256 color(`2^8`)

format:

	ESC[ … 38;5;<n> … m // 选择前景色
 ESC[ … 48;5;<n> … m // 选择背景色

example:

fg "\x1b[38;5;242m"
bg "\x1b[48;5;208m"
both "\x1b[38;5;242;48;5;208m"

links:

https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97#8位
View Source
const (
	TplFgRGB = "38;2;%d;%d;%d"
	TplBgRGB = "48;2;%d;%d;%d"
)

24 bit RGB color RGB:

R 0-255 G 0-255 B 0-255
R 00-FF G 00-FF B 00-FF (16进制)

Format:

ESC[ … 38;2;<r>;<g>;<b> … m // 选择RGB前景色
ESC[ … 48;2;<r>;<g>;<b> … m // 选择RGB背景色

links:

https://zh.wikipedia.org/wiki/ANSI%E8%BD%AC%E4%B9%89%E5%BA%8F%E5%88%97#24位

example:

fg: \x1b[38;2;30;144;255mMESSAGE\x1b[0m
bg: \x1b[48;2;30;144;255mMESSAGE\x1b[0m
both: \x1b[38;2;233;90;203;48;2;30;144;255mMESSAGE\x1b[0m
View Source
const (
	AsFg uint8 = iota
	AsBg
)

mark color is fg or bg.

View Source
const (
	// Regex to match color tags
	// golang 不支持反向引用.  即不支持使用 \1 引用第一个匹配 ([a-z=;]+)
	// MatchExpr = `<([a-z=;]+)>(.*?)<\/\1>`
	// 所以调整一下 统一使用 `</>` 来结束标签,例如 "<info>some text</>"
	// 支持自定义颜色属性的tag "<fg=white;bg=blue;op=bold>content</>"
	// (?s:...) s - 让 "." 匹配换行
	MatchExpr = `<([a-zA-Z_=,;]+)>(?s:(.*?))<\/>`

	// Regex to match color attributes
	AttrExpr = `(fg|bg|op)[\s]*=[\s]*([a-zA-Z,]+);?`

	// Regex used for removing color tags
	// StripExpr = `<[\/]?[a-zA-Z=;]+>`
	// 随着上面的做一些调整
	StripExpr = `<[\/]?[a-zA-Z_=,;]*>`
)

output colored text like use html tag. (not support windows cmd)

View Source
const CodeExpr = `\033\[[\d;?]+m`

CodeExpr regex to clear color codes eg "\033[1;36mText\x1b[0m"

View Source
const ResetSet = "\x1b[0m"

ResetSet 重置/正常 关闭所有属性。

Variables

View Source
var (
	// Info color style
	Info = &Theme{"info", Style{OpReset, FgGreen}}
	// Note color style
	Note = &Theme{"note", Style{OpBold, FgLightCyan}}
	// Warn color style
	Warn = &Theme{"warning", Style{OpBold, FgYellow}}
	// Light color style
	Light = &Theme{"light", Style{FgLightWhite, BgBlack}}
	// Error color style
	Error = &Theme{"error", Style{FgLightWhite, BgRed}}
	// Danger color style
	Danger = &Theme{"danger", Style{OpBold, FgRed}}
	// Notice color style
	Notice = &Theme{"notice", Style{OpBold, FgCyan}}
	// Comment color style
	Comment = &Theme{"comment", Style{OpReset, FgLightYellow}}
	// Success color style
	Success = &Theme{"success", Style{OpBold, FgGreen}}
	// Primary color style
	Primary = &Theme{"primary", Style{OpReset, FgBlue}}
	// Question color style
	Question = &Theme{"question", Style{OpReset, FgMagenta}}
	// Secondary color style
	Secondary = &Theme{"secondary", Style{FgDarkGray}}
)

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 Enable = true

Enable switch color display

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": {FgLightWhite, BgRed},

	"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{
	"info":  Info,
	"note":  Note,
	"light": Light,
	"error": Error,

	"danger":  Danger,
	"notice":  Notice,
	"success": Success,
	"comment": Comment,
	"primary": Primary,
	"warning": Warn,

	"question":  Question,
	"secondary": Secondary,
}

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 added in v1.1.1

func AddTheme(name string, style Style)

AddTheme add a theme and style

func ApplyTag

func ApplyTag(tag string, a ...interface{}) string

ApplyTag for messages

func ClearCode

func ClearCode(str string) string

ClearCode clear color codes. eg:

"\033[36;1mText\x1b[0m" -> "Text"

func ClearTag

func ClearTag(s string) string

ClearTag clear all tag for a string

func Disable

func Disable()

Disable disable color output

func Fprint

func Fprint(w io.Writer, a ...interface{})

Fprint print rendered messages to writer

func Fprintf

func Fprintf(w io.Writer, format string, a ...interface{}) (int, error)

Fprintf print format and rendered messages to writer

func Fprintln

func Fprintln(w io.Writer, a ...interface{}) (int, error)

Fprintln print rendered messages line to writer

func GetColorTags

func GetColorTags() map[string]string

GetColorTags get all internal color tags

func GetTagCode added in v1.1.2

func GetTagCode(name string) string

GetTagCode get color code by tag name

func HexToRGB added in v1.1.2

func HexToRGB(hex string) (rgb []int)

HexToRGB hex color string to RGB numbers Usage:

rgb := HexToRGB("ccc") // rgb: [204 204 204]
rgb := HexToRGB("aabbcc") // rgb: [170 187 204]
rgb := HexToRGB("#aabbcc") // rgb: [170 187 204]
rgb := HexToRGB("0xad99c0") // rgb: [170 187 204]

func IsConsole added in v1.1.2

func IsConsole(out io.Writer) bool

IsConsole 判断 w 是否为 stderr、stdout、stdin 三者之一

func IsDefinedTag

func IsDefinedTag(name string) bool

IsDefinedTag is defined tag name

func IsMSys added in v1.1.2

func IsMSys() bool

IsMSys msys(MINGW64) 环境,不一定支持颜色

func IsSupport256Color added in v1.1.2

func IsSupport256Color() bool

IsSupport256Color render

func IsSupportColor added in v1.1.2

func IsSupportColor() bool

IsSupportColor check current console is support color.

Supported:

linux, mac, or windows's ConEmu, Cmder, putty, git-bash.exe

Not support:

windows cmd.exe, powerShell.exe

func ParseCodeFromAttr

func ParseCodeFromAttr(attr string) (code string)

ParseCodeFromAttr parse color attributes. attr like:

"fg=VALUE;bg=VALUE;op=VALUE" // VALUE please see var: FgColors, BgColors, Options

eg:

"fg=yellow"
"bg=red"
"op=bold,underscore" option is allow multi value
"fg=white;bg=blue;op=bold"
"fg=white;op=bold,underscore"

func Print

func Print(a ...interface{})

Print messages

func Printf

func Printf(format string, a ...interface{})

Printf format and print messages

func Println

func Println(a ...interface{})

Println messages with new line

func Render

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

Render return rendered string

func RenderCode added in v1.1.2

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

RenderCode render message by color code. Usage:

msg := RenderCode("3;32;45", "some", "message")

func RenderString added in v1.1.2

func RenderString(code string, str string) string

RenderString render a string with color code. Usage:

msg := RenderString("3;32;45", "a message")

func ReplaceTag

func ReplaceTag(str string) string

ReplaceTag parse string, replace color tag and return rendered string

func Reset

func Reset() (int, error)

Reset reset console color attributes

func Set

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

Set set console color attributes

func Sprint added in v1.1.1

func Sprint(args ...interface{}) string

Sprint return rendered string

func Sprintf added in v1.1.1

func Sprintf(format string, a ...interface{}) string

Sprintf format and return rendered string

func String added in v1.1.1

func String(s string) string

String alias of the ReplaceTag

func Text added in v1.1.2

func Text(s string) string

Text alias of the ReplaceTag

func WrapTag

func WrapTag(s string, tag string) string

WrapTag wrap a tag for a string "<tag>content</>"

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) Darken added in v1.1.4

func (c Color) Darken() Color

Darken current color. eg. 96(FgLightCyan) -> 36(FgCyan) Usage:

cyan := LightCyan.Darken()
cyan.Print("message")

func (Color) IsValid

func (c Color) IsValid() bool

IsValid color value

func (Color) Light added in v1.1.4

func (c Color) Light() Color

Light current color. eg: 36(FgCyan) -> 96(FgLightCyan). Usage:

lightCyan := Cyan.Light()
lightCyan.Print("message")

func (Color) Print

func (c Color) Print(args ...interface{})

Print messages. Usage:

color.Green.Print("message")

OR:

green := color.FgGreen.Print
green("message")

func (Color) Printf

func (c Color) Printf(format string, a ...interface{})

Printf format and print messages. usage:

color.Cyan.Printf("string %s", "arg0")

func (Color) Println

func (c Color) Println(a ...interface{})

Println messages with new line

func (Color) Render

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

Render messages by color setting usage:

green := color.FgGreen.Render
fmt.Println(green("message"))

func (Color) Sprint added in v1.1.2

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

Sprint render messages by color setting. is alias of the Render()

func (Color) Sprintf added in v1.1.2

func (c Color) Sprintf(format string, args ...interface{}) string

Sprintf format and render message. Usage:

	green := color.Green.Sprintf
 colored := green("message")

func (Color) String

func (c Color) String() string

String to code string. eg "35"

func (Color) Text added in v1.1.2

func (c Color) Text(message string) string

Text render a text message

type Color256 added in v1.1.2

type Color256 [2]uint8

Color256 256 (8 bit) color, uint8 range at 0 - 255

颜色值使用10进制和16进制都可 0x98 = 152

颜色有两位uint8组成,

	0: color value
	1: color type, Fg=0 Bg=1
	>1: unset value
	fg color: [152, 0]
 bg color: [152, 1]

func Bit8 added in v1.1.2

func Bit8(val uint8, isBg ...bool) Color256

Bit8 create a color256

func C256 added in v1.1.2

func C256(val uint8, isBg ...bool) Color256

C256 create a color256

func (Color256) IsEmpty added in v1.1.2

func (c Color256) IsEmpty() bool

IsEmpty value

func (Color256) Print added in v1.1.2

func (c Color256) Print(a ...interface{})

Print print message

func (Color256) Printf added in v1.1.2

func (c Color256) Printf(format string, a ...interface{})

Printf format and print message

func (Color256) Println added in v1.1.2

func (c Color256) Println(a ...interface{})

Println print message with newline

func (Color256) Sprint added in v1.1.2

func (c Color256) Sprint(a ...interface{}) string

Sprint returns rendered message

func (Color256) Sprintf added in v1.1.2

func (c Color256) Sprintf(format string, a ...interface{}) string

Sprintf returns format and rendered message

func (Color256) String added in v1.1.2

func (c Color256) String() string

String convert to color code string.

func (Color256) Value added in v1.1.2

func (c Color256) Value() uint8

Value return color value

type Printer added in v1.1.3

type Printer struct {
	// ColorCode color code string. eg "32;45;3"
	ColorCode string
}

Printer a generic color message printer. Usage:

p := &Printer{"32;45;3"}
p.Print("message")

func NewPrinter added in v1.1.4

func NewPrinter(colorCode string) *Printer

NewPrinter instance

func (*Printer) IsEmpty added in v1.1.4

func (p *Printer) IsEmpty() bool

IsEmpty color code

func (*Printer) Print added in v1.1.3

func (p *Printer) Print(a ...interface{})

Print rendering colored messages

func (*Printer) Printf added in v1.1.3

func (p *Printer) Printf(format string, a ...interface{})

Printf format and rendering colored messages

func (*Printer) Println added in v1.1.3

func (p *Printer) Println(a ...interface{})

Println rendering colored messages with newline

func (*Printer) Sprint added in v1.1.3

func (p *Printer) Sprint(a ...interface{}) string

Sprint returns rendering colored messages

func (*Printer) Sprintf added in v1.1.3

func (p *Printer) Sprintf(format string, a ...interface{}) string

Sprintf returns format and rendering colored messages

func (*Printer) String added in v1.1.3

func (p *Printer) String() string

String returns color code string. eg: "32;45;3"

type PrinterFace added in v1.1.3

type PrinterFace interface {
	fmt.Stringer
	Sprint(a ...interface{}) string
	Sprintf(format string, a ...interface{}) string
	Print(a ...interface{})
	Printf(format string, a ...interface{})
	Println(a ...interface{})
}

PrinterFace interface

type RGBColor added in v1.1.2

type RGBColor [4]uint8

RGBColor definition.

The first to third digits represent the color value. The last digit represents the foreground(0), background(1), >1 is unset value

Usage:

// 0, 1, 2 is R,G,B.
// 3th: Fg=0, Bg=1, >1: unset value
RGBColor{30,144,255, 0}
RGBColor{30,144,255, 1}

func HEX added in v1.1.2

func HEX(hex string, isBg ...bool) RGBColor

HEX create RGB color from a HEX color string. Usage:

c := HEX("ccc") // rgb: [204 204 204]
c := HEX("aabbcc") // rgb: [170 187 204]
c := HEX("#aabbcc")
c := HEX("0xaabbcc")
c.Print("message")

func RGB added in v1.1.2

func RGB(r, g, b uint8, isBg ...bool) RGBColor

RGB color create. Usage:

c := RGB(30,144,255)
c := RGB(30,144,255, true)
c.Print("message")

func RGBFromString added in v1.1.3

func RGBFromString(rgb string, isBg ...bool) RGBColor

RGBFromString create RGB color from a string. Usage:

c := RGBFromString("170,187,204")
c.Print("message")

func (RGBColor) IsEmpty added in v1.1.2

func (c RGBColor) IsEmpty() bool

IsEmpty value

func (RGBColor) Print added in v1.1.2

func (c RGBColor) Print(a ...interface{})

Print print message

func (RGBColor) Printf added in v1.1.2

func (c RGBColor) Printf(format string, a ...interface{})

Printf format and print message

func (RGBColor) Println added in v1.1.2

func (c RGBColor) Println(a ...interface{})

Println print message with newline

func (RGBColor) Sprint added in v1.1.2

func (c RGBColor) Sprint(a ...interface{}) string

Sprint returns rendered message

func (RGBColor) Sprintf added in v1.1.2

func (c RGBColor) Sprintf(format string, a ...interface{}) string

Sprintf returns format and rendered message

func (RGBColor) String added in v1.1.2

func (c RGBColor) String() string

String to color code string

func (RGBColor) Values added in v1.1.2

func (c RGBColor) Values() []int

Values to RGB values

type RGBStyle added in v1.1.2

type RGBStyle struct {
	// Name of the style
	Name string
	// contains filtered or unexported fields
}

RGBStyle definition.

前/背景色 都是由4位uint8组成, 前三位是色彩值; 最后一位与RGBColor不一样的是,在这里表示是否设置了值 1 表示已设置 ^1 未设置

func HEXStyle added in v1.1.3

func HEXStyle(fg string, bg ...string) *RGBStyle

HEXStyle create a RGBStyle from HEX color string. Usage:

s := HEXStyle("aabbcc", "eee")
s.Print("message")

func NewRGBStyle added in v1.1.3

func NewRGBStyle(fg RGBColor, bg ...RGBColor) *RGBStyle

NewRGBStyle create a RGBStyle.

func RGBStyleFromString added in v1.1.3

func RGBStyleFromString(fg string, bg ...string) *RGBStyle

RGBStyleFromString create a RGBStyle from color value string. Usage:

s := RGBStyleFromString("170,187,204", "70,87,4")
s.Print("message")

func (*RGBStyle) IsEmpty added in v1.1.3

func (s *RGBStyle) IsEmpty() bool

IsEmpty style

func (*RGBStyle) Print added in v1.1.3

func (s *RGBStyle) Print(a ...interface{})

Print print message

func (*RGBStyle) Printf added in v1.1.3

func (s *RGBStyle) Printf(format string, a ...interface{})

Printf format and print message

func (*RGBStyle) Println added in v1.1.3

func (s *RGBStyle) Println(a ...interface{})

Println print message with newline

func (*RGBStyle) Set added in v1.1.3

func (s *RGBStyle) Set(fg, bg RGBColor) *RGBStyle

Set fg and bg color

func (*RGBStyle) SetBg added in v1.1.3

func (s *RGBStyle) SetBg(bg RGBColor) *RGBStyle

SetBg set bg color

func (*RGBStyle) SetFg added in v1.1.3

func (s *RGBStyle) SetFg(fg RGBColor) *RGBStyle

SetFg set fg color

func (*RGBStyle) Sprint added in v1.1.3

func (s *RGBStyle) Sprint(a ...interface{}) string

Sprint returns rendered message

func (*RGBStyle) Sprintf added in v1.1.3

func (s *RGBStyle) Sprintf(format string, a ...interface{}) string

Sprintf returns format and rendered message

func (*RGBStyle) String added in v1.1.2

func (s *RGBStyle) String() string

String convert to color code string

type Style

type Style []Color

Style a 16 color style can add: fg color, bg color, color options Example:

color.Style(color.FgGreen).Print("message")

func GetStyle

func GetStyle(name string) Style

GetStyle get defined style by name

func New

func New(colors ...Color) Style

New create a custom style

func (Style) IsEmpty added in v1.1.0

func (s Style) IsEmpty() bool

IsEmpty style

func (Style) Print

func (s Style) Print(a ...interface{})

Print render and Print text

func (Style) Printf

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

Printf render and print text

func (Style) Println

func (s Style) Println(a ...interface{})

Println render and print text line

func (Style) Render

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

Render render text usage:

color.New(color.FgGreen).Render("text")
color.New(color.FgGreen, color.BgBlack, color.OpBold).Render("text")

func (Style) Save

func (s Style) Save(name string)

Save to styles map

func (Style) Sprint added in v1.1.0

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

Sprint is alias of the 'Render'

func (Style) Sprintf added in v1.1.3

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

Sprintf is alias of the 'Render'

func (Style) String added in v1.1.2

func (s Style) String() string

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

type Style256 added in v1.1.2

type Style256 struct {

	// Name of the style
	Name string
	// contains filtered or unexported fields
}

Style256 definition

前/背景色 都是由两位uint8组成, 第一位是色彩值; 第二位与Bit8Color不一样的是,在这里表示是否设置了值 0 未设置 ^0 已设置

func S256 added in v1.1.2

func S256(fgAndBg ...uint8) *Style256

S256 create a color256 style Usage:

s := color.S256()
s := color.S256(132) // fg
s := color.S256(132, 203) // fg and bg

func (*Style256) Print added in v1.1.2

func (s *Style256) Print(a ...interface{})

Print print message

func (*Style256) Printf added in v1.1.2

func (s *Style256) Printf(format string, a ...interface{})

Printf format and print message

func (*Style256) Println added in v1.1.2

func (s *Style256) Println(a ...interface{})

Println print message with newline

func (*Style256) Set added in v1.1.2

func (s *Style256) Set(fgVal, bgVal uint8) *Style256

Set fg and bg color value

func (*Style256) SetBg added in v1.1.2

func (s *Style256) SetBg(bgVal uint8) *Style256

SetBg set bg color value

func (*Style256) SetFg added in v1.1.2

func (s *Style256) SetFg(fgVal uint8) *Style256

SetFg set fg color value

func (*Style256) Sprint added in v1.1.2

func (s *Style256) Sprint(a ...interface{}) string

Sprint returns rendered message

func (*Style256) Sprintf added in v1.1.2

func (s *Style256) Sprintf(format string, a ...interface{}) string

Sprintf returns format and rendered message

func (*Style256) String added in v1.1.2

func (s *Style256) String() string

String convert to color code string

type Tag

type Tag string

Tag value is a defined style name

func (Tag) Print

func (tg Tag) Print(a ...interface{})

Print messages

func (Tag) Printf

func (tg Tag) Printf(format string, args ...interface{})

Printf format and print messages

func (Tag) Println

func (tg Tag) Println(a ...interface{})

Println messages line

func (Tag) Sprint added in v1.1.0

func (tg Tag) Sprint(a ...interface{}) string

Sprint render messages

func (Tag) Sprintf added in v1.1.4

func (tg Tag) Sprintf(format string, a ...interface{}) string

Sprintf format and render messages

type Theme added in v1.1.1

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

Theme definition. extends from Style

func GetTheme added in v1.1.1

func GetTheme(name string) *Theme

GetTheme get defined theme by name

func NewTheme added in v1.1.1

func NewTheme(name string, style Style) *Theme

NewTheme instance

func (*Theme) Block added in v1.1.1

func (t *Theme) Block(format string, a ...interface{})

Block like Prompt, but will wrap a empty line

func (*Theme) Prompt added in v1.1.1

func (t *Theme) Prompt(format string, a ...interface{})

Prompt use name as title, and apply style for message

func (*Theme) Save added in v1.1.2

func (t *Theme) Save()

Save to themes map

func (*Theme) Tips added in v1.1.1

func (t *Theme) Tips(format string, a ...interface{})

Tips use name as title, only apply style for name

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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