Skip to content

Commit e60acd2

Browse files
fix: utf8 on windows (#35)
1 parent 0065ba6 commit e60acd2

2 files changed

Lines changed: 24 additions & 6 deletions

File tree

src/index.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {spawn} from 'node:child_process';
22

3-
const TIMEOUT = 2000;
3+
const TIMEOUT = 3000;
44

55
function checkUnixCommandExists(command: string): Promise<boolean> {
66
return new Promise((resolve) => {
@@ -12,17 +12,26 @@ function checkUnixCommandExists(command: string): Promise<boolean> {
1212

1313
type Command = [string, Array<string>];
1414

15+
const WINDOWS_READ_COMMAND: Command = [
16+
'powershell',
17+
[
18+
'-NoProfile',
19+
'-Command',
20+
'[Console]::OutputEncoding = [Text.UTF8Encoding]::new($false); Get-Clipboard'
21+
]
22+
];
23+
1524
async function getReadCommand(): Promise<Command | undefined> {
1625
switch (process.platform) {
1726
case 'darwin':
1827
return ['pbpaste', []];
1928
case 'win32':
20-
return ['powershell', ['Get-Clipboard']];
29+
return WINDOWS_READ_COMMAND;
2130
case 'linux':
2231
case 'freebsd':
2332
case 'openbsd':
2433
if (process.env.WSL_DISTRO_NAME) {
25-
return ['powershell.exe', ['-noprofile', '-command', 'Get-Clipboard']];
34+
return WINDOWS_READ_COMMAND;
2635
}
2736
if (process.env.WAYLAND_DISPLAY) {
2837
return ['wl-paste', []];
@@ -69,17 +78,26 @@ export function readText(): Promise<string> {
6978
});
7079
}
7180

81+
const WINDOWS_WRITE_COMMAND: Command = [
82+
'powershell',
83+
[
84+
'-NoProfile',
85+
'-Command',
86+
'[Console]::InputEncoding = [Text.UTF8Encoding]::new($false); [Console]::In.ReadToEnd() | Set-Clipboard'
87+
]
88+
];
89+
7290
async function getWriteCommand(): Promise<Command | undefined> {
7391
switch (process.platform) {
7492
case 'darwin':
7593
return ['pbcopy', []];
7694
case 'win32':
77-
return ['clip', []];
95+
return WINDOWS_WRITE_COMMAND;
7896
case 'linux':
7997
case 'freebsd':
8098
case 'openbsd':
8199
if (process.env.WSL_DISTRO_NAME) {
82-
return ['clip.exe', []];
100+
return WINDOWS_WRITE_COMMAND;
83101
}
84102
if (process.env.WAYLAND_DISPLAY) {
85103
return ['wl-copy', []];

tests/index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ describe('clipboard', () => {
1414
});
1515

1616
it('should copy then read successfully', async () => {
17-
const text = Math.random().toString();
17+
const text = '❤️' + Math.random().toString();
1818
await clipboard.writeText(text);
1919
expect(await clipboard.readText()).toEqual(text);
2020
});

0 commit comments

Comments
 (0)