Skip to content

Commit a23417e

Browse files
authored
Merge pull request #1 from unclejimao/performance-optimize
Performance optimize
2 parents bec2f2f + 08e12b6 commit a23417e

7 files changed

Lines changed: 943 additions & 196 deletions

File tree

README.md

Lines changed: 152 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# WSL Image Clipboard Helper for ClaudeCode / Codex
1+
# WSL Image Clipboard Helper
22

33
Language: [中文说明](#中文说明) | [English Guide](#english-guide)
44

@@ -8,63 +8,182 @@ Language: [中文说明](#中文说明) | [English Guide](#english-guide)
88

99
### 概述
1010

11-
该工具用于在 Windows 中配合 WSL 环境下的 Claude Code、CodeX 等 AI 工具,快速保存剪贴板图片并将其转换为 WSL 路径,方便粘贴给 AI 读取。
11+
#### 背景
12+
当前许多智能编程 CLI Agent(如 Codex 、Amazon Q Developer CLI 等)主要针对 Linux 和 macOS 系统优化,Windows 用户想要体验这些工具,通常需要通过 WSL2(Windows Subsystem for Linux 2)来运行。然而,WSL2 在某些功能上的支持并不完善,**图片粘贴**就是其中一个典型痛点:
13+
14+
- **问题**:WSL2 终端无法直接访问 Windows 剪贴板中的图片数据
15+
- **影响**:用户无法像在原生 Linux/macOS 中那样,直接将截图粘贴给 AI 工具进行分析
16+
- **现状**:一些 AI CLI 工具(如 Amazon Q Developer CLI)通过"保存图片到文件 → 传递文件路径"的方式来变相实现图片输入
17+
18+
#### 解决方案
19+
本工具正是为了弥补这一缺陷而设计:通过 `Alt+V` 快捷键,自动将 Windows 剪贴板中的图片保存到本地,并将对应的 WSL 路径(`/mnt/c/...`)粘贴到当前窗口,让 AI 工具能够无缝读取图片。
20+
21+
**v2.0 版本重点优化**:在保留 `Alt+V` 快捷键工作流的基础上,引入输入法保护、异步保存、自动清理等能力。与旧版需要等待 SHA256 去重与图片落盘相比,现在路径不到 1 秒即可粘贴完成,且不会再出现字符逐个跳出的视觉延迟。
22+
23+
### 核心特性
24+
- **即时路径输出**`Alt+V` 触发后立即粘贴 `/mnt/...` 路径,无需等待图片写入完成,整体响应时间从约 3 秒缩短到 1 秒以内。
25+
- **输入法智能保护**:粘贴前自动切换至英文输入法,完成后恢复原状态,避免中文输入法导致路径错乱。
26+
- **后台异步保存**:借助 PowerShell 脚本在后台保存图片,确保操作无感延迟,并对错误静默处理。
27+
- **自动清理机制**:定时清理超过 2 小时的临时图片,退出时自动回收缓存与子进程。
28+
- **托盘管理增强**:托盘菜单支持一键打开缓存目录、退出程序,便于日常维护。
1229

1330
### 必备环境
14-
- Windows 10/11,已启用 WSL
15-
- PowerShell 允许执行本地脚本(建议执行 `Set-ExecutionPolicy RemoteSigned -Scope CurrentUser`
16-
- (可选)如需重新编译或直接运行 `.ahk` 脚本,请安装 [AutoHotkey v2 官方版](https://www.autohotkey.com/download/ahk-v2.exe)
31+
- Windows 10/11,已启用 WSL2
32+
- PowerShell 5.1 及以上,允许执行本地脚本(建议运行 `Set-ExecutionPolicy RemoteSigned -Scope CurrentUser`
33+
- AutoHotkey v2(已编译为 `wsl_clipboard.exe`;仅在需要重新编译或调试脚本时安装)
1734

1835
### 使用方式
19-
1. 通过以下命令克隆仓库
36+
1. 克隆仓库并进入目录
2037
```bash
2138
git clone https://github.com/cpulxb/WSL-Image-Clipboard-Helper.git
39+
cd WSL-Image-Clipboard-Helper
2240
```
23-
2. 打开仓库目录,进入 `scripts` 文件夹,确认 `wsl_clipboard.exe``.ps1` 辅助脚本保持在一起。
24-
3. 双击 `wsl_clipboard.exe`,程序会最小化到系统托盘常驻后台。
25-
4. 在任意输入框按下 `Alt+V`,剪贴板中的图片会保存到 `temp` 目录,并将对应的 `/mnt/...` 路径粘贴到当前窗口。
26-
5. 需要退出时,在托盘图标上点击并选择 `Exit`,程序会自动调用 `exit-all.ps1` 清理子进程与缓存后退出。
41+
2. 保证 `scripts` 目录下的 `wsl_clipboard.exe` 与相关 `.ps1` 脚本位于同一文件夹。
42+
3. 双击 `scripts/wsl_clipboard.exe`,程序会最小化至系统托盘。
43+
4. 在任意文本输入框按下 `Alt+V`
44+
- 剪贴板图片保存至 `temp/` 目录(后台进行)
45+
- `/mnt/...` 路径立即粘贴至当前窗口
46+
5. 退出时,从托盘图标右键菜单选择 `Exit`,程序会调用 `exit-all.ps1` 清理缓存与子进程。
2747

2848
### 常见注意事项
29-
- `Alt+V` 是全局热键,可能与其他软件的快捷键冲突。若冲突,请修改 `scripts\wsl_clipboard.ahk` 中的热键并重新编译。
30-
- 如果托盘图标没有及时出现,可在任务栏的隐藏图标列表中查看。
31-
- 支持手动运行 `scripts\exit-all.ps1` 以清理所有相关进程和缓存文件。
49+
- `Alt+V` 为全局快捷键,如与其他软件冲突,可编辑 `scripts/wsl_clipboard.ahk` 并重新编译。
50+
- 若托盘图标未显示,请检查任务栏的隐藏图标区域。
51+
- 所有 PowerShell 脚本推荐使用 UTF-8 with BOM 保存,以避免中文内容导致解析失败。
52+
- 可随时运行 `scripts/exit-all.ps1` 手动清理缓存与相关进程。
3253

3354
### 重新编译(可选)
34-
如需自定义热键或分发新的 `.exe`,需先安装 AutoHotkey v2,然后使用自带的 Ahk2Exe:
35-
1. 打开 `C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe`
36-
2. 选择 `scripts\wsl_clipboard.ahk` 作为源文件
37-
3. 指定合适的 Base(二进制)和输出路径,点击 `Convert`
55+
56+
如需自定义热键、修改临时目录路径或分发新的 `.exe`,需先安装 AutoHotkey v2,然后使用自带的 Ahk2Exe 编译器:
57+
58+
1. **安装 AutoHotkey v2**
59+
- 下载并安装 [AutoHotkey v2 官方版](https://www.autohotkey.com/download/ahk-v2.exe)
60+
61+
2. **修改脚本(可选)**
62+
- **修改热键**:编辑 `scripts/wsl_clipboard.ahk` 第 18 行,将 `!v::` 改为其他组合键
63+
- `!v` = Alt+V
64+
- `^!v` = Ctrl+Alt+V
65+
- `^+v` = Ctrl+Shift+V
66+
- **修改临时目录**:编辑第 5 行 `gTempDir` 变量的路径
67+
- **修改清理间隔**:编辑第 125 行的时间参数(默认 2 小时 = 7200000 毫秒)
68+
69+
3. **编译为可执行文件**
70+
- 打开 `C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe`
71+
- **Source (script file)**:选择 `scripts\wsl_clipboard.ahk`
72+
- **Destination (.exe file)**:指定输出路径(如 `scripts\wsl_clipboard.exe`
73+
- **Base File (.bin, .exe)**:选择合适的 Base(推荐 `AutoHotkey64.exe`
74+
- 点击 `Convert` 开始编译
75+
76+
4. **测试新版本**
77+
- 先从托盘退出旧版本
78+
- 双击新编译的 `wsl_clipboard.exe` 测试
79+
80+
### 附加文档
81+
- [技术架构与流程说明](docs/architecture_by_codex.md)
82+
83+
### 版本历史
84+
85+
#### v2.0 (当前版本)
86+
-**路径优先异步保存**:先粘贴路径,后台保存图片,响应时间从 ~3 秒降至 <1 秒
87+
- 🔤 **输入法智能保护**:自动切换英文输入法,避免中文输入法干扰路径
88+
- 🧹 **自动清理机制**:每 2 小时清理超过 2 小时的临时图片
89+
- 🚀 **代码精简**:PowerShell 脚本从 86 行减少到 28 行(-67%)
90+
- 🐛 **编码修复**:exit-all.ps1 改用 UTF-8 with BOM,支持 emoji 和中文字符
91+
-**移除缓存文件**:删除 last_output.txt、last_seq.txt、last_hash.txt
92+
93+
#### v1.0
94+
- 基础剪贴板图片同步功能
95+
- SHA256 去重机制
96+
- 缓存文件管理
3897

3998
---
4099

41100
## English Guide
42101

43102
### Overview
44-
This helper targets Windows users running AI tools (Claude Code, Codex, etc.) inside WSL. Pressing `Alt+V` saves the clipboard image and pastes the corresponding WSL-friendly file path into the active window.
103+
104+
#### Background
105+
Many modern AI-powered CLI agents (such as Codex, Amazon Q Developer CLI, etc.) are primarily optimized for Linux and macOS systems. Windows users who want to experience these tools typically need to run them through WSL2 (Windows Subsystem for Linux 2). However, WSL2 has incomplete support for certain features, with **image pasting** being a notable pain point:
106+
107+
- **Problem**: WSL2 terminals cannot directly access image data from the Windows clipboard
108+
- **Impact**: Users cannot paste screenshots directly to AI tools for analysis, unlike on native Linux/macOS
109+
- **Workaround**: Some AI CLI tools (like Amazon Q Developer CLI) work around this by using a "save image to file → pass file path" approach
110+
111+
#### Solution
112+
This tool is designed to bridge this gap: pressing `Alt+V` automatically saves the Windows clipboard image to a local file and pastes the corresponding WSL path (`/mnt/c/...`) into the active window, enabling AI tools to seamlessly read the image.
113+
114+
**v2.0 Enhancements**: While keeping the familiar `Alt+V` workflow, this version adds input method protection, asynchronous saves, automatic cleanup, and improved tray controls so that WSL-ready paths appear instantly in your terminal. By skipping upfront SHA256 deduplication and deferring disk I/O, the new flow pastes the path in under a second without the character-by-character delay that previously took roughly three seconds.
115+
116+
### Highlights
117+
- **Instant Path Output**: Paste the `/mnt/...` path immediately after `Alt+V`, trimming end-to-end latency from ~3 seconds to under 1 second and avoiding the prior character-by-character send effect.
118+
- **Input Method Safeguard**: Temporarily switch to the English keyboard layout to avoid IME mis-typing, then restore the prior layout.
119+
- **Asynchronous Save Pipeline**: Offload image persistence to PowerShell in the background with silent error handling, keeping the hotkey responsive.
120+
- **Automatic Cleanup**: Periodically prune cached images older than two hours and remove leftovers when exiting from the tray.
121+
- **Enhanced Tray Menu**: Quickly open the cache directory or exit the helper directly from the system tray.
45122

46123
### Requirements
47-
- Windows 10/11 with WSL enabled
48-
- PowerShell allowed to run local scripts (run `Set-ExecutionPolicy RemoteSigned -Scope CurrentUser` if needed)
49-
- (Optional) Install the official [AutoHotkey v2](https://www.autohotkey.com/download/ahk-v2.exe) if you plan to edit the source `.ahk` or rebuild the executable
124+
- Windows 10/11 with WSL2 enabled
125+
- PowerShell 5.1+ with local script execution allowed (`Set-ExecutionPolicy RemoteSigned -Scope CurrentUser`)
126+
- AutoHotkey v2 (already compiled into `wsl_clipboard.exe`; install only if you need to rebuild or debug)
50127

51128
### Usage
52-
1. Clone the repository locally:
129+
1. Clone the repository and move into the project folder:
53130
```bash
54131
git clone https://github.com/cpulxb/WSL-Image-Clipboard-Helper.git
132+
cd WSL-Image-Clipboard-Helper
55133
```
56-
2. Open the repo folder, navigate into `scripts`, and keep `wsl_clipboard.exe` beside the accompanying `.ps1` helpers.
57-
3. Double-click `wsl_clipboard.exe`; it minimizes to the system tray and runs in the background.
58-
4. Press `Alt+V` in any text field. The clipboard image is saved to `temp`, and the corresponding `/mnt/...` path is pasted into the active window.
59-
5. To exit, open the tray icon menu and choose `Exit`; this triggers `exit-all.ps1` to clean up processes and caches.
134+
2. Keep `wsl_clipboard.exe` and its companion `.ps1` scripts together inside the `scripts` directory.
135+
3. Double-click `scripts/wsl_clipboard.exe`; the helper minimizes to the system tray.
136+
4. Press `Alt+V` in any editable field:
137+
- The clipboard image is stored in `temp/` asynchronously.
138+
- The `/mnt/...` path is pasted right away into the active window.
139+
5. Use the tray icon menu → `Exit` to shut down gracefully; this triggers `exit-all.ps1` to clean processes and cached files.
60140

61141
### Notes
62-
- `Alt+V` is a global hotkey and may conflict with other applications. If so, adjust the hotkey in `scripts\wsl_clipboard.ahk` and rebuild.
63-
- If the tray icon is hidden, check the taskbar overflow area.
64-
- You can run `scripts\exit-all.ps1` manually to reset caches and terminate helper processes.
142+
- `Alt+V` is a global hotkey; adjust it inside `scripts/wsl_clipboard.ahk` and rebuild if you encounter conflicts.
143+
- If the tray icon is hidden, look in the taskbar overflow section.
144+
- Save PowerShell scripts as UTF-8 with BOM when they contain non-ASCII characters to avoid parsing issues.
145+
- You can run `scripts/exit-all.ps1` manually for a quick cleanup at any time.
65146

66147
### Rebuild (Optional)
67-
If you want to customize the hotkey or rebuild the helper, install AutoHotkey v2 and use Ahk2Exe:
68-
1. Launch `C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe`
69-
2. Select `scripts\wsl_clipboard.ahk` as the source
70-
3. Pick the desired base binary and output path, then click `Convert`
148+
149+
If you want to customize the hotkey, modify the temp directory path, or distribute a new `.exe`, install AutoHotkey v2 first and use the bundled Ahk2Exe compiler:
150+
151+
1. **Install AutoHotkey v2**
152+
- Download and install the official [AutoHotkey v2](https://www.autohotkey.com/download/ahk-v2.exe)
153+
154+
2. **Modify the Script (Optional)**
155+
- **Change hotkey**: Edit `scripts/wsl_clipboard.ahk` line 18, change `!v::` to another key combination
156+
- `!v` = Alt+V
157+
- `^!v` = Ctrl+Alt+V
158+
- `^+v` = Ctrl+Shift+V
159+
- **Change temp directory**: Edit line 5, modify the `gTempDir` variable path
160+
- **Change cleanup interval**: Edit line 125, adjust the time parameter (default 2 hours = 7200000 milliseconds)
161+
162+
3. **Compile to Executable**
163+
- Launch `C:\Program Files\AutoHotkey\Compiler\Ahk2Exe.exe`
164+
- **Source (script file)**: Select `scripts\wsl_clipboard.ahk`
165+
- **Destination (.exe file)**: Specify output path (e.g., `scripts\wsl_clipboard.exe`)
166+
- **Base File (.bin, .exe)**: Choose appropriate base (recommended `AutoHotkey64.exe`)
167+
- Click `Convert` to start compilation
168+
169+
4. **Test the New Version**
170+
- Exit the old version from the tray first
171+
- Double-click the newly compiled `wsl_clipboard.exe` to test
172+
173+
### Additional Resources
174+
- [Architecture & Workflow Details](docs/architecture_by_codex.md)
175+
176+
### Changelog
177+
178+
#### v2.0 (Current)
179+
-**Path-first async save**: Paste path immediately, save image in background, latency reduced from ~3s to <1s
180+
- 🔤 **IME protection**: Auto-switch to English input during paste, restore after
181+
- 🧹 **Auto cleanup**: Remove images older than 2 hours every 2 hours
182+
- 🚀 **Code simplification**: PowerShell scripts reduced from 86 to 28 lines (-67%)
183+
- 🐛 **Encoding fix**: exit-all.ps1 now uses UTF-8 with BOM for emoji and Chinese characters
184+
-**Cache removal**: Deleted last_output.txt, last_seq.txt, last_hash.txt
185+
186+
#### v1.0
187+
- Basic clipboard image sync functionality
188+
- SHA256 deduplication mechanism
189+
- Cache file management

0 commit comments

Comments
 (0)