colors

package
v3.2.9 Latest Latest
Warning

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

Go to latest
Published: May 10, 2024 License: MIT Imports: 6 Imported by: 10

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().
    Color(colors.Normal, colors.Red, colors.Yellow).
    Print("foreground:%v;background:%v").
    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 ...any) (int, error)

Fprint 带色彩输出的 fmt.Fprint

func Fprintf

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

Fprintf 带色彩输出的 fmt.Fprintf

func Fprintln

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

Fprintln 带色彩输出的 fmt.Fprintln

func Print

func Print(t Type, foreground, background Color, v ...any) (int, error)

Print 带色彩输出的 fmt.Print

func Printf

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

Printf 带色彩输出的 fmt.Printf

func Println

func Println(t Type, foreground, background Color, v ...any) (int, error)

Println 带色彩输出的 fmt.Println

func Sprint added in v3.1.0

func Sprint(t Type, foreground, background Color, v ...any) string

func Sprintf added in v3.1.0

func Sprintf(t Type, foreground, background Color, format string, v ...any) string

func Sprintln added in v3.1.0

func Sprintln(t Type, foreground, background Color, v ...any) string

Types

type Color

type Color int32

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

颜色定义分为以下几种:

  • 默认色: math.MaxInt32 定义为 Default
  • 基本色: 0-7 定义从 Black 至 White
  • 增强色: 8-15 定义从 BrightBlack 至 BrightWhite
  • 256 色: 0-256,数值,其中 0-15 的数据会被转换成以上的色彩;
  • 真彩色: 负数,可由 RGB 函数生成;

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

关于颜色的具体定义,可参考以下文章 ANSI_escape_code

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

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

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 根据 RGB 生成真色彩

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 {
	// contains filtered or unexported fields
}

func New

func New(w io.Writer) *Colorize

func (*Colorize) Color

func (c *Colorize) Color(t Type, foreground, background Color) *Colorize

Color 切换输出颜色

func (*Colorize) Err

func (c *Colorize) Err() error

func (*Colorize) Print

func (c *Colorize) Print(v ...any) *Colorize

func (*Colorize) Printf

func (c *Colorize) Printf(format string, v ...any) *Colorize

func (*Colorize) Println

func (c *Colorize) Println(v ...any) *Colorize

func (*Colorize) Reset

func (c *Colorize) Reset() *Colorize

Reset 重置为默认色

func (*Colorize) WByte

func (c *Colorize) WByte(b byte) *Colorize

WByte 写入单个字节内容

func (*Colorize) WBytes

func (c *Colorize) WBytes(data []byte) *Colorize

WBytes 写入字节内容

func (*Colorize) WString

func (c *Colorize) WString(str string) *Colorize

WString 写入字符串

func (*Colorize) Write

func (c *Colorize) Write(bs []byte) (int, error)

Writer 暴露原始的 io.Writer 接口

此接口的出错误信息会直接返回,并不会记录在 Colorize.Err 之中。

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