
V 语言 term 模块实战指南用 ANSI 转义码实现终端着色、光标定位与基础 TUI【免费下载链接】vSimple, fast, safe, compiled language for developing maintainable software. Compiles itself in 1s with zero library dependencies. Supports automatic C V translation. https://vlang.io项目地址: https://gitcode.com/GitHub_Trending/v/vterm是 V 语言标准库中面向终端的基础模块它为构建极简 TUI文本用户界面提供积木式能力给输出文本着色/加样式、查询终端尺寸、精确控制光标位置、读取按键输入。本文以 vlib/term/README.md 为骨架结合 colors.v、control.v、term.v 等源码与 term_test.v 测试用例讲透 term 模块的每个 API 及其底层 ANSI 原理并给出可直接复制运行的完整示例。读完你将能够用十几行 V 代码写出清屏 居中着色输出 光标定位 按键退出的终端程序并理解何时该从term升级到term.ui。term 模块的定位极简 TUI 的积木而非完整框架文档开篇即明确了 term 的边界它专为构建非常简单的 TUI 应用而设计提供的是构建积木building blocks。如果你需要更复杂的应用——涉及终端事件键盘/鼠标、更高的绘制性能、复杂的布局——应当转向term.ui模块源码位于 vlib/term/ui/含完整 Quickstart 与配置说明它内建了事件循环、逐帧回调frame_fn、矩形/文本绘制原语以及frame_rate、hide_cursor、capture_events、mouse_enabled等完整配置项。从源码结构上看term模块的核心能力恰好分为两大块对应 colors.v 与 control.v 两个文件着色输出把任意字符串包裹上 ANSI 颜色/样式转义码位置控制查询与移动光标、查询与清除屏幕区域。快速上手在终端正中央打印一行彩色文字README 提供了一个覆盖模块主要特性的完整示例清屏 → 获取终端尺寸 → 把光标移到屏幕正中央 → 打印绿色 删除线样式的文本 → 把光标移到屏幕底部 → 等待用户按q退出。import term import os fn main() { term.clear() // clears the content in the terminal width, height : term.get_terminal_size() // get the size of the terminal term.set_cursor_position(x: width / 2, y: height / 2) // now we point the cursor to the middle of the terminal println(term.strikethrough(term.bright_green(hello world))) // Print green text term.set_cursor_position(x: 0, y: height) // Sets the position of the cursor to the bottom of the terminal // Keep prompting until the user presses the q key for { if var : os.input_opt(press q to quit: ) { if var ! q { continue } break } println() break } println(Goodbye.) }这个程序虽小却演示了 term 模块的四个关键特性也揭示了两个容易被忽视的要点坐标从 1 开始set_cursor_position的x、y是 1-based 坐标因此把光标定位到width / 2, height / 2就是正中x: 0, y: height则是最左侧 最后一行。样式可以嵌套叠加strikethrough(bright_green(hello world))把亮绿色前景和删除线两个 SGR 属性组合到同一段文本上这是由 colors.v 中format(msg, open, close)的前开后关配对机制天然支持的。关键方法在底层做了什么调用底层实现源码位置term.clear()输出\x1b[2J清屏\x1b[H光标回左上角term_nix.c.vterm.get_terminal_size()通过ioctl(fd, TIOCGWINSZ, winsize)读取窗口行列数term_nix.c.vterm.set_cursor_position(x, y)输出\x1b[y;xHCUP 光标定位序列control.v注意clear()、get_terminal_size()等在非 TTY 环境如管道重定向、TERMdumb下会返回 false 或回退到默认值80×25见 term.v这是编码时需要自行处理的边界情况。颜色与消息 API 详解README 的 API 章节强调了一个极易踩坑的重要事实ok_message、yellow、bold这类着色/样式函数本身并不会打印任何东西——它们只是返回一段嵌入了 ANSI 转义码的新字符串你必须把它交给println或写入 stdout 的类似函数才能真正显示。完整的 API 演示程序import term import os // returns the height and the width of the terminal width, height : term.get_terminal_size() println(terminal dimensions: width: ${width} height: ${height}) mut output : // returns the string as green text output ok_message() text term.ok_message(is green) println(output) // returns the string as red text output fail_message() text term.fail_message(is red) println(output) // returns the string as yellow text output warn_message() text term.warn_message(is yellow) println(output) os.input(hit Enter to clear the console and continue) // clears the entire terminal term.clear() // Set the color output of the text. // The available colors are: // black, white, blue, yellow, // green, red, cyan, magenta, // bright_black, bright_white, bright_blue, bright_yellow, // bright_green, bright_red, bright_cyan, bright_magenta, output yellow() - term.yellow(text) println(output) // transforms the given string into bold text output bold() - term.bold(text) println(output) // puts a strikethrough into the given string output strikethrough() - term.strikethrough(text) println(output) // underlines the given string output underline() - term.underline(text) println(output) // colors the background of the output following the given color // the available colors are: black, blue, yellow, green, cyan, gray output bg_green() - term.bg_green(text) println(output) // sets the position of the cursor term.set_cursor_position(x: 5, y: 10) println(Cursor at (5,10)) // flashes (blinks) the text output term.slow_blink(done) println(output)三种语义化消息函数ok / fail / warnterm.ok_message(s)—— 绿色文本green表示成功term.fail_message(s)—— 红色文本实为failed()即亮白字 红底 加粗bg_red(bold(white(s)))比普通红字更醒目见 term.vterm.warn_message(s)—— 亮黄色文本bright_yellow表示警告。三者都带颜色可用性保护当终端不支持转义序列时例如输出被重定向到文件它们会原样返回传入字符串而不是输出一堆\x1b[...m乱码。判断逻辑集中在supports_escape_sequences()term.v环境变量VCOLORSalways→ 强制启用颜色VCOLORSnever→ 强制禁用TERMdumb→ 禁用否则检查 stdout/stderr 是否为 TTYWindows 上还会额外判断ConEmuANSION或是否启用了虚拟终端处理标志。判定结果会被缓存到__global can_show_color_on_stdout_cache/can_show_color_on_stderr_cache避免每次调用都做系统调用。colorize(cfn, s)与ecolorize(cfn, s)则把这种条件着色封装成了函数式风格term.colorize(term.yellow, the message)。全部前景色/背景色/样式速查结合 colors.v 源码term模块实际上提供了远超 README 列表的完整 SGR 调色板标准前景色black(30)、red(31)、green(32)、yellow(33)、blue(34)、magenta(35)、cyan(36)、white(37)亮色前景bright_black(90)、bright_red(91)、bright_green(92)、bright_yellow(93)、bright_blue(94)、bright_magenta(95)、bright_cyan(96)、bright_white(97)其中gray等价于bright_black背景色bg_black(40) 至bg_white(47) 的八种标准色加上bright_bg_black(100) 至bright_bg_white(107) 八种亮色文本样式bold(1/22)、dim(2/22)、italic(3/23)、underline(4/24)、slow_blink(5/25)、rapid_blink(6/26)、inverse(7/27)、hidden(8/28)、strikethrough(9/29)、reset(0)。括号中的两个数字即format(msg, open, close)的开启码/关闭码配对例如yellow(text)实际返回\x1b[33mtext\x1b[39m。测试 term_test.v 验证了这些嵌套样式可以被term.strip_ansi()干净地剥离回纯文本。24 位真彩色支持rgb(r, g, b, msg)与bg_rgb(r, g, b, msg)使用\x1b[38;2;r;g;bm/\x1b[48;2;r;g;bm扩展序列hex(hex, msg)与bg_hex(hex, msg)则接受 0xRRGGBB 形式的整数并自动拆分为 RGB 分量见 colors.v。注意旧式 16 色终端可能不支持这些扩展序列。为日志/进度条场景设计的辅助函数strip_ansi(text)移除文本中的所有 ANSI 序列处理 CSI、OSC、%三种转义形式常用于日志清洗或计算纯文本宽度h_divider(divider)返回一条铺满当前终端宽度的水平分隔线若传空字符串则用空格撑满一行见 term.vheader(text, divider)/header_left(text, divider)生成居中标题或左对齐标题的装饰线如 TITLE ...标题超长时会按终端宽度自动截断。低级光标控制与屏幕擦除README 还列出了一些直接写 stdout的低级光标辅助函数它们不再返回字符串而是立即输出控制序列import term // moves the cursor up term.cursor_up(1) // moves the cursor down term.cursor_down(1) // moves the cursor to the right term.cursor_forward(2) // moves the cursor to the left term.cursor_back(2) // hides the cursor term.hide_cursor() // shows the cursor term.show_cursor()这些函数只是move(n, direction)的封装最终输出\x1b[nA上、\x1b[nB下、\x1b[nC右、\x1b[nD左四种 CSI 光标移动序列方向由A/B/C/D字母决定control.v。hide_cursor()/show_cursor()则分别输出\x1b[?25l/\x1b[?25h是进度条刷新和全屏 TUI场景的常用组合。term模块还提供了一整套屏幕擦除原语适合实现进度条与局部刷新control.v函数输出序列效果erase_toend()\x1b[0J从光标擦除到窗口末尾erase_tobeg()\x1b[1J从光标擦除到窗口开头erase_clear()\033[H\033[J清屏并把光标移回左上角erase_del_clear()\x1b[3J清屏并同时清空滚动缓冲区erase_line_toend()\x1b[0K从光标擦除到行尾erase_line_tobeg()\x1b[1K从行首擦除到光标erase_line_clear()\x1b[2K擦除整行光标位置不变clear_previous_line()\r\x1b[1A\x1b[2K回到行首、上移一行并擦除用于让下一行输出覆盖上一行最后一个clear_previous_line()是官方注释钦点的进度条利器——它保证下一次println直接覆盖上一行的内容不会产生滚动。交互输入读按键、读光标、改终端标题虽然 README 的主示例用os.input_opt实现按 q 退出但term模块本身尤其非 Windows 平台还内置了更底层的输入能力可直接支撑简单的交互逻辑key_pressed(blocking: true, echo: false)临时关闭 ICANON行缓冲与 ECHO逐字符读取标准输入非阻塞模式下无按键时返回-1。底层通过termios修改终端属性并在结束时恢复原状term_nix.c.v。Windows 实现则基于kbhit()/_getch()函数键方向键等会返回0xE0与真实键码的合成值term_windows.c.vutf8_getchar()从标准输入读取一个完整的 UTF-8 字符rune内部按首字节推断后续字节数并逐字节拼装utf8.vget_cursor_position() !Coord发送 DSR 请求序列\e[6n解析终端回传的ESC [ y ; x R应答term_nix.c.v。测试 term_test.v 验证了 set 后再 get 能取回相同坐标需要注意哑终端dumb上该调用返回Coord{0, 0}set_terminal_title(title)/set_tab_title(title)分别输出\033]0;...\007与\033]30;...\007修改窗口/标签页标题term_nix.c.vWindows 上通过SetConsoleTitle实现且set_tab_title目前等价于设置窗口标题term_windows.c.venable_echo(enable)开启/关闭输入回显ECHO标志Windows 上为空操作由key_pressed的 echo 参数承担supports_sixel()与graphics_num_colors()探测终端对 Sixel 图形协议的支持情况与色彩寄存器数量用于判断能否直接向终端输出 Sixel 位图。Linux 平台通过关闭回显后发送设备查询序列\e[c并解析应答实现term_nix.c.vWindows Console 目前不支持两个函数固定返回false/0term_windows.c.v。平台差异与回退行为term 模块采用平台分文件编译同名公开函数在不同平台有不同实现类 UnixLinux/macOS/BSD 等get_terminal_size()走ioctl TIOCGWINSZclear()、get_cursor_position()等基于 ANSI 序列与 termiosWindowsget_terminal_size()走 Win32 控制台 APIGetConsoleScreenBufferInfo读取窗口矩形clear()通过ScrollConsoleScreenBuffer实现语义与 ANSI 清屏一致JavaScript 后端还存在独立的 term.js.v 实现公共回退stdout 非 TTY 或TERMdumb时get_terminal_size()返回默认值80×25常量default_columns_size/default_rows_sizeterm.v。何时升级到 term.uiREADME 的定位非常清晰term 只适合非常简单的 TUI。当你的应用出现以下任一诉求时就应转向 vlib/term/ui/README.md 中的term.ui需要键盘/鼠标事件如.key_down、.mouse_*而不是裸读按键需要按frame_rate默认 30fps驱动逐帧重绘且对大屏绘制有性能要求需要矩形、文本等绘制原语和完整的初始化/清理生命周期init_fn、frame_fn、cleanup_fn、fail_fn回调需要 raw 模式拦截ctrlc、ctrlzcapture_events或终端鼠标追踪mouse_enabled。term.ui的 Quickstart 只用了约 30 行代码就完成了一个按键退出 绘制矩形与文本的可交互程序而同样的能力若用纯term实现将需要自行维护事件循环与差分绘制——这正是文档建议复杂应用用 term.ui的根本原因。真实范例可参考 examples/term.ui/ 下的pong.v、text_editor.v、vyper.v等程序。小结与验证term模块的完整能力可归纳为三条主线条件化 ANSI 着色含 16/24 位色与全部样式、光标与屏幕区域控制、终端状态查询与底层输入。所有函数要么返回带转义码的字符串着色/样式类要么直接写 stdout控制类理解这一二分法就不会再用错。仓库自带的测试 term_test.v 为上述行为提供了可执行佐证test_get_terminal_size断言终端宽度大于 0test_header系列断言各种标题/分隔线长度与终端宽度一致test_get_cursor_position验证光标坐标可被 set/get 往返一致test_strip_ansi验证多层嵌套样式可被完整剥离test_write_color则检查ColorConfigstyles/fg/bg/custom字段拼装出的 SGR 序列格式如\x1b[1;3;4;31;46m。运行v test vlib/term/即可在本地复现这些验证动手改一改示例里的颜色与坐标是理解 ANSI 终端编程最快的路径。【免费下载链接】vSimple, fast, safe, compiled language for developing maintainable software. Compiles itself in 1s with zero library dependencies. Supports automatic C V translation. https://vlang.io项目地址: https://gitcode.com/GitHub_Trending/v/v创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考