ansi

package
v3.4.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2025 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package ansi 输出 ansi 控制码

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

ansi 的相关文档,可参考以下内容 ansiANSI转义序列

Index

Constants

View Source
const ResetCode = 0

ResetCode 重置所有状态的 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 {
	// contains filtered or unexported fields
}

Writer ansi 控制码的 io.Writer 实现

a := NewWriter(os.Stdout)

a.Left(5).
    ClearLine(2).
    SGR(32, 33).
    Print("abc")

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

func NewWriter

func NewWriter(w io.Writer) *Writer

NewWriter 声明一个 Writer 结构体

func (*Writer) B256Color

func (w *Writer) B256Color(color uint8) *Writer

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

func (*Writer) BTrueColor

func (w *Writer) BTrueColor(r, g, b uint8) *Writer

BTrueColor 输出 24 色彩背景色

func (*Writer) Color256

func (w *Writer) Color256(f, b uint8) *Writer

Color256 输出 256 色的颜色信息

func (*Writer) Down

func (w *Writer) Down(n int) *Writer

Down 下移 n 行光标

func (*Writer) Erase

func (w *Writer) Erase(n int) *Writer

Erase 清除屏幕

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

func (*Writer) EraseLine

func (w *Writer) EraseLine(n int) *Writer

EraseLine 清除行

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

func (*Writer) Err

func (w *Writer) Err() error

Err 返回写入过程中的错误

以链式的方式调用,中途如果出错,后续不会再写入,但是不会中断链式的调用。 可以通过此值判断写入途中是否存在错误。

func (*Writer) F256Color

func (w *Writer) F256Color(color uint8) *Writer

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

func (*Writer) FTrueColor

func (w *Writer) FTrueColor(r, g, b uint8) *Writer

FTrueColor 输出 24 色彩前景色

func (*Writer) Left

func (w *Writer) Left(n int) *Writer

Left 左移 n 个字符光标

func (*Writer) Move

func (w *Writer) Move(x, y int) *Writer

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

func (*Writer) Print

func (w *Writer) Print(args ...any) *Writer

Print 相当于 fmt.Print

func (*Writer) Printf

func (w *Writer) Printf(format string, args ...any) *Writer

Printf 相当于 fmt.Printf

func (*Writer) Println

func (w *Writer) Println(args ...any) *Writer

Println 相当于 fmt.Println

func (*Writer) RestoreCursor

func (w *Writer) RestoreCursor() *Writer

RestoreCursor 恢复光标位置

func (*Writer) Right

func (w *Writer) Right(n int) *Writer

Right 右移 n 个字符光标

func (*Writer) SGR

func (w *Writer) SGR(sgr ...int) *Writer

SGR 输出 SGR 指令

func (*Writer) SaveCursor

func (w *Writer) SaveCursor() *Writer

SaveCursor 保存光标位置

func (*Writer) TrueColor

func (w *Writer) TrueColor(fr, fg, fb, br, bg, bb uint8) *Writer

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

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

func (*Writer) Up

func (w *Writer) Up(n int) *Writer

Up 上移 n 行光标

func (*Writer) WByte

func (w *Writer) WByte(b byte) *Writer

WByte 写入单个字节内容

func (*Writer) WBytes

func (w *Writer) WBytes(data []byte) *Writer

WBytes 写入字节内容

func (*Writer) WString

func (w *Writer) WString(str string) *Writer

WString 写入字符串

func (*Writer) Write

func (w *Writer) Write(bs []byte) (int, error)

Writer 暴露原始的 io.Writer 接口

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

func (*Writer) WriteESC

func (w *Writer) WriteESC(esc ESC) *Writer

WriteESC 输出字符串

Jump to

Keyboard shortcuts

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