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';
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];

View File

@ -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;