lazily set raw mode.

This commit is contained in:
Christopher Jeffrey 2013-02-07 12:33:50 -06:00
parent 1428b7137b
commit 9020f645af
1 changed files with 38 additions and 32 deletions

View File

@ -44,43 +44,42 @@ Program.prototype.term = function(is) {
};
Program.prototype.listen = function() {
if (!this.input.isTTY || !this.output.isTTY) {
return;
}
var readline = require('readline')
, self = this;
if (!this.input.isRaw) {
this.input.setRawMode(true);
this.input.on('keypress', function(ch, key) {
key = key || 0;
if (key.ctrl && key.name === 'c') {
if (process.listeners('SIGINT').length) {
process.emit('SIGINT');
}
if (self.listeners('SIGINT').length) {
self.emit('SIGINT');
}
return;
}
self.emit('keypress', ch, key);
});
this.input.on('data', function(data) {
self.emit('data', data);
});
readline.emitKeypressEvents(this.input);
this.input.resume();
if (!this.input.isTTY || !this.output.isTTY) {
throw new Error('Not a terminal.');
}
this.output.on('resize', function() {
self.cols = self.output.columns;
self.rows = self.output.rows;
self.emit('resize');
// Input
this.input.on('keypress', function(ch, key) {
key = key || 0;
if (key.ctrl && key.name === 'c') {
if (process.listeners('SIGINT').length) {
process.emit('SIGINT');
}
if (self.listeners('SIGINT').length) {
self.emit('SIGINT');
}
return;
}
self.emit('keypress', ch, key);
});
this.input.on('data', function(data) {
self.emit('data', data);
});
readline.emitKeypressEvents(this.input);
this.on('newListener', function fn(type) {
if (type === 'keypress' || type === 'mouse') {
self.removeListener('newListener', fn);
if (!self.input.isRaw) {
self.input.setRawMode(true);
self.input.resume();
}
}
});
this.on('newListener', function fn(type) {
@ -92,6 +91,13 @@ Program.prototype.listen = function() {
// self.bindMouse();
// self.bindResponse();
// Output
this.output.on('resize', function() {
self.cols = self.output.columns;
self.rows = self.output.rows;
self.emit('resize');
});
};
// XTerm mouse events