ansi

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2018 License: MIT Imports: 3 Imported by: 1

Documentation

Overview

Package ansi 输出 ansi 控制码,windows 系统默认情况下不支持 ansi 控制码。 若仅仅是需要输出彩色字符到控制台,请使用 term/colors 包。

fmt.Printf("%v这是红色的字", term.FRed)
fmt.Printf("%v这是红色字,绿色背景", term.SGR(term.SGRFRed,term.SGRBGreen))
fmt.Printf("%v%v这是红色字,绿色背景", term.FRed,term.BGreen)

// 包装一个stderr。
w := term.NewWriter(os.Stderr)
w.Left(5)
w.SGR(term.SGRFRed)
w.Printf("%s", "string")
w.Move(1,1)

ansi 的相关文档,可参考以下内容:

http://en.wikipedia.org/wiki/ANSI_escape_code
http://www.mudpedia.org/mediawiki/index.php/ANSI_colors

Index

Constants

View Source
const (
	Reset           = "\033[0m"
	Bold            = "\033[1m"
	Underline       = "\033[4m"
	Blink           = "\033[5m" // 闪烁
	ReverseVideo    = "\033[7m" // 反显
	Conceal         = "\033[8m"
	BoldOff         = "\033[22m"
	UnderlineOff    = "\033[24m"
	BlinkOff        = "\033[25m"
	ReverseVideoOff = "\033[27m"
	ConcealOff      = "\033[28m"

	FBlack   = "\033[30m"
	FRed     = "\033[31m"
	FGreen   = "\033[32m"
	FYellow  = "\033[33m"
	FBlue    = "\033[34m"
	FMagenta = "\033[35m"
	FCyan    = "\033[36m"
	FWhite   = "\033[37m"
	FDefault = "\033[39m" // 默认前景色

	BBlack   = "\033[40m"
	BRed     = "\033[41m"
	BGreen   = "\033[42m"
	BYellow  = "\033[43m"
	BBlue    = "\033[44m"
	BMagenta = "\033[45m"
	BCyan    = "\033[46m"
	BWhite   = "\033[47m"
	BDefault = "\033[49m" // 默认背景色

	SaveCursor    = "\033[s"    // 保存光标位置
	RestoreCursor = "\033[u"    // 恢复光标位置
	HideCursor    = "\033[?25l" // 隐藏光标
	ShowCursor    = "\033[?25h" // 显示光标
)

ANSI 码的定义

View Source
const (
	SGRReset           = "0"
	SGRBold            = "1"
	SGRUnderline       = "4"
	SGRBlink           = "5" // 闪烁
	SGRReverseVideo    = "7" // 反显
	SGRConceal         = "8"
	SGRBoldOff         = "22"
	SGRUnderlineOff    = "24"
	SGRBlinkOff        = "25"
	SGRReverseVideoOff = "27"
	SGRConcealOff      = "28"

	SGRFBlack   = "30"
	SGRFRed     = "31"
	SGRFGreen   = "32"
	SGRFYellow  = "33"
	SGRFBlue    = "34"
	SGRFMagenta = "35"
	SGRFCyan    = "36"
	SGRFWhite   = "37"
	SGRFDefault = "39" // 默认前景色

	SGRBBlack   = "40"
	SGRBRed     = "41"
	SGRBGreen   = "42"
	SGRBYellow  = "43"
	SGRBBlue    = "44"
	SGRBMagenta = "45"
	SGRBCyan    = "46"
	SGRBWhite   = "47"
	SGRBDefault = "49" // 默认背景色
)

SGR 控制码

Variables

This section is empty.

Functions

func BColor256

func BColor256(color int) string

BColor256 获取扩展的背景颜色值控制码,当 color 的值超出[0,255]时,将触发 panic

func Down

func Down(n int) string

Down 返回下移N行的控制符

func Erase

func Erase(n int) string

Erase 返回清除屏幕的控制符。

