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

just merged this in case someone wants to use it... #102

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
language: node_js
node_js:
- "0.10"
- "0.8"
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ term.on('data', function(data) {
console.log(data);
});

term.on('close', function() {
console.log('exit status: %d', term.status);
});

term.write('ls\r');
term.resize(100, 40);
term.write('ls /\r');
Expand Down
89 changes: 27 additions & 62 deletions lib/pty.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ var net = require('net');
var tty = require('tty');
var extend = require('extend');
var pty = require('../build/Release/pty.node');
var stream = require('stream');
var util = require('util');

/**
* Terminal
Expand All @@ -23,10 +25,12 @@ var pty = require('../build/Release/pty.node');
// });

function Terminal(file, args, opt) {
Terminal.super_.call(this, opt);
if (!(this instanceof Terminal)) {
return new Terminal(file, args, opt);
}


var self = this
, env
, cwd
Expand Down Expand Up @@ -99,7 +103,7 @@ function Terminal(file, args, opt) {
// setup
this.socket.on('error', function(err) {
// close
self._close();
self.close();

// EIO, happens when someone closes our child
// process: the only process in the terminal.
Expand Down Expand Up @@ -131,12 +135,14 @@ function Terminal(file, args, opt) {
Terminal.total++;
this.socket.on('close', function() {
Terminal.total--;
self._close();
self.close();
self.emit('exit', null);
});

env = null;
}
util.inherits(Terminal, stream.Duplex);


Terminal.fork =
Terminal.spawn =
Expand Down Expand Up @@ -178,6 +184,7 @@ Terminal.open = function(opt) {
self.pid = null;
self.fd = term.master;
self.pty = term.pty;
self.canPush = false;

self.file = process.argv[0] || 'node';
self.name = process.env.TERM || '';
Expand All @@ -197,7 +204,12 @@ Terminal.open = function(opt) {
Terminal.total++;
self.socket.on('close', function() {
Terminal.total--;
self._close();
self.close();
});

self.socket.on('readable', function() {
if(self._canPush)
self._canPush = self.push(self.socket.read());
});

return self;
Expand All @@ -216,66 +228,13 @@ Terminal.total = 0;
* Events
*/

// Don't inherit from net.Socket in
// order to avoid collisions.

Terminal.prototype.write = function(data) {
return this.socket.write(data);
};

Terminal.prototype.end = function(data) {
return this.socket.end(data);
};

Terminal.prototype.pipe = function(dest, options) {
return this.socket.pipe(dest, options);
};

Terminal.prototype.pause = function() {
this.socket.pause();
};

Terminal.prototype.resume = function() {
this.socket.resume();
};

Terminal.prototype.setEncoding = function(enc) {
if (this.socket._decoder) {
delete this.socket._decoder;
}
if (enc) {
this.socket.setEncoding(enc);
}
};

Terminal.prototype.addListener =
Terminal.prototype.on = function(type, func) {
this.socket.on(type, func);
return this;
};

Terminal.prototype.emit = function() {
return this.socket.emit.apply(this.socket, arguments);
};

Terminal.prototype.listeners = function(type) {
return this.socket.listeners(type);
};

Terminal.prototype.removeListener = function(type, func) {
this.socket.removeListener(type, func);
return this;
};

Terminal.prototype.removeAllListeners = function(type) {
this.socket.removeAllListeners(type);
return this;
};
Terminal.prototype._read = function() {
this._canPush = this.push(this.socket.read());
}

Terminal.prototype.once = function(type, func) {
this.socket.once(type, func);
return this;
};
Terminal.prototype._write = function(chunk, encoding, callback) {
this.socket.write(chunk, encoding, callback);
}

Terminal.prototype.__defineGetter__('stdin', function() {
return this;
Expand Down Expand Up @@ -342,10 +301,16 @@ Terminal.prototype.redraw = function() {
}, 30);
};

Terminal.prototype.close = Terminal.prototype.end;

Terminal.prototype.__defineGetter__('process', function() {
return pty.process(this.fd, this.pty) || this.file;
});

Terminal.prototype.__defineGetter__('status', function() {
return pty.status(this.pid);
});

Terminal.prototype._close = function() {
this.socket.writable = false;
this.socket.readable = false;
Expand Down
12 changes: 6 additions & 6 deletions lib/pty_win.js
Original file line number Diff line number Diff line change
Expand Up @@ -157,12 +157,12 @@ function Terminal(file, args, opt) {
self.dataPipe = socket;

// These events needs to be forwarded.
['connect', 'data', 'end', 'timeout', 'drain'].forEach(function(event) {
self.dataPipe.on(event, function(data) {
['connect', 'readable', 'end', 'timeout', 'drain'].forEach(function(event) {
self.dataPipe.on(event, function() {

// Wait until the first data event is fired
// then we can run deferreds.
if(!self.isReady && event == 'data') {
if(!self.isReady && event == 'readable') {

// Terminal is now ready and we can
// avoid having to defer method calls.
Expand All @@ -184,7 +184,7 @@ function Terminal(file, args, opt) {
}

// Emit to dummy socket
self.socket.emit(event, data);
self.socket.emit.apply(self.socket, arguments);

});
});
Expand Down Expand Up @@ -263,9 +263,9 @@ Terminal.open = function () {
* Events
*/

Terminal.prototype.write = function(data) {
Terminal.prototype._write = function(chunk, encoding, callback) {
defer(this, function() {
this.dataPipe.write(data);
this.dataPipe.write(chunk, encoding, callback);
});
};

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
"terminal"
],
"dependencies": {
"extend": "~1.2.1",
"nan": "~1.0.0"
"extend": "~2.0.0",
"nan": "~1.4.1"
},
"devDependencies": {
"mocha": "~1.16.2"
"mocha": "~2.1.0"
}
}
Loading