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,16 +44,14 @@ 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);
if (!this.input.isTTY || !this.output.isTTY) {
throw new Error('Not a terminal.');
}
// Input
this.input.on('keypress', function(ch, key) {
key = key || 0;
if (key.ctrl && key.name === 'c') {
@ -74,13 +72,14 @@ Program.prototype.listen = function() {
readline.emitKeypressEvents(this.input);
this.input.resume();
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.output.on('resize', function() {
self.cols = self.output.columns;
self.rows = self.output.rows;
self.emit('resize');
});
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