Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use the private _events variable #18

Merged
merged 1 commit into from Jun 15, 2012
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -639,46 +627,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 @@ -691,4 +694,3 @@ module.exports = function dir (obj, options) {
});

};