Sahli/16col/ansi.js

267 lines
8.4 KiB
JavaScript
Raw Normal View History

// Generated by CoffeeScript 1.7.1
(function() {
var ANSI_BG, ANSI_BG_INTENSE, ANSI_CSI, ANSI_CUR_DOWN, ANSI_ESC, ANSI_FG, ANSI_FG_INTENSE, ANSI_RESET, ANSI_RETURN, ANSI_SEP, ANSI_TEXT_PROP;
ANSI_ESC = String.fromCharCode(0x1b);
ANSI_CSI = ANSI_ESC + '[';
ANSI_TEXT_PROP = 'm';
ANSI_RESET = '0';
ANSI_FG = '3';
ANSI_BG = '4';
ANSI_FG_INTENSE = '9';
ANSI_BG_INTENSE = '10';
ANSI_CUR_DOWN = 'B';
ANSI_SEP = ';';
ANSI_RETURN = 'A';
({
write: function() {
var attr, content, max_x, pixel, prevattr, x, y, _i, _j, _ref, _ref1;
content = "" + ANSI_CSI + "2J";
for (y = _i = 0, _ref = this.screen.length; 0 <= _ref ? _i < _ref : _i > _ref; y = 0 <= _ref ? ++_i : --_i) {
if (this.screen[y] == null) {
content += "\n";
}
if (this.screen[y] == null) {
continue;
}
for (x = _j = 0, _ref1 = this.getWidth(); 0 <= _ref1 ? _j < _ref1 : _j > _ref1; x = 0 <= _ref1 ? ++_j : --_j) {
pixel = this.screen[y][x];
if (pixel == null) {
pixel = {
ch: ' ',
attr: 7
};
}
attr = this.gen_args(pixel.attr);
if (attr !== prevattr) {
content += "" + ANSI_CSI + "0;" + attr + ANSI_TEXT_PROP;
prevattr = attr;
}
content += pixel.ch != null ? pixel.ch : ' ';
max_x = x;
}
}
content += "" + ANSI_CSI + "0m";
return this.toBinaryArray(content);
},
gen_args: function(attr) {
var a, attrs, bg, bl, fg, intense, _i, _len, _ref;
fg = 30 + (attr & 7);
bg = 40 + ((attr & 112) >> 4);
bl = attr & 128 ? 5 : '';
intense = attr & 8 ? 1 : '';
_ref = [fg, bg, bl, intense];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
a = _ref[_i];
if (a !== '') {
attrs = a;
}
}
return [fg, bg, bl, intense].join(";");
},
parse: function(content) {
var arg, args, ch, i, _i, _j, _k, _l, _len, _m, _n, _o, _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _results;
this.screen = [];
this.state = 0;
this.x = 0;
this.y = 0;
this.save_x = 0;
this.save_y = 0;
this.attr = 7;
this.argbuf = '';
content = content.split('');
_results = [];
while (ch = content.shift()) {
if (this.state === 0) {
switch (ch) {
case "\x1a":
_results.push(this.state = 3);
break;
case "\x1b":
_results.push(this.state = 1);
break;
case "\n":
this.x = 0;
_results.push(this.y++);
break;
case "\r":
break;
case "\t":
i = (this.x + 1) % this.tabstop;
_results.push((function() {
var _results1;
_results1 = [];
while (i-- > 0) {
_results1.push(this.putpixel(' '));
}
return _results1;
}).call(this));
break;
default:
_results.push(this.putpixel(ch));
}
} else if (this.state === 1) {
if (ch !== "[") {
this.putpixel("\x1b");
this.putpixel("[");
_results.push(this.state = 0);
} else {
_results.push(this.state = 2);
}
} else if (this.state === 2) {
if (ch.match('[A-Za-z]')) {
args = (function() {
var _i, _len, _ref, _results1;
_ref = this.argbuf.split(';');
_results1 = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
i = _ref[_i];
_results1.push(parseInt(i));
}
return _results1;
}).call(this);
switch (ch) {
case "m":
for (_i = 0, _len = args.length; _i < _len; _i++) {
arg = args[_i];
if (arg === 0) {
this.attr = 7;
} else if (arg === 1) {
this.attr |= 8;
} else if (arg === 5) {
this.attr |= 128;
} else if ((30 <= arg && arg <= 37)) {
this.attr &= 248;
this.attr |= arg - 30;
} else if ((40 <= arg && arg <= 47)) {
this.attr &= 143;
this.attr |= (arg - 40) << 4;
}
}
break;
case "H":
case "f":
this.y = (args[0] || 1) - 1;
this.x = (args[1] || 1) - 1;
if (this.y < 0) {
this.y = 0;
}
if (this.x < 0) {
this.x = 0;
}
break;
case "A":
this.y -= args[0] || 1;
if (this.y < 0) {
this.y = 0;
}
break;
case "B":
this.y += args[0] || 1;
break;
case "C":
this.x += args[0] || 1;
break;
case "D":
this.x -= args[0] || 1;
if (this.x < 0) {
this.x = 0;
}
break;
case "E":
this.y += args[0] || 1;
this.x = 0;
break;
case "F":
this.y -= args[0] || 1;
if (this.y > 0) {
this.y = 0;
}
this.x = 0;
break;
case "G":
this.x = (args[0] || 1) - 1;
break;
case "s":
this.save_x = this.x;
this.save_y = this.y;
break;
case "u":
this.x = this.save_x;
this.y = this.save_y;
break;
case "J":
if (args.length === 0 || args[0] === 0) {
for (i = _j = _ref = this.y + 1, _ref1 = screen.length - 1; _ref <= _ref1 ? _j <= _ref1 : _j >= _ref1; i = _ref <= _ref1 ? ++_j : --_j) {
this.screen[i] = null;
}
for (i = _k = _ref2 = this.x, _ref3 = screen[this.y].length - 1; _ref2 <= _ref3 ? _k <= _ref3 : _k >= _ref3; i = _ref2 <= _ref3 ? ++_k : --_k) {
this.screen[this.y][i] = null;
}
} else if (args[0] === 1) {
for (i = _l = 0, _ref4 = this.y - 1; 0 <= _ref4 ? _l <= _ref4 : _l >= _ref4; i = 0 <= _ref4 ? ++_l : --_l) {
this.screen[i] = null;
}
for (i = _m = 0, _ref5 = this.x; 0 <= _ref5 ? _m <= _ref5 : _m >= _ref5; i = 0 <= _ref5 ? ++_m : --_m) {
this.screen[this.y][i] = null;
}
} else if (args[0] === 2) {
this.x = 0;
this.y = 0;
this.screen = [];
}
break;
case "K":
if (args.length === 0 || args[0] === 0) {
for (i = _n = _ref6 = this.x, _ref7 = this.screen[this.y].length - 1; _ref6 <= _ref7 ? _n <= _ref7 : _n >= _ref7; i = _ref6 <= _ref7 ? ++_n : --_n) {
this.screen[this.y][i] = null;
}
} else if (args[0] === 1) {
for (i = _o = 0, _ref8 = this.x; 0 <= _ref8 ? _o <= _ref8 : _o >= _ref8; i = 0 <= _ref8 ? ++_o : --_o) {
this.screen[this.y][i] = null;
}
} else if (args[0] === 2) {
this.screen[this.y] = null;
}
}
this.argbuf = '';
_results.push(this.state = 0);
} else {
_results.push(this.argbuf += ch);
}
} else if (this.state === 3) {
break;
} else {
_results.push(this.state = 0);
}
}
return _results;
},
putpixel: function(ch) {
if (this.screen[this.y] == null) {
this.screen[this.y] = [];
}
this.screen[this.y][this.x] = {
ch: ch,
attr: this.attr
};
if (++this.x >= this.linewrap) {
this.x = 0;
return this.y++;
}
}
});
}).call(this);