diff --git a/packages/cli/src/ui/utils/commandUtils.test.ts b/packages/cli/src/ui/utils/commandUtils.test.ts index b48bb4c9..9d2fddd9 100644 --- a/packages/cli/src/ui/utils/commandUtils.test.ts +++ b/packages/cli/src/ui/utils/commandUtils.test.ts @@ -13,6 +13,7 @@ import { isSlashCommand, copyToClipboard, getUrlOpenCommand, + CodePage, } from './commandUtils.js'; // Mock child_process @@ -188,7 +189,10 @@ describe('commandUtils', () => { await copyToClipboard(testText); - expect(mockSpawn).toHaveBeenCalledWith('clip', []); + expect(mockSpawn).toHaveBeenCalledWith('cmd', [ + '/c', + `chcp ${CodePage.UTF8} >nul && clip`, + ]); expect(mockChild.stdin.write).toHaveBeenCalledWith(testText); expect(mockChild.stdin.end).toHaveBeenCalled(); }); diff --git a/packages/cli/src/ui/utils/commandUtils.ts b/packages/cli/src/ui/utils/commandUtils.ts index 32bebceb..89d1045a 100644 --- a/packages/cli/src/ui/utils/commandUtils.ts +++ b/packages/cli/src/ui/utils/commandUtils.ts @@ -7,6 +7,23 @@ import type { SpawnOptions } from 'node:child_process'; import { spawn } from 'node:child_process'; +/** + * Common Windows console code pages (CP) used for encoding conversions. + * + * @remarks + * - `UTF8` (65001): Unicode (UTF-8) — recommended for cross-language scripts. + * - `GBK` (936): Simplified Chinese — default on most Chinese Windows systems. + * - `BIG5` (950): Traditional Chinese. + * - `LATIN1` (1252): Western European — default on many Western systems. + */ +export const CodePage = { + UTF8: 65001, + GBK: 936, + BIG5: 950, + LATIN1: 1252, +} as const; + +export type CodePage = (typeof CodePage)[keyof typeof CodePage]; /** * Checks if a query string potentially represents an '@' command. * It triggers if the query starts with '@' or contains '@' preceded by whitespace @@ -80,7 +97,7 @@ export const copyToClipboard = async (text: string): Promise => { switch (process.platform) { case 'win32': - return run('clip', []); + return run('cmd', ['/c', `chcp ${CodePage.UTF8} >nul && clip`]); case 'darwin': return run('pbcopy', []); case 'linux':