From 78b858742b63c4651e623aaed19289e37675f0bf Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 27 Jul 2015 21:42:33 -0700 Subject: [PATCH] fix tput.js for browserifying. fixes #158 again. --- browser/Makefile | 7 ++++ browser/transform.js | 90 +++++++++++++++++++++++++++++++++++++------- 2 files changed, 84 insertions(+), 13 deletions(-) create mode 100644 browser/Makefile diff --git a/browser/Makefile b/browser/Makefile new file mode 100644 index 0000000..1bd040c --- /dev/null +++ b/browser/Makefile @@ -0,0 +1,7 @@ +all: + @cd .. && browserify -e index.js -o browser/blessed.js + +clean: + @rm -f blessed.js + +.PHONY: clean all diff --git a/browser/transform.js b/browser/transform.js index 99f23ee..3030fb5 100644 --- a/browser/transform.js +++ b/browser/transform.js @@ -8,30 +8,94 @@ var Transform = require('stream').Transform , path = require('path') , fs = require('fs'); -var widgets = fs.readdirSync(__dirname + '/../lib/widgets'); +/** + * Transformer + */ -var requires = widgets.reduce(function(out, name) { - name = path.basename(name, '.js'); - out += '\nrequire(\'./widgets/' + name + '\');'; - return out; -}, ''); - -function transform(target) { +function transformer(code) { var stream = new Transform; stream._transform = function(chunk, encoding, callback) { return callback(null, chunk); }; stream._flush = function(callback) { - if (target) { - stream.push(requires); + if (code) { + stream.push(code); } return callback(); }; return stream; } +/** + * Explicitly require all widgets in widget.js + */ + +var widgets = fs.readdirSync(__dirname + '/../lib/widgets'); + +var requireWidgets = widgets.reduce(function(out, name) { + name = path.basename(name, '.js'); + out += '\nrequire(\'./widgets/' + name + '\');'; + return out; +}, ''); + +/** + * Do not make filesystem calls in tput.js for + * terminfo or termcap, just use xterm terminfo/cap. + */ + +var infoPath = path.resolve(__dirname, '..', 'usr', 'xterm') + , capPath = path.resolve(__dirname, '..', 'usr', 'xterm.termcap'); + +var infoPathFake = path.resolve( + path.sep, 'usr', 'share', 'terminfo', + path.basename(infoPath)[0], + path.basename(infoPath) +); + +function readMethods() { + Tput._infoBuffer = new Buffer(TERMINFO, 'base64'); + + Tput.prototype.readTerminfo = function() { + this.terminal = TERMINFO_NAME; + return this.parseTerminfo(Tput._infoBuffer, TERMINFO_PATH); + }; + + Tput.cpaths = []; + Tput.termcap = TERMCAP; + + Tput.prototype._readTermcap = Tput.prototype.readTermcap; + Tput.prototype.readTermcap = function() { + this.terminal = TERMCAP_NAME; + return this._readTermcap(this.terminal); + }; +} + +readMethods = readMethods.toString().slice(24, -2) + .replace(/^ /gm, '') + .replace('TERMINFO', JSON.stringify(fs.readFileSync(infoPath, 'base64'))) + .replace('TERMINFO_NAME', JSON.stringify(path.basename(infoPath))) + .replace('TERMINFO_PATH', JSON.stringify(infoPathFake)) + .replace('TERMCAP', JSON.stringify(fs.readFileSync(capPath, 'utf8'))) + .replace('TERMCAP_NAME', JSON.stringify(path.basename(capPath, '.termcap'))); + +/** + * Helpers + */ + +function end(file, offset) { + return file.split(path.sep).slice(offset).join(path.sep); +} + +/** + * Expose + */ + module.exports = function(file) { - return path.basename(file) === 'widget.js' - ? transform(true) - : transform(); + if (end(file, -2) === 'lib/widget.js') { + return transformer(requireWidgets); + } + if (end(file, -2) === 'lib/tput.js') { + return transformer(readMethods); + } + return transformer(); };