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 ¶
- func Fprint(w io.Writer, t Type, foreground, background Color, v ...interface{}) (int, error)
- func Fprintf(w io.Writer, t Type, foreground, background Color, format string, ...) (int, error)
- func Fprintln(w io.Writer, t Type, foreground, background Color, v ...interface{}) (int, error)
- func Print(t Type, foreground, background Color, v ...interface{}) (int, error)
- func Printf(t Type, foreground, background Color, format string, v ...interface{}) (int, error)
- func Println(t Type, foreground, background Color, v ...interface{}) (int, error)
- type Color
- type Colorize
- func (c Colorize) Fprint(w io.Writer, v ...interface{}) (int, error)
- func (c Colorize) Fprintf(w io.Writer, format string, v ...interface{}) (int, error)
- func (c Colorize) Fprintln(w io.Writer, v ...interface{}) (int, error)
- func (c Colorize) Print(v ...interface{}) (int, error)
- func (c Colorize) Printf(format string, v ...interface{}) (int, error)
- func (c Colorize) Println(v ...interface{}) (int, error)
- type Type
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Fprint ¶
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 ¶
Fprintln 带色彩输出的 fmt.Fprintln
颜色值只在 w 不为 os.Stderr、os.Stdin、os.Stdout 中的一个时才启作用,否则只向 w 输出普通字符串。
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 )
颜色值定义
type Colorize ¶
type Colorize struct { Type Type Foreground Color Background Color // contains filtered or unexported fields }
Colorize 指定了颜色的输出工具
默认输出到 os.Stdout,若要指定其它,可以使用 Colorize.Fprintf 系列函数。