"""Offline VS Code Integrated Terminal commands, links & multi-shell verifier for Ch11."""
import json
import re
import tempfile
from pathlib import Path
VERSION = "CPython 3.12.14 offline VS Code Integrated Terminal verifier"
def check(name, summary, expected, actual):
return {
"name": name,
"summary": summary,
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = []
# Case 1: Terminal shortcuts matrix + file creation (greetings.txt) + Command.txt line search
shortcuts = {
"显示集成终端": ("Ctrl + `", "Ctrl + `"),
"新建终端": ("Ctrl+Shift+``", "Cmd+Shift+``"),
"切换终端": ("Ctrl+PageUp/PageDown", "Cmd+PageUp/PageDown"),
"关闭终端": ("Ctrl+Shift+W", "Cmd+Shift+W"),
}
with tempfile.TemporaryDirectory() as tmp:
ws = Path(tmp)
(ws / "greetings.txt").write_text("Hello, VS Code\n", encoding="utf-8")
(ws / "Command.txt").write_text("ls\ndir\nmkdir\necho\ngrep\nrmdir\n", encoding="utf-8")
matches = []
for idx, line in enumerate((ws / "Command.txt").read_text(encoding="utf-8").splitlines(), 1):
if "dir" in line:
matches.append(f"Command.txt:{idx}:{line}")
cases.append(
check(
"terminal_shortcuts_and_shell_commands",
"Verify 4 terminal shortcuts, greetings.txt creation, and line-numbered Command.txt search",
{
"shortcut_count": 4,
"greetings_content": "Hello, VS Code",
"dir_search_matches": ["Command.txt:2:dir", "Command.txt:3:mkdir", "Command.txt:6:rmdir"],
},
{
"shortcut_count": len(shortcuts),
"greetings_content": (ws / "greetings.txt").read_text(encoding="utf-8").strip(),
"dir_search_matches": matches,
},
)
)
# Case 2: Clickable terminal output link detection, command history & multi-shell profiles
output_lines = [
"Command.txt:2:dir",
"Docs: https://code.visualstudio.com/docs/terminal/basics",
]
file_links = [m.groups() for line in output_lines for m in re.finditer(r"([\w.-]+\.txt):(\d+)", line)]
urls = [m.group(0) for line in output_lines for m in re.finditer(r"https://[^\s]+", line)]
history = ["ls", 'echo "Hello, VS Code" > greetings.txt', 'grep -n "dir" *.txt']
prev_cmd = history[-2]
terminals = [
{"id": 1, "profile": "PowerShell", "location": "panel"},
{"id": 2, "profile": "Bash", "location": "editor"},
]
terminals = [t for t in terminals if t["id"] != 1]
cases.append(
check(
"terminal_output_link_and_history",
"Verify terminal file:line and URL link extraction, history navigation, and multi-shell lifecycle",
{
"parsed_file_link": ["Command.txt", "2"],
"parsed_url_count": 1,
"history_two_steps_up": 'echo "Hello, VS Code" > greetings.txt',
"remaining_terminal_profile": "Bash",
"remaining_terminal_location": "editor",
},
{
"parsed_file_link": list(file_links[0]),
"parsed_url_count": len(urls),
"history_two_steps_up": prev_cmd,
"remaining_terminal_profile": terminals[0]["profile"],
"remaining_terminal_location": terminals[0]["location"],
},
)
)
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/vscode-terminal-lab/explore-terminal.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-terminal.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联25章) · 含当前章节或实例 · mbskycom202612a2203.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2222.zip