From 52efd5169be1c45f47704d5bba4432d0f77007a5 Mon Sep 17 00:00:00 2001 From: Luke Plaster Date: Tue, 10 May 2016 14:49:22 +0800 Subject: [PATCH 1/3] win32: using UTF-8 and code page 65001 when piping to clip.exe --- index.js | 4 ++-- platform/fallbacks/copy.bat | 8 ++++++++ platform/win32.js | 33 ++++++++++++++++++++------------- test/copypaste.js | 12 +++++++++++- 4 files changed, 41 insertions(+), 16 deletions(-) create mode 100644 platform/fallbacks/copy.bat diff --git a/index.js b/index.js index 117cdf6..d2c9e16 100644 --- a/index.js +++ b/index.js @@ -51,7 +51,7 @@ exports.copy = function(text, callback) { .on("data", function(chunk) { err.push(chunk); }) .on("end", function() { if(err.length === 0) { return; } - done(new Error(config.decode(err))); + done(new Error(config.decode(err, true))); }) ; @@ -90,7 +90,7 @@ exports.paste = function(callback) { .on("end", function() { if(err.length === 0) { return; } - done(new Error(config.decode(err))); + done(new Error(config.decode(err, true))); }) ; } else { diff --git a/platform/fallbacks/copy.bat b/platform/fallbacks/copy.bat new file mode 100644 index 0000000..296ce83 --- /dev/null +++ b/platform/fallbacks/copy.bat @@ -0,0 +1,8 @@ +::Setting this code page is more likely to make isTextUnicode return true, making clip.exe do what we want. +chcp 65001 & ::UTF-8 + +clip & ::Reads stdin + +::This is the default code page for the US locale. It might not be the user's original code page. This *probably* doesn't matter. +::Restoring this code page makes cmd.exe behave nicely! Without this it freezes when running the tests. +chcp 437 & ::US ASCII (restore) diff --git a/platform/win32.js b/platform/win32.js index 8727acf..dfb61d7 100644 --- a/platform/win32.js +++ b/platform/win32.js @@ -1,22 +1,29 @@ var iconv = require("iconv-lite"); var path = require("path"); -var vbsPath = path.join(__dirname, ".\\fallbacks\\paste.vbs"); +var copyHelperPath = path.join(__dirname, ".\\fallbacks\\copy.bat"); +var pasteHelperPath = path.join(__dirname, ".\\fallbacks\\paste.vbs"); -var paste = { command: "cscript", args: [ "/Nologo", vbsPath ] }; -paste.full_command = [ paste.command, paste.args[0], '"'+vbsPath+'"' ].join(" "); +var paste = { command: "cscript", args: [ "/Nologo", pasteHelperPath ] }; +paste.full_command = [ paste.command, paste.args[0], '"'+pasteHelperPath+'"' ].join(" "); -exports.copy = { command: "clip", args: [] }; +exports.copy = { command: "cmd", args: [ "/U", "/C", copyHelperPath ] }; exports.paste = paste; -exports.encode = function(str) { return iconv.encode(str, "utf16le"); }; -exports.decode = function(chunks) { - if(!Array.isArray(chunks)) { chunks = [ chunks ]; } +// explicitly do not add BOM otherwise clip.exe might include the BOM in the text that is copied to the clipboard! +exports.encode = function(str) { return iconv.encode(str, "utf-8", {addBOM: false}); }; +exports.decode = function(chunks, isError) { + if (isError) { + // no base64 decoding error messages! + return chunks; + } else { + if(!Array.isArray(chunks)) { chunks = [ chunks ]; } - var b64 = iconv.decode(Buffer.concat(chunks), "cp437"); - b64 = b64.substr(0, b64.length - 2); // Chops off extra "\r\n" - - // remove bom and decode - var result = new Buffer(b64, "base64").slice(3).toString("utf-8"); - return result; + var b64 = iconv.decode(Buffer.concat(chunks), "cp437"); + b64 = b64.substr(0, b64.length - 2); // Chops off extra "\r\n" + + // remove bom and decode + var result = new Buffer(b64, "base64").slice(3).toString("utf8"); + return result; + } }; diff --git a/test/copypaste.js b/test/copypaste.js index ebe2914..1a8b84a 100644 --- a/test/copypaste.js +++ b/test/copypaste.js @@ -36,4 +36,14 @@ describe('copy and paste', function () { copy_and_paste("±", done); }); -}); \ No newline at end of file + + it('should work correctly for "❤"', function (done) { + + copy_and_paste("❤", done); + }); + + it('should work correctly for "돋움"', function (done) { + + copy_and_paste("돋움", done); + }); +}); From 49a1f0bc71253dd1846e8843e1228bbb32326d37 Mon Sep 17 00:00:00 2001 From: Luke Plaster Date: Thu, 12 May 2016 16:06:42 +0800 Subject: [PATCH 2/3] Bumped the version to bust npm's cache --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 8edcaad..fc5fbb4 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "name": "copy-paste" -, "version": "1.1.4" +, "version": "1.2.0-2" , "description": "A command line utility that allows read/write (i.e copy/paste) access to the system clipboard." , "keywords": [ "copy", "paste", "copy and paste", "clipboard" ] , "maintainers": From a6a811034fb70594759419a69aa0c62e2c30bec3 Mon Sep 17 00:00:00 2001 From: Luke Plaster Date: Fri, 13 May 2016 16:56:01 +0800 Subject: [PATCH 3/3] Revert "Bumped the version to bust npm's cache" This reverts commit 49a1f0bc71253dd1846e8843e1228bbb32326d37. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index fc5fbb4..8edcaad 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { "name": "copy-paste" -, "version": "1.2.0-2" +, "version": "1.1.4" , "description": "A command line utility that allows read/write (i.e copy/paste) access to the system clipboard." , "keywords": [ "copy", "paste", "copy and paste", "clipboard" ] , "maintainers":