colors

package
v2.1.5 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2021 License: MIT Imports: 6 Imported by: 2

Documentation

Overview

Package colors 带色彩的控制台文本输出包

兼容 windows 平台。

// 输出段蓝底红字:foreground:Red;background:Blue
colors.Printf(colors.Red, colors.Blue, "foreground:%v;background:%v", colors.Red, colors.Blue)

// 功能同上,但是可以重复调用 Print* 系列函数输出内容。
c := colors.New(colors.Red, colors.Yellow)
c.Print("foreground:%v;background:%v")
c.Print(colors.Red, colors.Blue)

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Fprint

func Fprint(w io.Writer, t Type, foreground, background Color, v ...interface{}) (int, error)

Fprint 带色彩输出的 fmt.Fprint

颜色值只在 w 不为 os.Stderr、os.Stdin、os.Stdout 中的一个时才启作用,否则只向 w 输出普通字符串。

func Fprintf

func Fprintf(w io.Writer, t Type, foreground, background Color, format string, v ...interface{}) (int, error)

Fprintf 带色彩输出的 fmt.Fprintf

颜色值只在 w 不为 os.Stderr、os.Stdin、os.Stdout 中的一个时才启作用,否则只向 w 输出普通字符串。

func Fprintln

func Fprintln(w io.Writer, t Type, foreground, background Color, v ...interface{}) (int, error)

Fprintln 带色彩输出的 fmt.Fprintln

颜色值只在 w 不为 os.Stderr、os.Stdin、os.Stdout 中的一个时才启作用,否则只向 w 输出普通字符串。

func Print

func Print(t Type, foreground, background Color, v ...interface{}) (int, error)

Print 带色彩输出的 fmt.Print

func Printf

func Printf(t Type, foreground, background Color, format string, v ...interface{}) (int, error)

Printf 带色彩输出的 fmt.Printf

func Println

func Println(t Type, foreground, background Color, v ...interface{}) (int, error)

Println 带色彩输出的 fmt.Println

Types

type Color

type Color int32

Color 定义了控制台能接受的所有颜色值

具体颜色值在不同的平台上可能有一定的差异。

颜色定义分为以下几种: 默认色: math.MaxInt32 基本色: 0-7 增强色: 8-15 256 色: 0-256,其中 0-15 的数据会被转换成以上的色彩; 真彩色: 负数;

以上这样设置,可以最大限度地保证兼容性。

默认色、增强色和 256 色基本上所有的终端都支持, 而 24 位真彩色则未必所有终端都支持, 比如 macOS 自带的终端对该色彩支持并不好。

关于颜色的具体定义,可参考以下文章: https://en.wikipedia.org/wiki/ANSI_escape_code

const (
	Black         Color = iota // 黑色
	Red                        // 红色
	Green                      // 绿色
	Yellow                     // 黄色
	Blue                       // 蓝色
	Magenta                    // 洋红色
	Cyan                       // 青色
	White                      // 白色
	BrightBlack                // 亮黑
	BrightRed                  // 亮红色
	BrightGreen                // 亮绿色
	BrightYellow               // 亮黄色
	BrightBlue                 // 亮蓝色
	BrightMagenta              // 亮洋红色
	BrightCyan                 // 亮青色
	BrightWhite                // 亮白色

	Default Color = math.MaxInt32
)

颜色值定义

func HEX added in v2.1.0

func HEX(hex string) Color

HEX 以 16 进制的形式转换成颜色

可以由以下形式:

HEX("#aaa") ==> RGB(0xaa, 0xaa, 0xaa)
HEX("aaa") ==> RGB(0xaa, 0xaa, 0xaa)
HEX("ababab") ==> RGB(0xab, 0xab, 0xab)

func RGB

func RGB(r, g, b uint8) Color

RGB 根据 RBG 生成真色彩

func (Color) BColor

func (c Color) BColor() ansi.ESC

BColor 转换成前景色的 ansi.ESC

func (Color) FColor

func (c Color) FColor() ansi.ESC

FColor 转换成前景色的 ansi.ESC

func (Color) RGB

func (c Color) RGB() (r, g, b uint8)

RGB 转换成 RGB 三原色

func (Color) String

func (c Color) String() string

type Colorize

type Colorize struct {
	Type       Type
	Foreground Color
	Background Color
	// contains filtered or unexported fields
}

Colorize 指定了颜色的输出工具

默认输出到 os.Stdout,若要指定其它,可以使用 Colorize.Fprintf 系列函数。

func New

func New(t Type, foreground, background Color) Colorize

New 新建一个 Colorize

func (Colorize) Fprint

func (c Colorize) Fprint(w io.Writer, v ...interface{}) (int, error)

Fprint 等同于 fmt.Fprint()

若 w 不指向控制台,则颜色值被忽略。

func (Colorize) Fprintf

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

Fprintf 等同于 fmt.Fprintf()

若 w 不指向控制台,则颜色值被忽略。

func (Colorize) Fprintln

func (c Colorize) Fprintln(w io.Writer, v ...interface{}) (int, error)

Fprintln 等同于 fmt.Fprintln()

若 w 不指向控制台,则颜色值被忽略。

func (Colorize) Print

func (c Colorize) Print(v ...interface{}) (int, error)

Print 等同于 fmt.Print()

func (Colorize) Printf

func (c Colorize) Printf(format string, v ...interface{}) (int, error)

Printf 等同于 fmt.Printf()

func (Colorize) Println

func (c Colorize) Println(v ...interface{}) (int, error)

Println 等同于 fmt.Println()

type Type

type Type int

Type 字符的各类显示方式

const (
	Bold         Type = iota + 1
	Faint             // 弱化
	Italic            // 斜体
	Underline         // 下划线
	Blink             // 闪烁
	RapidBlink        // 快速闪烁
	ReverseVideo      // 反显
	Conceal           // 隐藏
	Delete            // 删除线

	Normal Type = -1 // 正常显示
)

各类字体控制属性

NOTE: 并不是所有的终端都支持这些所有特性。

Jump to

Keyboard shortcuts

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