Skip to content

Instantly share code, notes, and snippets.

@Andrews54757
Created November 28, 2016 01:49
Show Gist options
  • Save Andrews54757/cab20e450cd81efb4e5459042f3d0ed4 to your computer and use it in GitHub Desktop.
Save Andrews54757/cab20e450cd81efb4e5459042f3d0ed4 to your computer and use it in GitHub Desktop.
ANSI code getter
var stdin = process.stdin;
stdin.setRawMode(true);
stdin.resume()
stdin.setEncoding('utf8');
stdin.on('data',function(key) {
console.log(toUnicode(key));
if (key == '\u0003') { process.exit(); }
})
function toUnicode(theString) {
var unicodeString = '';
for (var i=0; i < theString.length; i++) {
var theUnicode = theString.charCodeAt(i).toString(16).toUpperCase();
while (theUnicode.length < 4) {
theUnicode = '0' + theUnicode;
}
theUnicode = '\\u' + theUnicode;
unicodeString += theUnicode;
}
return unicodeString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment