ansi

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: 3 Imported by: 0

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 的相关文档,可参考以下内容:

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

Index

Constants

View Source
const ResetCode = 0

ResetCode 重置所有状态的 ansi.ESC 状态码

Variables

This section is empty.

Functions

This section is empty.

Types

type ESC

type ESC string

ESC 表示 ansi 转码序列

func B256Color

func B256Color(color uint8) ESC

B256Color 获取扩展的背景颜色值控制码

func BTrueColor

func BTrueColor(r, g, b uint8) ESC

BTrueColor 返回真色彩的背景颜色值

func CHA

func CHA(n int) ESC

CHA 光标移至 n 列位置

func CNL

func CNL(n int) ESC

CNL 光标下移 n 行并至行首

func CPL

func CPL(n int) ESC

CPL 光标上移 n 行并至行首

func CSI

func CSI(end byte, v ...int) ESC

CSI 生成 CSI 指令

func CUB

func CUB(n int) ESC

CUB 光标后移

func CUD

func CUD(n int) ESC

CUD 光标下移 n

func CUF

func CUF(n int) ESC

CUF 光标前移

func CUP

func CUP(x, y int) ESC

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

func CUU

func CUU(n int) ESC

CUU 光标上移 n

func DSR

func DSR() ESC

DSR 设备状态报告

func ED

func ED(n int) ESC

ED 返回清除屏幕的控制符

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

func EL

func EL(n int) ESC

EL 获取清除行的控制符

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

func F256Color

func F256Color(color uint8) ESC

F256Color 获取扩展的前景颜色值控制码

func FTrueColor

func FTrueColor(r, g, b uint8) ESC

FTrueColor 返回真色彩的前景颜色值

func RCP

func RCP() ESC

RCP 恢复光标位置

func SCP

func SCP() ESC

SCP 保存光标位置

func SD

func SD(n int) ESC

SD 向下滚动 n 行并至行尾

func SGR

func SGR(args ...int) ESC

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

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

func SU

func SU(n int) ESC

SU 向上滚动 n 行并至行尾

type Writer

type Writer struct {
	io.Writer
}

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) B256Color

func (a *Writer) B256Color(color uint8) (int, error)

B256Color 输出 256 色的背景颜色信息

func (*Writer) BTrueColor

func (a *Writer) BTrueColor(r, g, b uint8) (int, error)

BTrueColor 输出 24 色彩背景色

func (*Writer) Color256

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

Color256 输出 256 色的颜色信息

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) F256Color

func (a *Writer) F256Color(color uint8) (int, error)

F256Color 输出 256 色的背景颜色信息

func (*Writer) FTrueColor

func (a *Writer) FTrueColor(r, g, b uint8) (int, error)

FTrueColor 输出 24 色彩前景色

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)

Print 相当于 fmt.Print

func (*Writer) Printf

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

Printf 相当于 fmt.Printf

func (*Writer) Println

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

Println 相当于 fmt.Println

func (*Writer) RestoreCursor

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

RestoreCursor 恢复光标位置

func (*Writer) Right

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

Right 右移 n 个字符光标

func (*Writer) SGR

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

SGR 输出 SGR 指令

func (*Writer) SaveCursor

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

SaveCursor 保存光标位置

func (*Writer) TrueColor

func (a *Writer) TrueColor(fr, fg, fb, br, bg, bb uint8) (int, error)

TrueColor 输出 24 位色彩的颜色信息

fr、fb 和 fb 表示前景色的 RGB 颜色值; br、bb 和 bb 表示背景色的 RGB 颜色值;

func (*Writer) Up

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

Up 上移 n 行光标

func (*Writer) WriteESC

func (a *Writer) WriteESC(esc ESC) (int, error)

WriteESC 输出字符串

Jump to

Keyboard shortcuts

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