普贤教程 › 包参考
ansi 包
ANSI 颜色/样式输出
| 项 | 值 |
|---|---|
| 包名 | ansi |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | ansi.px |
| 分类 | 文本与中文处理 |
| 文件数 / 行数 | 1 / 207 |
安装与引入
bash
pxpkg init # 首次 pxpkg add ansi@0.2.0 pxpkg install
px
import ansi所属分类教程:文本与中文处理
公开接口
| 函数 | 参数 |
|---|---|
an_color | s, fg |
an_bg | s, bg |
an_style | s, style |
an_reset | |
an_strip | s |
其余 30 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
an_fg_code | name |
an_style_code | name |
an_is_letter | c |
an_combine | s, styles |
an_256 | s, code |
an_256_bg | s, code |
an_rgb | s, r, g, b |
an_rgb_bg | s, r, g, b |
an_red | s |
an_green | s |
an_yellow | s |
an_blue | s |
an_cyan | s |
an_magenta | s |
an_bold | s |
an_dim | s |
an_italic | s |
an_underline | s |
an_cursor_up | n = 1 |
an_cursor_down | n = 1 |
an_cursor_left | n = 1 |
an_cursor_right | n = 1 |
an_cursor_hide | |
an_cursor_show | |
an_move_to | row, col |
an_clear_screen | |
an_clear_line | |
an_save_cursor | |
an_restore_cursor | |
an_width | s |
包内说明(源码头注释)
text
============================================================ 普贤生态库:registry/ansi —— ANSI 颜色/样式输出 ------------------------------------------------------------ 纯函数库(无 IO、无状态),编译(px build)与解释(px run)双模式一致。 an_color(s, fg) → str 前景色(black/red/green/yellow/blue/magenta/cyan/white + bright 前缀 bright_red 等) an_bg(s, bg) → str 背景色(同色名) an_style(s, style)→ str 样式:bold/dim/italic/underline/blink/reverse/strike an_reset() → str "\u{1b}[0m" an_strip(s) → str 去除全部 ANSI 转义序列(SGR 及其它 CSI) 说明: - 转义序列用 \u{1b}[...m 形式(PuXian 不支持 \x / 八进制转义) - an_strip 按 rune 扫描:遇 ESC 后若接 '[' 则跳到 CSI 终结字节(A-Z/a-z), 否则仅跳过 ESC+1(两字节序列子集) - 不探测 tty:是否染色由调用方决定(log/cli 增强用) ============================================================