n == 0 时,清除从当前光标到屏幕尾的所有字符; n == 1 时,清除从当前光标到屏幕头的所有字符; n == 2 时,清除当前屏幕的所有字符; 当 n 为其它值时,将触发 panic

func EraseLine

func EraseLine(n int) string

EraseLine 获取清除行的控制符。

n == 0 时,清除从当前光标到行尾的所有字符; n == 1 时,清除从当前光标到行头的所有字符; n == 2 时,清除当前行的所有字符。 当 n 为其它值时,将触发 panic

func FColor256

func FColor256(color int) string

FColor256 获取扩展的文本颜色值控制码,当 color 的值超出[0,255]时,将触发 panic

func Left

func Left(n int) string

Left 返回左移 N 个字符的控制符

func Move

func Move(x, y int) string

Move 移动光标到 x,y 的位置

func Right(n int) string

Right 返回右移 N 个字符的控制符

func SGR

func SGR(args ...string) string

SGR 将几个 SGR 控制符合成一个ansi控制符

"30", "31", "32"
// 以上参数将会被转换成以下内容返回
"\033[30;31;32m"

func Up

func Up(n int) string

Up 返回上移N行的控制符

Types

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer ansi 控制码的 io.Writer 接口.

a := NewWriter(os.Stdout)

a.Left(5)
a.ClearLine(2)
a.SGR(term.SGRFRed,term.SGRBGreen)
a.Print("abc")

fmt.Fprintf(a, "%v", term.SGRFBBlue)

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter 声明一个 Writer 结构体

func (*Writer) BColor256

func (a *Writer) BColor256(color int) (int, error)

func (*Writer) Color256

func (a *Writer) Color256(f, b int) (int, error)

func (*Writer) Down

func (a *Writer) Down(n int) (int, error)

Down 下移 n 行光标

func (*Writer) Erase

func (a *Writer) Erase(n int) (int, error)

Erase 清除屏幕。

n==0 时,清除从当前光标到屏幕尾的所有字符; n==1 时,清除从当前光标到屏幕头的所有字符; n==2 时,清除当前屏幕的所有字符; 当 n 为其它值时,将触发 panic

func (*Writer) EraseLine

func (a *Writer) EraseLine(n int) (int, error)

EraseLine 清除行。

n==0 时,清除从当前光标到行尾的所有字符; n==1 时,清除从当前光标到行头的所有字符; n==2 时,清除当前行的所有字符; 当 n 为其它值时,将触发 panic

func (*Writer) FColor256

func (a *Writer) FColor256(color int) (int, error)

func (*Writer) HideCursor

func (a *Writer) HideCursor() (int, error)

func (*Writer) Left

func (a *Writer) Left(n int) (int, error)

Left 左移 n 个字符光标

func (*Writer) Move

func (a *Writer) Move(x, y int) (int, error)

Move 移动光标到 x,y 的位置

func (*Writer) Print

func (a *Writer) Print(args ...interface{}) (int, error)

func (*Writer) Printf

func (a *Writer) Printf(format string, args ...interface{}) (int, error)

func (*Writer) Println

func (a *Writer) Println(args ...interface{}) (int, error)

func (*Writer) RestoreCursor

func (a *Writer) RestoreCursor() (int, error)

func (*Writer) Right

func (a *Writer) Right(n int) (int, error)

Right 右移 n 个字符光标

func (*Writer) SGR

func (a *Writer) SGR(sgr ...string) (int, error)

func (*Writer) SaveCursor

func (a *Writer) SaveCursor() (int, error)

func (*Writer) ShowCursor

func (a *Writer) ShowCursor() (int, error)

func (*Writer) Up

func (a *Writer) Up(n int) (int, error)

Up 上移 n 行光标

func (*Writer) Write

func (a *Writer) Write(b []byte) (int, error)

func (*Writer) WriteString

func (a *Writer) WriteString(str string) (int, error)

WriteString 输出字符串

Jump to

Keyboard shortcuts

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