From 618ba1436937a71a7a8798098a1cfc40e961bf8b Mon Sep 17 00:00:00 2001 From: Christopher Jeffrey Date: Mon, 25 Feb 2013 06:26:21 -0600 Subject: [PATCH] optional printf. --- lib/program.js | 8 +++++++- lib/tput.js | 22 +++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lib/program.js b/lib/program.js index 56b8c63..369b270 100644 --- a/lib/program.js +++ b/lib/program.js @@ -44,13 +44,19 @@ function Program(options) { this.terminal = process.env.TERM || 'xterm'; if (options.tput) { - this.tput = new Tput(this.terminal); + this.tput = new Tput({ + term: this.terminal, + extended: options.extended || false, + printf: options.printf || false + }); + this.put = function() { var args = Array.prototype.slice.call(arguments) , cap = args.shift(); if (!this.tput[cap]) return; return this.write(this.tput[cap].apply(this.tput, args)); }; + Object.keys(this.tput).forEach(function(key) { if (typeof self.tput[key] !== 'function') { self.put[key] = self.tput[key]; diff --git a/lib/tput.js b/lib/tput.js index 6275f88..67d2d16 100644 --- a/lib/tput.js +++ b/lib/tput.js @@ -38,6 +38,7 @@ function Tput(options) { this.debug = options.debug; this.padding = options.padding; this.extended = options.extended; + this.printf = options.printf; this.readTerminfo(); this.compile(); @@ -636,16 +637,22 @@ Tput.prototype._compile = function(val) { // next character to be a `-' flag, avoiding interpreting "%-" as an // operator. if (read(/^%((?::-|[+# ]){1,4})?(\d+(?:\.\d+)?)?([doxXs])/)) { - // echo('sprintf("'+ cap[0].replace(':-', '-') + '", stack.pop())'); - echo('stack.pop()'); + if (this.printf) { + echo('sprintf("'+ cap[0].replace(':-', '-') + '", stack.pop())'); + } else { + echo('stack.pop()'); + } continue; } // %c print pop() like %c in printf if (read(/^%c/)) { - // echo('sprintf("%c", stack.pop())'); - // echo('stack.pop()'); - echo('String.fromCharCode(stack.pop())'); + if (this.printf) { + echo('sprintf("%c", stack.pop())'); + } else { + // echo('stack.pop()'); // TODO: check screen terminfo for an example + echo('String.fromCharCode(stack.pop())'); + } continue; } @@ -857,8 +864,9 @@ Tput.prototype._compile = function(val) { } try { - return new Function('params', code); - // return new Function('sprintf, params', code).bind(null, sprintf); + return this.printf + ? new Function('sprintf, params', code).bind(null, sprintf) + : new Function('params', code); } catch (e) { e.stack = e.stack.replace(/\x1b/g, '\\x1b'); throw e;