This commit is contained in:
Christopher Jeffrey 2013-02-15 14:10:23 -06:00
parent f5fdf2a9c0
commit 14df28fb35
1 changed files with 144 additions and 13 deletions

View File

@ -288,6 +288,8 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// CSI Ps ; Ps r
// CSI ? Pm r
var code = 'var dyn = {}, stat = {}, stack = []; out.push("';
// man terminfo, around line 940
// '\e' -> ^[
@ -295,16 +297,105 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// '^A' -> ^A
val = val.replace(/\^(.)/gi, function(_, ch) { // case-insensitive?
// TODO
return '';
switch (ch) {
case '@':
return '\x00';
case 'A':
return '\x01';
case 'B':
return '\x02';
case 'C':
return '\x03';
case 'D':
return '\x04';
case 'E':
return '\x05';
case 'F':
return '\x06';
case 'G':
return '\x07';
case 'H':
return '\x08';
case 'I':
return '\x09'; // \t
case 'J':
return '\x0a'; // \n
case 'K':
return '\x0b';
case 'L':
return '\x0c';
case 'M':
return '\x0d';
case 'N':
return '\x0e';
case 'O':
return '\x0f';
case 'P':
return '\x10';
case 'Q':
return '\x11';
case 'R':
return '\x12';
case 'S':
return '\x13';
case 'T':
return '\x14';
case 'U':
return '\x15';
case 'V':
return '\x16';
case 'W':
return '\x17';
case 'X':
return '\x18';
case 'Y':
return '\x19';
case 'Z':
return '\x1a';
case '\\':
return '\x1c';
case '^':
return '\x1e';
case '_':
return '\x1f';
case '[':
return '\x1b';
case ']':
return '\x1d';
case '?':
return '\x7f';
}
});
// '\n' -> \n
// '\r' -> \r
// '\0' -> \200 (special case)
val = val.replace(/\\([nlrtbfs\^\\,:0])/g, function(_, ch) {
// TODO
return '';
switch (ch) {
case 'n':
return '\n';
case 'l':
return '\l';
case 'r':
return '\r';
case 't':
return '\t';
case 'b':
return '\b';
case 'f':
return '\f';
case 's':
return '\s';
case '\\':
return '\\';
case ',':
return ',';
case ';':
return ';';
case '0':
//return '\0';
return '\200';
}
});
// 3 octal digits -> character
@ -314,6 +405,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// $<5> -> padding
val = val.replace(/\$<(\d+)>(\*|\/)/g, function(_, ch, opt) {
// code += '';
// TODO
return '';
return Array(+ch + 1).join(' '); // "padding" characters?
@ -326,7 +418,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %[[:]flags][width[.precision]][doxXs]
// as in printf, flags are [-+#] and space. Use a `:' to allow the
// next character to be a `-' flag, avoiding interpreting "%-" as an
// opera tor.
// operator.
val = val.replace(/%(?:(:)?([\-+# ]+)?)(?:(\d+)(\.\d+)?)?([doxXs])?/g, function() {
// TODO
return '';
@ -334,12 +426,14 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %c print pop() like %c in printf
val = val.replace(/%c/g, function() {
// code += 'out += stack.pop()'; // TODO: FORMAT
// TODO
return '';
});
// %s print pop() like %s in printf
val = val.replace(/%s/g, function() {
// code += 'out += stack.pop()'; // TODO: FORMAT
// TODO
return '';
});
@ -347,12 +441,14 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %p[1-9]
// push i'th parameter
val = val.replace(/%p([1-9])/g, function(_, i) {
// code += 'params[i]';
return params[i] || '';
});
// %P[a-z]
// set dynamic variable [a-z] to pop()
val = val.replace(/%P([a-z])/g, function(_, v) {
// code += 'dyn[' + v + '] = stack.pop()';
// TODO
return '';
});
@ -360,6 +456,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %g[a-z]
// get dynamic variable [a-z] and push it
val = val.replace(/%g([a-z])/g, function(_, v) {
// code += '(stack.push(dyn[' + v + ']), data[' + v + '])';
// TODO
return '';
});
@ -367,6 +464,7 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %P[A-Z]
// set static variable [a-z] to pop()
val = val.replace(/%P([A-Z])/g, function(_, v) {
// code += 'stat[' + v + '] = stack.pop()';
// TODO
return '';
});
@ -386,7 +484,8 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
});
// %'c' char constant c
val = val.replace(/%'c'/g, function() {
val = val.replace(/%'(\w)'/g, function(_, ch) {
// code += '"' + ch + '"';
// TODO
return '';
});
@ -394,33 +493,38 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %{nn}
// integer constant nn
val = val.replace(/%\{(\d+)\}/g, function(_, nn) {
// code += '(' + ch + ')';
// TODO
return '';
});
// %l push strlen(pop)
val = val.replace(/%l/g, function() {
// code += 'stack.push(stack.pop().length)';
// TODO
return '';
});
// %+ %- %* %/ %m
// arithmetic (%m is mod): push(pop() op pop())
val = val.replace(/%([+\-*\/m])/g, function(_, p) {
val = val.replace(/%([+\-*\/m])/g, function(_, op) {
// code += 'stack.push(stack.pop() ' + op + ' stack.pop())';
// TODO
return '';
});
// %& %| %^
// bit operations (AND, OR and exclusive-OR): push(pop() op pop())
val = val.replace(/%([&|\^])/g, function(_, v) {
val = val.replace(/%([&|\^])/g, function(_, op) {
// code += 'stack.push(stack.pop() ' + op + ' stack.pop())';
// TODO
return '';
});
// %= %> %<
// logical operations: push(pop() op pop())
val = val.replace(/%([=><])/g, function(_, v) {
val = val.replace(/%([=><])/g, function(_, op) {
// code += 'stack.push(stack.pop() ' + op + ' stack.pop())';
// TODO
return '';
});
@ -428,19 +532,22 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// %A, %O
// logical AND and OR operations (for conditionals)
val = val.replace(/%([AO])/g, function(_, v) {
// code += v === ' A ' ? ' && ' : ' || ';
// TODO
return '';
});
// %! %~
// unary operations (logical and bit complement): push(op pop())
val = val.replace(/%([!~])/g, function(_, v) {
val = val.replace(/%([!~])/g, function(_, op) {
// code += 'stack.push(' + op + 'stack.pop())';
// TODO
return '';
});
// %i add 1 to first two parameters (for ANSI terminals)
val = val.replace(/%i/g, function(_, v) {
// code += '(params[0]++, params[1]++)';
// TODO
return '';
});
@ -460,9 +567,33 @@ Tput.prototype.invoke = function(key, prefix, params, suffix) {
// if-then-else's. Some strings, e.g., sgr can be very complicated when
// written on one line. The -f option splits the string into lines with
// the parts indented.
val = val.replace(/%\?(.+?)%t(.+?)%e(.+?)%;/g, function(_, expr, thenpart, elsepart) {
// TODO: Generate code:
// code += ';if (' + parse(expr) + ') {' + out(thenpart) + '} else {' + out(elsepart) + '}';
//val = val.replace(/%\?(.+?)%t(.+?)%e(.+?)%;/g, function(_, expr, thenpart, elsepart) {
// // TODO: Generate code:
// // code += ';if (' + parse(expr) + ') {' + out(thenpart) + '} else {' + out(elsepart) + '}';
// // TODO
// return '';
//});
val = val.replace(/%\?/g, function(_, expr, thenpart, elsepart) {
// code += ';if (';
// TODO
return '';
});
val = val.replace(/%t/g, function(_, expr, thenpart, elsepart) {
// code += ') {';
// TODO
return '';
});
val = val.replace(/%e/g, function(_, expr, thenpart, elsepart) {
// code += '} else {';
// TODO
return '';
});
val = val.replace(/%;/g, function(_, expr, thenpart, elsepart) {
// code += '}';
// TODO
return '';
});