allow multiple programs to use the same streams correctly. see #157.

This commit is contained in:
Christopher Jeffrey 2015-07-22 05:43:50 -07:00
parent 5e31eb1370
commit e35811ae97
1 changed files with 36 additions and 24 deletions

View File

@ -363,30 +363,38 @@ Program.prototype._listenInput = function() {
key.full = name; key.full = name;
self.emit('keypress', ch, key); Program.list.forEach(function(self) {
self.emit('key ' + name, ch, key); self.emit('keypress', ch, key);
self.emit('key ' + name, ch, key);
});
}); });
this.input.on('data', function(data) { this.input.on('data', function(data) {
self.emit('data', data); Program.list.forEach(function(self) {
self.emit('data', data);
});
}); });
keys.emitKeypressEvents(this.input); keys.emitKeypressEvents(this.input);
this.on('newListener', function fn(type) { this.on('newListener', function fn(type) {
if (type === 'keypress' || type === 'mouse') { if (type === 'keypress' || type === 'mouse') {
self.removeListener('newListener', fn); Program.list.forEach(function(self) {
if (self.input.setRawMode && !self.input.isRaw) { self.removeListener('newListener', fn);
self.input.setRawMode(true); if (self.input.setRawMode && !self.input.isRaw) {
self.input.resume(); self.input.setRawMode(true);
} self.input.resume();
}
});
} }
}); });
this.on('newListener', function fn(type) { this.on('newListener', function fn(type) {
if (type === 'mouse') { if (type === 'mouse') {
self.removeListener('newListener', fn); Program.list.forEach(function(self) {
self.bindMouse(); self.removeListener('newListener', fn);
self.bindMouse();
});
} }
}); });
}; };
@ -402,23 +410,27 @@ Program.prototype._listenOutput = function() {
// Output // Output
function resize() { function resize() {
self.cols = self.output.columns; Program.list.forEach(function(self) {
self.rows = self.output.rows; self.cols = self.output.columns;
self.emit('resize'); self.rows = self.output.rows;
self.emit('resize');
});
} }
this.output.on('resize', function() { this.output.on('resize', function() {
if (!self.options.resizeTimeout) { Program.list.forEach(function(self) {
return resize(); if (!self.options.resizeTimeout) {
} return resize();
if (self._resizeTimer) { }
clearTimeout(self._resizeTimer); if (self._resizeTimer) {
delete self._resizeTimer; clearTimeout(self._resizeTimer);
} delete self._resizeTimer;
var time = typeof self.options.resizeTimeout === 'number' }
? self.options.resizeTimeout var time = typeof self.options.resizeTimeout === 'number'
: 300; ? self.options.resizeTimeout
self._resizeTimer = setTimeout(resize, time); : 300;
self._resizeTimer = setTimeout(resize, time);
});
}); });
}; };