From 0372788f2d85f0d8e6463b7c45928be7d1c6093f Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Wed, 24 Jul 2013 13:23:35 -0500 Subject: [PATCH] add a buffering write function for program. --- lib/program.js | 42 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 34 insertions(+), 8 deletions(-) diff --git a/lib/program.js b/lib/program.js index 5cd21bc..03442a9 100644 --- a/lib/program.js +++ b/lib/program.js @@ -78,6 +78,8 @@ function Program(options) { this.setupTput(); } + this.__flush = this._flush.bind(this); + this.listen(); } @@ -1162,13 +1164,37 @@ Program.prototype._parseChar = function(text, attr) { } }; -Program.prototype.write = -Program.prototype.echo = function(text, attr) { - // if (this.output === process.stdout) { - // return attr - // ? Program._write.call(this.output, this.text(text, attr)) - // : Program._write.call(this.output, text); - // } +Program.prototype.echo_ = +Program.prototype.write_ = function(text, attr) { + if (!text) return; + + if (attr) { + text = this.text(text, attr); + } + + if (this._buf) { + this._buf += text; + return true; + } + + this._buf = text; + + //if (!this.__flush) { + // this.__flush = this._flush.bind(this); + //} + + process.nextTick(this.__flush); + + return true; +}; + +Program.prototype._flush = function() { + this.output.write(this._buf); + this._buf = ''; +}; + +Program.prototype.echo = +Program.prototype.write = function(text, attr) { return attr ? this.output.write(this.text(text, attr)) : this.output.write(text); @@ -1261,7 +1287,7 @@ Program.prototype.repeat = function(ch, i) { //Program.prototype.pad = Program.prototype.nul = function() { //if (this.has('pad')) return this.put.pad(); - return this.write('\0'); + return this.write('\200'); }; Program.prototype.bel =