const proc = require('child_process').spawn('pbcopy');
const iconv = require('iconv-lite');
const name = '吉星高照';
function iconvDecode(str = '') {
return iconv.decode(Buffer.from(str, 'binary'), 'cp936');
}
function pbcopy(data) {
proc.stdin.write(iconvDecode(data));
proc.stdin.end();
return data;
}
pbcopy(name);
剪贴板(在macOS上)的预期输出应该是吉星高照
,而不是˱®
。
我需要为pbcopy:
设置环境变量LC_CTYPE
const proc = require('child_process').spawn('pbcopy', {
env: {
LC_CTYPE: 'UTF-8',
},
});
const name = '吉星高照';
function pbcopy(data) {
proc.stdin.write(data);
proc.stdin.end();
return data;
}
pbcopy(name);