Skip to content

Commit e1f793b

Browse files
citlalindalinda
andauthored
fix: character encoding corruption when executing the /copy command on Windows. (#1069)
Co-authored-by: linda <hxn@163.com>
1 parent 3c64f7b commit e1f793b

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

packages/cli/src/ui/utils/commandUtils.test.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
isSlashCommand,
1414
copyToClipboard,
1515
getUrlOpenCommand,
16+
CodePage,
1617
} from './commandUtils.js';
1718

1819
// Mock child_process
@@ -188,7 +189,10 @@ describe('commandUtils', () => {
188189

189190
await copyToClipboard(testText);
190191

191-
expect(mockSpawn).toHaveBeenCalledWith('clip', []);
192+
expect(mockSpawn).toHaveBeenCalledWith('cmd', [
193+
'/c',
194+
`chcp ${CodePage.UTF8} >nul && clip`,
195+
]);
192196
expect(mockChild.stdin.write).toHaveBeenCalledWith(testText);
193197
expect(mockChild.stdin.end).toHaveBeenCalled();
194198
});

packages/cli/src/ui/utils/commandUtils.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,23 @@
77
import type { SpawnOptions } from 'node:child_process';
88
import { spawn } from 'node:child_process';
99

10+
/**
11+
* Common Windows console code pages (CP) used for encoding conversions.
12+
*
13+
* @remarks
14+
* - `UTF8` (65001): Unicode (UTF-8) — recommended for cross-language scripts.
15+
* - `GBK` (936): Simplified Chinese — default on most Chinese Windows systems.
16+
* - `BIG5` (950): Traditional Chinese.
17+
* - `LATIN1` (1252): Western European — default on many Western systems.
18+
*/
19+
export const CodePage = {
20+
UTF8: 65001,
21+
GBK: 936,
22+
BIG5: 950,
23+
LATIN1: 1252,
24+
} as const;
25+
26+
export type CodePage = (typeof CodePage)[keyof typeof CodePage];
1027
/**
1128
* Checks if a query string potentially represents an '@' command.
1229
* It triggers if the query starts with '@' or contains '@' preceded by whitespace
@@ -80,7 +97,7 @@ export const copyToClipboard = async (text: string): Promise<void> => {
8097

8198
switch (process.platform) {
8299
case 'win32':
83-
return run('clip', []);
100+
return run('cmd', ['/c', `chcp ${CodePage.UTF8} >nul && clip`]);
84101
case 'darwin':
85102
return run('pbcopy', []);
86103
case 'linux':

0 commit comments

Comments
 (0)