fix: character encoding corruption when executing the /copy command on Windows. (#1069)

Co-authored-by: linda <hxn@163.com>
This commit is contained in:
citlalinda
2025-11-20 10:23:17 +08:00
committed by GitHub
parent 3c64f7bff5
commit e1f793b2e0
2 changed files with 23 additions and 2 deletions

View File

@@ -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();
});

View File

@@ -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<void> => {
switch (process.platform) {
case 'win32':
return run('clip', []);
return run('cmd', ['/c', `chcp ${CodePage.UTF8} >nul && clip`]);
case 'darwin':
return run('pbcopy', []);
case 'linux':