morikuni/aec

GitHub: morikuni/aec

Go 语言的 ANSI 转义码封装库,提供流式的 API 来简化终端光标控制、滚动、擦除以及彩色文本输出的开发。

Stars: 99 | Forks: 3

# aec [![GoDoc](https://godoc.org/github.com/morikuni/aec?status.svg)](https://godoc.org/github.com/morikuni/aec) ANSI escape code 的 Go 封装。 ## 安装 ``` go get github.com/morikuni/aec ``` ## 功能 ANSI escape code 依赖于终端环境。 其中部分功能可能无法正常工作。 请使用 [checkansi](./checkansi) 检查支持的字体样式/字体颜色功能。 有关更多详细信息,请参阅 [Wikipedia](https://en.wikipedia.org/wiki/ANSI_escape_code)。 ### 光标 - `Up(n)` - `Down(n)` - `Right(n)` - `Left(n)` - `NextLine(n)` - `PreviousLine(n)` - `Column(col)` - `Position(row, col)` - `Save` - `Restore` - `Hide` - `Show` - `Report` ### 擦除 - `EraseDisplay(mode)` - `EraseLine(mode)` ### 滚动 - `ScrollUp(n)` - `ScrollDown(n)` ### 字体样式 - `Bold` - `Faint` - `Italic` - `Underline` - `BlinkSlow` - `BlinkRapid` - `Inverse` - `Conceal` - `CrossOut` - `Frame` - `Encircle` - `Overline` ### 字体颜色 前景色。 - `DefaultF` - `BlackF` - `RedF` - `GreenF` - `YellowF` - `BlueF` - `MagentaF` - `CyanF` - `WhiteF` - `LightBlackF` - `LightRedF` - `LightGreenF` - `LightYellowF` - `LightBlueF` - `LightMagentaF` - `LightCyanF` - `LightWhiteF` - `Color3BitF(color)` - `Color8BitF(color)` - `FullColorF(r, g, b)` 背景色。 - `DefaultB` - `BlackB` - `RedB` - `GreenB` - `YellowB` - `BlueB` - `MagentaB` - `CyanB` - `WhiteB` - `LightBlackB` - `LightRedB` - `LightGreenB` - `LightYellowB` - `LightBlueB` - `LightMagentaB` - `LightCyanB` - `LightWhiteB` - `Color3BitB(color)` - `Color8BitB(color)` - `FullColorB(r, g, b)` ### 颜色转换器 24位 RGB 颜色转 ANSI 颜色。 - `NewRGB3Bit(r, g, b)` - `NewRGB8Bit(r, g, b)` ### 构建器 用于组合这些功能。 ``` custom := aec.EmptyBuilder.Right(2).RGB8BitF(128, 255, 64).RedB().ANSI custom.Apply("Hello World") ``` ## 用法 1. 通过 `aec.XXX().With(aec.YYY())` 或 `aec.EmptyBuilder.XXX().YYY().ANSI` 创建 ANSI 2. 通过 `fmt.Print(ansi, "some string", aec.Reset)` 或 `fmt.Print(ansi.Apply("some string"))` 打印 ANSI 使用字体样式或字体颜色功能时,应添加 `aec.Reset`。 ## 示例 简单的进度条。 ![示例](https://static.pigsec.cn/wp-content/uploads/repos/cas/2e/2e7fe9f2bfa478cb18fb0791ff134a1e3b7a89aad52f7c6cb01da85a7c8dd9e1.gif) ``` package main import ( "fmt" "strings" "time" "github.com/morikuni/aec" ) func main() { const n = 20 builder := aec.EmptyBuilder up2 := aec.Up(2) col := aec.Column(n + 2) bar := aec.Color8BitF(aec.NewRGB8Bit(64, 255, 64)) label := builder.LightRedF().Underline().With(col).Right(1).ANSI // for up2 fmt.Println() fmt.Println() for i := 0; i <= n; i++ { fmt.Print(up2) fmt.Println(label.Apply(fmt.Sprint(i, "/", n))) fmt.Print("[") fmt.Print(bar.Apply(strings.Repeat("=", i))) fmt.Println(col.Apply("]")) time.Sleep(100 * time.Millisecond) } } ``` ## 许可证 [MIT](./LICENSE)
标签:ANSI转义码, EVTX分析, Go, Ruby工具, SOC Prime, 开发工具, 日志审计, 终端UI