很多 AHK 脚本一开始都是热键和 GUI,但写到批处理、测试脚本、命令行工具时,控制台输出就会变得越来越重要。普通 FileAppend, *, ... 能输出文本,但一旦信息多起来,成功、警告、错误、表格、进度就很难看清。

RichAHK 是我给 AHK v1 整理的一个终端输出美化库,思路参考 Python 的 Rich,但没有照搬成一个庞然大物。它只做 AHK 里比较容易落地的部分:彩色文本、表格、面板、树形结构、进度条、日志和简单交互选单。

先看一个最小例子

#Requires AutoHotkey v1.1
#Include 

console := new Rich_Console()

console.Print("[bold cyan]RichAHK[/] [dim]for AutoHotkey v1[/]")
console.Log("任务开始", "info", "demo")
console.Log("保存成功", "success", "demo")
console.Log("出现警告", "warning", "demo")

标记写法类似 [bold red]错误[/]。如果终端支持 ANSI,就能看到颜色;如果只是拿来生成纯文本,也可以用 Rich_Plain() 去掉标记。

表格输出比拼字符串舒服

AHK 新手常见写法是自己用空格拼表格。英文还凑合,碰到中文宽度就容易歪。RichAHK 做了 CJK 中文宽度处理,用来展示测试结果、文件列表、批处理统计会顺手很多。

table := new Rich_Table("任务报告")
table.AddColumn("任务")
table.AddColumn("状态", "center")
table.AddColumn("备注", "left", "", 28)
table.AddRow("解析标记", "[green]完成[/]", "支持 bold、颜色、背景色")
table.AddRow("渲染表格", "[green]完成[/]", "长文本可以自动换行")
console.Print(table)

进度和日志

这个库的进度条是快照式渲染,适合展示当前状态。要实时刷新,可以配合回车符循环输出。对于 AHK 这种轻量脚本来说,这样已经够用,没必要把控制台做得太复杂。

group := new Rich_ProgressGroup(24)
group.AddTask("下载", 100, 75, "cyan")
group.AddTask("测试", 100, 40, "yellow")
console.Print(group)

Rich_Print("Working " . Rich_Progress(5, 10, 30))

交互式选单

如果当前环境是可交互控制台,可以用方向键选择菜单项;如果环境不支持交互,库会返回默认项,不会让脚本卡死。

choice := Rich_Select("请选择一个操作", ["执行任务", "查看状态", "退出"], 1)
if (choice.cancelled)
    MsgBox, 已取消
else
    MsgBox, % "选择了:" . choice.text

适合哪些脚本

我建议把它用在 CLI 工具、批量处理脚本、测试脚本、本地自动化运行日志里。比如文件整理、网页自动化、Excel 批处理、接口检测,这类脚本只要输出清楚,后期维护成本会低很多。

它不是完整 Python Rich 移植,不做 Markdown 渲染、Traceback 美化、Live Layout 这些大功能。定位就是让 AHK v1 的控制台输出更清楚、更像一个正经工具。

打包库和示例的下载地址:

demo代码片段展示:

#NoEnv
#SingleInstance Force
#Include <RichAHK>
if 0 >= 1
{
    outputFile = %1%
    Rich_SetOutputTarget(outputFile)
}

console := new Rich_Console()

console.Print("[bold cyan]RichAHK[/] [dim]terminal rendering for AutoHotkey v1[/]")
console.Rule("styles", 72)
console.Print("Plain [bold]bold[/] [underline]underline[/] [red]red[/] [#38bdf8]truecolor[/] [white on blue]white on blue[/]")
console.Print("")

table := new Rich_Table("Build report")
table.AddColumn("Task")
table.AddColumn("State", "center")
table.AddColumn("Time", "right", "cyan")
table.AddRow("Parse markup", "[green]OK[/]", "3 ms")
table.AddRow("Render table", "[green]OK[/]", "8 ms")
table.AddRow("Syntax highlight", "[yellow]计划支持[/]", "-")
console.Print(table)
console.Print("")

wrapped := new Rich_Table("Wrapped table")
wrapped.AddColumn("Feature")
wrapped.AddColumn("Notes", "left", "", 34)
wrapped.AddRow("Console", "Adds [cyan]Print[/], [cyan]Rule[/], [cyan]Panel[/], and [cyan]Log[/] helpers.")
wrapped.AddRow("Tables", "Cells can contain manual`nline breaks or long text that wraps.")
console.Print(wrapped)
console.Print("")

tree := new Rich_Tree("[bold]RichAHK[/]")
core := tree.Add("[cyan]core[/]")
core.Add("markup parser")
core.Add("ANSI styles")
widgets := tree.Add("[magenta]widgets[/]")
widgets.Add("tables")
widgets.Add("panels")
widgets.Add("progress")
console.Print(tree)
console.Print("")

console.Panel("[green]Done[/] This version covers the portable core plus console logging, wrapped tables, and multi-task progress snapshots.", "panel")
console.Print("")

console.Log("Console logging is online", "info", "demo")
console.Log("Warnings get their own color", "warning", "demo")
console.Log("Failures are easy to spot", "error", "demo")
console.Print("")

group := new Rich_ProgressGroup(24)
parseTask := group.AddTask("parse", 100, 100, "green")
renderTask := group.AddTask("render", 100, 65, "cyan")
testTask := group.AddTask("tests", 100, 35, "yellow")
console.Print(group)
console.Print("")

Loop, 20 {
    Rich_Print("Working " . Rich_Spinner(A_Index) . " " . Rich_Progress(A_Index, 20, 32), "`r")
    Sleep, 50
}
Rich_Print("", "`n")
声明:站内资源为整理优化好的代码上传分享与学习研究,如果是开源代码基本都会标明出处,方便大家扩展学习路径。请不要恶意搬运,破坏站长辛苦整理维护的劳动成果。本站为爱好者分享站点,所有内容不作为商业行为。如若本站上传内容侵犯了原著者的合法权益,请联系我们进行删除下架。