use emitKeys() from node v0.12.2. see #91 #92 slap-editor/slap#74 slap-editor/slap#96.

This commit is contained in:
Christopher Jeffrey 2015-04-15 10:30:09 -07:00
parent 85be64273f
commit 4ca28d0860
1 changed files with 182 additions and 175 deletions

View File

@ -40,7 +40,7 @@ function emitKeypressEvents(stream) {
function onData(b) { function onData(b) {
if (EventEmitter.listenerCount(stream, 'keypress') > 0) { if (EventEmitter.listenerCount(stream, 'keypress') > 0) {
var r = stream._keypressDecoder.write(b); var r = stream._keypressDecoder.write(b);
if (r) emitKey(stream, r); if (r) emitKeys(stream, r);
} else { } else {
// Nobody's watching anyway // Nobody's watching anyway
stream.removeListener('data', onData); stream.removeListener('data', onData);
@ -94,20 +94,17 @@ exports.emitKeypressEvents = emitKeypressEvents;
// Regexes used for ansi escape code splitting // Regexes used for ansi escape code splitting
var metaKeyCodeReAnywhere = /(?:\x1b)([a-zA-Z0-9])/; var metaKeyCodeReAnywhere = /(?:\x1b)([a-zA-Z0-9])/;
var metaKeyCodeRe = new RegExp('^' + metaKeyCodeReAnywhere.source + '$'); var metaKeyCodeRe = new RegExp('^' + metaKeyCodeReAnywhere.source + '$');
var functionKeyCodeReAnywhere = var functionKeyCodeReAnywhere = new RegExp('(?:\x1b+)(O|N|\\[|\\[\\[)(?:' + [
/(?:\x1b+)(O|N|\[|\[\[)(?:(\d+)(?:;(\d+))?([~^$])|(?:1;)?(\d+)?([a-zA-Z]))/; '(\\d+)(?:;(\\d+))?([~^$])',
'(?:M([@ #!a`])(.)(.))', // mouse
'(?:1;)?(\\d+)?([a-zA-Z])'
].join('|') + ')');
var functionKeyCodeRe = new RegExp('^' + functionKeyCodeReAnywhere.source); var functionKeyCodeRe = new RegExp('^' + functionKeyCodeReAnywhere.source);
var escapeCodeReAnywhere = new RegExp([
functionKeyCodeReAnywhere.source, metaKeyCodeReAnywhere.source, /\x1b./.source
].join('|'));
function emitKey(stream, s) { function emitKeys(stream, s) {
var ch,
key = {
name: undefined,
ctrl: false,
meta: false,
shift: false
},
parts;
if (Buffer.isBuffer(s)) { if (Buffer.isBuffer(s)) {
if (s[0] > 127 && s[1] === undefined) { if (s[0] > 127 && s[1] === undefined) {
s[0] -= 128; s[0] -= 128;
@ -117,11 +114,27 @@ function emitKey(stream, s) {
} }
} }
if (isMouse(s)) { if (isMouse(s)) return;
return;
}
key.sequence = s; var buffer = [];
var match;
while (match = escapeCodeReAnywhere.exec(s)) {
buffer = buffer.concat(s.slice(0, match.index).split(''));
buffer.push(match[0]);
s = s.slice(match.index + match[0].length);
}
buffer = buffer.concat(s.split(''));
buffer.forEach(function(s) {
var ch,
key = {
sequence: s,
name: undefined,
ctrl: false,
meta: false,
shift: false
},
parts;
if (s === '\r') { if (s === '\r') {
// carriage return // carriage return
@ -178,8 +191,8 @@ function emitKey(stream, s) {
// reassemble the key code leaving out leading \x1b's, // reassemble the key code leaving out leading \x1b's,
// the modifier key bitflag and any meaningless "1;" sequence // the modifier key bitflag and any meaningless "1;" sequence
var code = (parts[1] || '') + (parts[2] || '') + var code = (parts[1] || '') + (parts[2] || '') +
(parts[4] || '') + (parts[6] || ''), (parts[4] || '') + (parts[9] || ''),
modifier = (parts[3] || parts[5] || 1) - 1; modifier = (parts[3] || parts[8] || 1) - 1;
// Parse the key modifier // Parse the key modifier
key.ctrl = !!(modifier & 4); key.ctrl = !!(modifier & 4);
@ -284,13 +297,6 @@ function emitKey(stream, s) {
default: key.name = 'undefined'; break; default: key.name = 'undefined'; break;
} }
} else if (s.length > 1 && s[0] !== '\x1b') {
// Got a longer-than-one string of characters.
// Probably a paste, since it wasn't a control sequence.
Array.prototype.forEach.call(s, function(c) {
emitKey(stream, c);
});
return;
} }
// Don't emit a key if no name was found // Don't emit a key if no name was found
@ -313,6 +319,7 @@ function emitKey(stream, s) {
// stream.emit('keypress', ch, nkey); // stream.emit('keypress', ch, nkey);
// } // }
} }
});
} }
function isMouse(s) { function isMouse(s) {