|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +根据命令列表生成 asciinema v2 格式录制文件 |
| 4 | +解决 PowerSession-rs 在 Windows 上录制 TUI 程序时管道断裂的问题 |
| 5 | +
|
| 6 | +用法: |
| 7 | + 1. 编辑 commands.txt,每行一条命令 |
| 8 | + 2. 运行 python gen_cast.py |
| 9 | + 3. 输出 demo.cast(asciinema v2 格式) |
| 10 | +""" |
| 11 | + |
| 12 | +import json |
| 13 | +import time |
| 14 | +import os |
| 15 | +import sys |
| 16 | +import subprocess |
| 17 | +import tempfile |
| 18 | + |
| 19 | +COMMANDS_FILE = os.path.join(os.path.dirname(__file__), "commands.txt") |
| 20 | +OUTPUT_CAST = os.path.join(os.path.dirname(__file__), "demo.cast") |
| 21 | +WIDTH = 120 |
| 22 | +HEIGHT = 30 |
| 23 | + |
| 24 | +def run_command(cmd, timeout=30): |
| 25 | + """运行命令,返回 (stdout, stderr, duration)""" |
| 26 | + start = time.time() |
| 27 | + try: |
| 28 | + result = subprocess.run( |
| 29 | + cmd, |
| 30 | + shell=True, |
| 31 | + capture_output=True, |
| 32 | + text=True, |
| 33 | + timeout=timeout, |
| 34 | + encoding="utf-8", |
| 35 | + errors="replace" |
| 36 | + ) |
| 37 | + duration = time.time() - start |
| 38 | + return result.stdout, result.stderr, duration |
| 39 | + except subprocess.TimeoutExpired: |
| 40 | + duration = time.time() - start |
| 41 | + return f"[timeout after {timeout}s]", "", duration |
| 42 | + except Exception as e: |
| 43 | + duration = time.time() - start |
| 44 | + return "", str(e), duration |
| 45 | + |
| 46 | +def main(): |
| 47 | + # 读取命令列表 |
| 48 | + if not os.path.exists(COMMANDS_FILE): |
| 49 | + print(f"未找到 {COMMANDS_FILE}") |
| 50 | + print("请创建该文件,每行一条命令,例如:") |
| 51 | + print(" opencode plugin @herouucn/opencode-commandcode") |
| 52 | + print(" opencode models | findstr commandcode/") |
| 53 | + sys.exit(1) |
| 54 | + |
| 55 | + with open(COMMANDS_FILE, "r", encoding="utf-8") as f: |
| 56 | + commands = [line.strip() for line in f if line.strip() and not line.startswith("#")] |
| 57 | + |
| 58 | + if not commands: |
| 59 | + print(f"{COMMANDS_FILE} 为空") |
| 60 | + sys.exit(1) |
| 61 | + |
| 62 | + print(f"读取到 {len(commands)} 条命令,开始录制...") |
| 63 | + |
| 64 | + # 准备 cast 文件 |
| 65 | + header = { |
| 66 | + "version": 2, |
| 67 | + "width": WIDTH, |
| 68 | + "height": HEIGHT, |
| 69 | + "timestamp": int(time.time()), |
| 70 | + "env": { |
| 71 | + "TERM": "xterm-256color", |
| 72 | + "SHELL": "powershell.exe" if sys.platform == "win32" else os.environ.get("SHELL", "/bin/bash") |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + events = [] |
| 77 | + current_time = 0.0 |
| 78 | + |
| 79 | + # 初始清屏 |
| 80 | + events.append([current_time, "o", "\u001b[2J\u001b[H\u001b[?25h"]) |
| 81 | + current_time += 0.1 |
| 82 | + |
| 83 | + for i, cmd in enumerate(commands): |
| 84 | + # 显示命令提示符 |
| 85 | + prompt = f"PS C:\\> " if sys.platform == "win32" else "$ " |
| 86 | + events.append([current_time, "o", prompt]) |
| 87 | + current_time += 0.1 |
| 88 | + |
| 89 | + # 逐字符输入命令 |
| 90 | + for char in cmd: |
| 91 | + events.append([current_time, "i", char]) |
| 92 | + current_time += 0.02 |
| 93 | + |
| 94 | + # 回车 |
| 95 | + events.append([current_time, "o", "\r\n"]) |
| 96 | + current_time += 0.1 |
| 97 | + |
| 98 | + # 执行命令 |
| 99 | + print(f" [{i+1}/{len(commands)}] {cmd}") |
| 100 | + stdout, stderr, duration = run_command(cmd) |
| 101 | + |
| 102 | + # 输出结果 |
| 103 | + output = stdout + stderr |
| 104 | + if output: |
| 105 | + # 转义特殊字符 |
| 106 | + output = output.replace("\\", "\\\\").replace("\r\n", "\n").replace("\r", "\n") |
| 107 | + events.append([current_time, "o", output]) |
| 108 | + current_time += duration |
| 109 | + else: |
| 110 | + current_time += 0.5 |
| 111 | + |
| 112 | + # 命令间暂停 |
| 113 | + current_time += 0.5 |
| 114 | + |
| 115 | + # 结束提示 |
| 116 | + events.append([current_time, "o", "\r\n=== 演示完成 ===\r\n"]) |
| 117 | + current_time += 0.1 |
| 118 | + |
| 119 | + # 写入 cast 文件 |
| 120 | + with open(OUTPUT_CAST, "w", encoding="utf-8", newline="") as f: |
| 121 | + f.write(json.dumps(header) + "\n") |
| 122 | + for event in events: |
| 123 | + f.write(json.dumps(event) + "\n") |
| 124 | + |
| 125 | + file_size = os.path.getsize(OUTPUT_CAST) |
| 126 | + print(f"\n完成!") |
| 127 | + print(f"文件: {OUTPUT_CAST}") |
| 128 | + print(f"大小: {file_size} bytes") |
| 129 | + print(f"时长: {current_time:.1f} 秒") |
| 130 | + print(f"事件数: {len(events)}") |
| 131 | + print() |
| 132 | + print("播放测试:") |
| 133 | + print(f" asciinema play {OUTPUT_CAST}") |
| 134 | + print("或上传到 asciinema.org:") |
| 135 | + print(f" powersession upload {OUTPUT_CAST}") |
| 136 | + |
| 137 | +if __name__ == "__main__": |
| 138 | + main() |
0 commit comments