Skip to content

Commit

Permalink
Merge pull request #18 from TooTallNate/patch-1
Browse files Browse the repository at this point in the history
[lib] Don't use the private _events variable
  • Loading branch information
Paolo Fragomeni committed Jun 15, 2012
2 parents 6375609 + 9cb2452 commit 7145621
Showing 1 changed file with 39 additions and 37 deletions.
76 changes: 39 additions & 37 deletions cdir.js
@@ -1,19 +1,7 @@
var tty = require('tty');
var rl = require('readline');
var fs = require('fs');

//
// capture any existing events that we will use on stdin
//
var oldKeypressEvent, oldDataEvent, stdin;

if (process.stdin._events.keypress[0]) {
oldKeypressEvent = process.stdin._events.keypress[0];
}

if (process.stdin._events.data && process.stdin._events.data[0]) {
oldDataEvent = process.stdin._events.data[0];
}
var stdin;

//
// shorthand write to stdout
Expand Down Expand Up @@ -430,7 +418,7 @@ module.exports = function dir (obj, options) {
}

map = [];
for (var i = 0, l = meta.length; i < l; i++) {
for (var i = 0, l = meta.length; i < l; i++) {
if (meta[i].displayed === true) {
map.push(meta[i].index);
}
Expand Down Expand Up @@ -470,7 +458,7 @@ module.exports = function dir (obj, options) {
if (searchbuffer.length > 0) {

searchbuffer = searchbuffer.slice(0, -1);
write('\033[1D \033[1D');
write('\033[1D \033[1D');
}
}
else if (searchmode === true && typeof key !== 'undefined' && key.name === 'enter') {
Expand Down Expand Up @@ -587,7 +575,7 @@ module.exports = function dir (obj, options) {
return true;
}
else if (searchmode === true && typeof(chunk) !== 'undefined') {

write(chunk);
searchbuffer += chunk;
}
Expand Down Expand Up @@ -640,46 +628,61 @@ module.exports = function dir (obj, options) {

if (key.name === 'q' || (key.ctrl && key.name === 'c')) {

if (oldDataEvent) {
stdin.on('data', oldDataEvent);
if (!wasRaw) {
raw(false);
}

if (oldKeypressEvent) {
stdin.on('keypress', oldKeypressEvent);
stdin.emit('keypress', null, { name: 'enter' });
if (oldKeypressListeners.length > 0) {
oldKeypressListeners.forEach(function (l) {
stdin.on('keypress', l);
});
stdin.emit('keypress', '\r', { name: 'enter', ctrl: false, meta: false, shift: false });
stdin.resume();
}
else {
stdin.pause();
}
stdin.removeListener('keypress', listener);
tty.setRawMode(false);
}

}
};

if (oldKeypressEvent) {

stdin = process.stdin;
function raw (mode) {
var setRawMode = stdin.setRawMode || tty.setRawMode;
setRawMode.call(stdin, mode);
}
else {

stdin = process.openStdin();
tty.setRawMode(true);
stdin = process.stdin;
stdin.resume();

var wasRaw;
if ('isRaw' in stdin) {
wasRaw = stdin.isRaw
} else {
// node < v0.7.7, assume yes, makes running in the repl work
wasRaw = true
}
raw(true);

stdin.on('keypress', listener);

if (oldKeypressEvent) {
stdin.removeListener('keypress', oldKeypressEvent);
// node >= v0.7.7, "keypress" events don't automatically get emitted
if (rl.emitKeypressEvents) {
rl.emitKeypressEvents(stdin);
}

if (oldDataEvent) {
stdin.removeListener('data', oldDataEvent);
}
// grab hold of stdin's "keypress" listeners array
var keypressListeners = stdin.listeners('keypress');

// remove any current "keypress" listeners
var oldKeypressListeners = keypressListeners.splice(0);

stdin.on('keypress', listener);

// dont remove any "data" listeners, because one of
// them is the one that generates the "keypress" events

process.nextTick(function() {

up();
write('\033[K\r');
write('\033[K\r\n');
Expand All @@ -692,4 +695,3 @@ module.exports = function dir (obj, options) {
});

};

0 comments on commit 7145621

Please sign in to comment.