buffer everything. cancel acs at the end of draw() if necessary.

This commit is contained in:
Christopher Jeffrey 2013-07-31 17:09:41 -05:00
parent 51771dc19a
commit f3df07a237
2 changed files with 63 additions and 4 deletions

View File

@ -66,6 +66,10 @@ function Program(options) {
|| process.env.TERM || process.env.TERM
|| 'xterm'; || 'xterm';
this._buf = '';
this.__flush = this._flush.bind(this);
process.on('exit', this.__flush);
if (!Program.global) { if (!Program.global) {
Program.global = this; Program.global = this;
} }
@ -1233,16 +1237,42 @@ Program.prototype._parseChar = function(text, attr) {
} }
}; };
Program.prototype._buffer = function(text) {
if (process._exiting) {
this._flush();
this.output.write(text);
return;
}
if (this._buf) {
this._buf += text;
return;
}
this._buf = text;
process.nextTick(this.__flush);
};
Program.prototype._flush = function(text) {
if (!this._buf) return;
this.output.write(this._buf);
this._buf = '';
};
Program.prototype._write = function(text) { Program.prototype._write = function(text) {
if (this.ret) return text; if (this.ret) return text;
if (this.options.buffer) {
return this._buffer(text);
}
return this.output.write(text); return this.output.write(text);
}; };
Program.prototype.echo = Program.prototype.echo =
Program.prototype.write = function(text, attr) { Program.prototype.write = function(text, attr) {
return attr return attr
? this.output.write(this.text(text, attr)) ? this._write(this.text(text, attr))
: this.output.write(text); : this._write(text);
}; };
Program.prototype._ncoords = function() { Program.prototype._ncoords = function() {
@ -3480,6 +3510,28 @@ Program.prototype.resume = function(callback) {
if (this._resume) return this._resume(); if (this._resume) return this._resume();
}; };
/**
* Helpers
*/
// We could do this easier by just manipulating the _events object, or for
// older versions of node, manipulating the array returned by listeners(), but
// neither of these methods are guaranteed to work in future versions of node.
function unshiftEvent(obj, event, listener) {
var listeners = obj.listeners(event);
obj.removeAllListeners(event);
obj.on(event, listener);
listeners.forEach(function(listener) {
obj.on(event, listener);
});
};
// Ensure _exiting is set for future versions of node that may remove it.
// We need this to be the first listener executed.
unshiftEvent(process, 'exit', function() {
process._exiting = true;
});
/** /**
* Expose * Expose
*/ */

View File

@ -267,6 +267,7 @@ function Screen(options) {
options = { program: options }; options = { program: options };
} }
options.buffer = true;
options.program = options.program options.program = options.program
|| require('./program').global || require('./program').global
|| new (require('./program'))(options); || new (require('./program'))(options);
@ -280,7 +281,6 @@ function Screen(options) {
this.program = options.program; this.program = options.program;
this.program.zero = true; this.program.zero = true;
this.tput = this.program.tput; this.tput = this.program.tput;
this.output = this.program.output;
this.autoPadding = options.autoPadding; this.autoPadding = options.autoPadding;
@ -1122,6 +1122,11 @@ Screen.prototype.draw = function(start, end) {
} }
} }
if (acs) {
main += this.tput.rmacs();
acs = false;
}
if (main) { if (main) {
var pre = '' var pre = ''
, post = ''; , post = '';
@ -1142,7 +1147,9 @@ Screen.prototype.draw = function(start, end) {
: '\x1b[?25h'; : '\x1b[?25h';
} }
this.output.write(pre + main + post); // this.program._flush();
// this.program.output.write(pre + main + post);
this.program._write(pre + main + post);
} }
}; };