optional printf.

This commit is contained in:
Christopher Jeffrey 2013-02-25 06:26:21 -06:00
parent ce69995f1d
commit 618ba14369
2 changed files with 22 additions and 8 deletions

View File

@ -44,13 +44,19 @@ function Program(options) {
this.terminal = process.env.TERM || 'xterm'; this.terminal = process.env.TERM || 'xterm';
if (options.tput) { 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() { this.put = function() {
var args = Array.prototype.slice.call(arguments) var args = Array.prototype.slice.call(arguments)
, cap = args.shift(); , cap = args.shift();
if (!this.tput[cap]) return; if (!this.tput[cap]) return;
return this.write(this.tput[cap].apply(this.tput, args)); return this.write(this.tput[cap].apply(this.tput, args));
}; };
Object.keys(this.tput).forEach(function(key) { Object.keys(this.tput).forEach(function(key) {
if (typeof self.tput[key] !== 'function') { if (typeof self.tput[key] !== 'function') {
self.put[key] = self.tput[key]; self.put[key] = self.tput[key];

View File

@ -38,6 +38,7 @@ function Tput(options) {
this.debug = options.debug; this.debug = options.debug;
this.padding = options.padding; this.padding = options.padding;
this.extended = options.extended; this.extended = options.extended;
this.printf = options.printf;
this.readTerminfo(); this.readTerminfo();
this.compile(); this.compile();
@ -636,16 +637,22 @@ Tput.prototype._compile = function(val) {
// next character to be a `-' flag, avoiding interpreting "%-" as an // next character to be a `-' flag, avoiding interpreting "%-" as an
// operator. // operator.
if (read(/^%((?::-|[+# ]){1,4})?(\d+(?:\.\d+)?)?([doxXs])/)) { if (read(/^%((?::-|[+# ]){1,4})?(\d+(?:\.\d+)?)?([doxXs])/)) {
// echo('sprintf("'+ cap[0].replace(':-', '-') + '", stack.pop())'); if (this.printf) {
echo('sprintf("'+ cap[0].replace(':-', '-') + '", stack.pop())');
} else {
echo('stack.pop()'); echo('stack.pop()');
}
continue; continue;
} }
// %c print pop() like %c in printf // %c print pop() like %c in printf
if (read(/^%c/)) { if (read(/^%c/)) {
// echo('sprintf("%c", stack.pop())'); if (this.printf) {
// echo('stack.pop()'); echo('sprintf("%c", stack.pop())');
} else {
// echo('stack.pop()'); // TODO: check screen terminfo for an example
echo('String.fromCharCode(stack.pop())'); echo('String.fromCharCode(stack.pop())');
}
continue; continue;
} }
@ -857,8 +864,9 @@ Tput.prototype._compile = function(val) {
} }
try { try {
return new Function('params', code); return this.printf
// return new Function('sprintf, params', code).bind(null, sprintf); ? new Function('sprintf, params', code).bind(null, sprintf)
: new Function('params', code);
} catch (e) { } catch (e) {
e.stack = e.stack.replace(/\x1b/g, '\\x1b'); e.stack = e.stack.replace(/\x1b/g, '\\x1b');
throw e; throw e;