Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

ansi_up

Package Overview
Dependencies
Maintainers
1
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ansi_up - npm Package Compare versions

Comparing version 1.3.0 to 2.0.0

.vscode/launch.json

622

ansi_up.js

@@ -1,327 +0,333 @@

// ansi_up.js
// version : 1.3.0
// author : Dru Nelson
// license : MIT
// http://github.com/drudru/ansi_up
/* ansi_up.js
* author : Dru Nelson
* license : MIT
* http://github.com/drudru/ansi_up
*/
(function (factory) {
var v;
if (typeof module === "object" && typeof module.exports === "object") {
v = factory(require, exports);
if ("undefined" !== typeof v) module.exports = v;
}
else if ("function" === typeof define && define.amd) {
define(["require", "exports"], factory);
}
else {
var req, exp = {};
v = factory(req, exp);
window.AnsiUp = exp.default;
}
})(function (require, exports) {
(function (Date, undefined) {
var ansi_up,
VERSION = "1.3.0",
// check for nodeJS
hasModule = (typeof module !== 'undefined'),
// Normal and then Bright
ANSI_COLORS = [
[
{ color: "0, 0, 0", 'class': "ansi-black" },
{ color: "187, 0, 0", 'class': "ansi-red" },
{ color: "0, 187, 0", 'class': "ansi-green" },
{ color: "187, 187, 0", 'class': "ansi-yellow" },
{ color: "0, 0, 187", 'class': "ansi-blue" },
{ color: "187, 0, 187", 'class': "ansi-magenta" },
{ color: "0, 187, 187", 'class': "ansi-cyan" },
{ color: "255,255,255", 'class': "ansi-white" }
],
[
{ color: "85, 85, 85", 'class': "ansi-bright-black" },
{ color: "255, 85, 85", 'class': "ansi-bright-red" },
{ color: "0, 255, 0", 'class': "ansi-bright-green" },
{ color: "255, 255, 85", 'class': "ansi-bright-yellow" },
{ color: "85, 85, 255", 'class': "ansi-bright-blue" },
{ color: "255, 85, 255", 'class': "ansi-bright-magenta" },
{ color: "85, 255, 255", 'class': "ansi-bright-cyan" },
{ color: "255, 255, 255", 'class': "ansi-bright-white" }
]
],
// 256 Colors Palette
PALETTE_COLORS;
function Ansi_Up() {
this.fg = this.bg = this.fg_truecolor = this.bg_truecolor = null;
this.bright = 0;
"use strict";
function rgx(tmplObj) {
var subst = [];
for (var _i = 1; _i < arguments.length; _i++) {
subst[_i - 1] = arguments[_i];
}
Ansi_Up.prototype.setup_palette = function() {
PALETTE_COLORS = [];
// Index 0..15 : System color
(function() {
var i, j;
for (i = 0; i < 2; ++i) {
for (j = 0; j < 8; ++j) {
PALETTE_COLORS.push(ANSI_COLORS[i][j]['color']);
}
}
})();
// Index 16..231 : RGB 6x6x6
// https://gist.github.com/jasonm23/2868981#file-xterm-256color-yaml
(function() {
var regexText = tmplObj.raw[0];
var wsrgx = /^\s+|\s+\n|\s+#[\s\S]+?\n/gm;
var txt2 = regexText.replace(wsrgx, '');
return new RegExp(txt2, 'm');
}
var AnsiUp = (function () {
function AnsiUp() {
this.VERSION = "2.0.0";
this.ansi_colors = [
[
{ rgb: [0, 0, 0], class_name: "ansi-black" },
{ rgb: [187, 0, 0], class_name: "ansi-red" },
{ rgb: [0, 187, 0], class_name: "ansi-green" },
{ rgb: [187, 187, 0], class_name: "ansi-yellow" },
{ rgb: [0, 0, 187], class_name: "ansi-blue" },
{ rgb: [187, 0, 187], class_name: "ansi-magenta" },
{ rgb: [0, 187, 187], class_name: "ansi-cyan" },
{ rgb: [255, 255, 255], class_name: "ansi-white" }
],
[
{ rgb: [85, 85, 85], class_name: "ansi-bright-black" },
{ rgb: [255, 85, 85], class_name: "ansi-bright-red" },
{ rgb: [0, 255, 0], class_name: "ansi-bright-green" },
{ rgb: [255, 255, 85], class_name: "ansi-bright-yellow" },
{ rgb: [85, 85, 255], class_name: "ansi-bright-blue" },
{ rgb: [255, 85, 255], class_name: "ansi-bright-magenta" },
{ rgb: [85, 255, 255], class_name: "ansi-bright-cyan" },
{ rgb: [255, 255, 255], class_name: "ansi-bright-white" }
]
];
this.htmlFormatter = {
transform: function (fragment, instance) {
var txt = fragment.text;
if (txt.length === 0)
return txt;
if (instance._escape_for_html)
txt = instance.old_escape_for_html(txt);
if (!fragment.bright && fragment.fg === null && fragment.bg === null)
return txt;
var styles = [];
var classes = [];
var fg = fragment.fg;
var bg = fragment.bg;
if (fg === null && fragment.bright)
fg = instance.ansi_colors[1][7];
if (!instance._use_classes) {
if (fg)
styles.push("color:rgb(" + fg.rgb.join(',') + ")");
if (bg)
styles.push("background-color:rgb(" + bg.rgb + ")");
}
else {
if (fg) {
if (fg.class_name !== 'truecolor') {
classes.push(fg.class_name + "-fg");
}
else {
styles.push("color:rgb(" + fg.rgb.join(',') + ")");
}
}
if (bg) {
if (bg.class_name !== 'truecolor') {
classes.push(bg.class_name + "-bg");
}
else {
styles.push("background-color:rgb(" + bg.rgb.join(',') + ")");
}
}
}
var class_string = '';
var style_string = '';
if (classes.length)
class_string = " class=\"" + classes.join(' ') + "\"";
if (styles.length)
style_string = " style=\"" + styles.join(';') + "\"";
return "<span" + class_string + style_string + ">" + txt + "</span>";
},
compose: function (segments, instance) {
return segments.join("");
}
};
this.textFormatter = {
transform: function (fragment, instance) {
return fragment.text;
},
compose: function (segments, instance) {
return segments.join("");
}
};
this.setup_256_palette();
this._use_classes = false;
this._escape_for_html = true;
this.bright = false;
this.fg = this.bg = null;
this._buffer = '';
}
Object.defineProperty(AnsiUp.prototype, "use_classes", {
get: function () {
return this._use_classes;
},
set: function (arg) {
this._use_classes = arg;
},
enumerable: true,
configurable: true
});
Object.defineProperty(AnsiUp.prototype, "escape_for_html", {
get: function () {
return this._escape_for_html;
},
set: function (arg) {
this._escape_for_html = arg;
},
enumerable: true,
configurable: true
});
AnsiUp.prototype.setup_256_palette = function () {
var _this = this;
this.palette_256 = [];
this.ansi_colors.forEach(function (palette) {
palette.forEach(function (rec) {
_this.palette_256.push(rec);
});
});
var levels = [0, 95, 135, 175, 215, 255];
var format = function (r, g, b) { return levels[r] + ', ' + levels[g] + ', ' + levels[b] };
var r, g, b;
for (r = 0; r < 6; ++r) {
for (g = 0; g < 6; ++g) {
for (b = 0; b < 6; ++b) {
PALETTE_COLORS.push(format.call(this, r, g, b));
for (var r = 0; r < 6; ++r) {
for (var g = 0; g < 6; ++g) {
for (var b = 0; b < 6; ++b) {
var col = { rgb: [levels[r], levels[g], levels[b]], class_name: 'truecolor' };
this.palette_256.push(col);
}
}
}
}
})();
// Index 232..255 : Grayscale
(function() {
var level = 8;
var format = function(level) { return level + ', ' + level + ', ' + level };
var i;
for (i = 0; i < 24; ++i, level += 10) {
PALETTE_COLORS.push(format.call(this, level));
var grey_level = 8;
for (var i = 0; i < 24; ++i, grey_level += 10) {
var gry = { rgb: [grey_level, grey_level, grey_level], class_name: 'truecolor' };
this.palette_256.push(gry);
}
})();
};
Ansi_Up.prototype.escape_for_html = function (txt) {
return txt.replace(/[&<>]/gm, function(str) {
if (str == "&") return "&amp;";
if (str == "<") return "&lt;";
if (str == ">") return "&gt;";
});
AnsiUp.prototype.old_escape_for_html = function (txt) {
return txt.replace(/[&<>]/gm, function (str) {
if (str === "&")
return "&amp;";
if (str === "<")
return "&lt;";
if (str === ">")
return "&gt;";
});
};
Ansi_Up.prototype.linkify = function (txt) {
return txt.replace(/(https?:\/\/[^\s]+)/gm, function(str) {
return "<a href=\"" + str + "\">" + str + "</a>";
});
AnsiUp.prototype.old_linkify = function (txt) {
return txt.replace(/(https?:\/\/[^\s]+)/gm, function (str) {
return "<a href=\"" + str + "\">" + str + "</a>";
});
};
Ansi_Up.prototype.ansi_to_html = function (txt, options) {
return this.process(txt, options, true);
AnsiUp.prototype.detect_incomplete_ansi = function (txt) {
return !(/.*?[\x40-\x7e]/.test(txt));
};
Ansi_Up.prototype.ansi_to_text = function (txt) {
var options = {};
return this.process(txt, options, false);
AnsiUp.prototype.detect_incomplete_link = function (txt) {
var found = false;
for (var i = txt.length - 1; i > 0; i--) {
if (/\s|\x1B/.test(txt[i])) {
found = true;
break;
}
}
if (!found) {
if (/(https?:\/\/[^\s]+)/.test(txt))
return 0;
else
return -1;
}
var prefix = txt.substr(i + 1, 4);
if (prefix.length === 0)
return -1;
if ("http".indexOf(prefix) === 0)
return (i + 1);
};
Ansi_Up.prototype.process = function (txt, options, markup) {
var self = this;
var raw_text_chunks = txt.split(/\033\[/);
var first_chunk = raw_text_chunks.shift(); // the first chunk is not the result of the split
var color_chunks = raw_text_chunks.map(function (chunk) {
return self.process_chunk(chunk, options, markup);
});
color_chunks.unshift(first_chunk);
return color_chunks.join('');
AnsiUp.prototype.ansi_to = function (txt, formatter) {
var pkt = this._buffer + txt;
this._buffer = '';
var raw_text_pkts = pkt.split(/\x1B\[/);
if (raw_text_pkts.length === 1)
raw_text_pkts.push('');
this.handle_incomplete_sequences(raw_text_pkts);
var first_chunk = this.with_state(raw_text_pkts.shift());
var blocks = new Array(raw_text_pkts.length);
for (var i = 0, len = raw_text_pkts.length; i < len; ++i) {
blocks[i] = (formatter.transform(this.process_ansi(raw_text_pkts[i]), this));
}
if (first_chunk.text.length > 0)
blocks.unshift(formatter.transform(first_chunk, this));
return formatter.compose(blocks, this);
};
Ansi_Up.prototype.process_chunk = function (text, options, markup) {
// Are we using classes or styles?
options = typeof options == 'undefined' ? {} : options;
var use_classes = typeof options.use_classes != 'undefined' && options.use_classes;
var key = use_classes ? 'class' : 'color';
// Each 'chunk' is the text after the CSI (ESC + '[') and before the next CSI/EOF.
//
// This regex matches four groups within a chunk.
//
// The first and third groups match code type.
// We supported only SGR command. It has empty first group and 'm' in third.
//
// The second group matches all of the number+semicolon command sequences
// before the 'm' (or other trailing) character.
// These are the graphics or SGR commands.
//
// The last group is the text (including newlines) that is colored by
// the other group's commands.
var matches = text.match(/^([!\x3c-\x3f]*)([\d;]*)([\x20-\x2c]*[\x40-\x7e])([\s\S]*)/m);
if (!matches) return text;
var orig_txt = matches[4];
var nums = matches[2].split(';');
// We currently support only "SGR" (Select Graphic Rendition)
// Simply ignore if not a SGR command.
if (matches[1] !== '' || matches[3] !== 'm') {
return orig_txt;
}
if (!markup) {
return orig_txt;
}
var self = this;
while (nums.length > 0) {
var num_str = nums.shift();
var num = parseInt(num_str);
if (isNaN(num) || num === 0) {
self.fg = self.bg = null;
self.bright = 0;
} else if (num === 1) {
self.bright = 1;
} else if (num == 39) {
self.fg = null;
} else if (num == 49) {
self.bg = null;
} else if ((num >= 30) && (num < 38)) {
self.fg = ANSI_COLORS[self.bright][(num % 10)][key];
} else if ((num >= 90) && (num < 98)) {
self.fg = ANSI_COLORS[1][(num % 10)][key];
} else if ((num >= 40) && (num < 48)) {
self.bg = ANSI_COLORS[0][(num % 10)][key];
} else if ((num >= 100) && (num < 108)) {
self.bg = ANSI_COLORS[1][(num % 10)][key];
} else if (num === 38 || num === 48) { // extend color (38=fg, 48=bg)
(function() {
var is_foreground = (num === 38);
if (nums.length >= 1) {
var mode = nums.shift();
if (mode === '5' && nums.length >= 1) { // palette color
var palette_index = parseInt(nums.shift());
if (palette_index >= 0 && palette_index <= 255) {
if (!use_classes) {
if (!PALETTE_COLORS) {
self.setup_palette.call(self);
}
if (is_foreground) {
self.fg = PALETTE_COLORS[palette_index];
} else {
self.bg = PALETTE_COLORS[palette_index];
}
} else {
var klass = (palette_index >= 16)
? ('ansi-palette-' + palette_index)
: ANSI_COLORS[palette_index > 7 ? 1 : 0][palette_index % 8]['class'];
if (is_foreground) {
self.fg = klass;
} else {
self.bg = klass;
}
}
}
} else if(mode === '2' && nums.length >= 3) { // true color
var r = parseInt(nums.shift());
var g = parseInt(nums.shift());
var b = parseInt(nums.shift());
if ((r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255)) {
var color = r + ', ' + g + ', ' + b;
if (!use_classes) {
if (is_foreground) {
self.fg = color;
} else {
self.bg = color;
}
} else {
if (is_foreground) {
self.fg = 'ansi-truecolor';
self.fg_truecolor = color;
} else {
self.bg = 'ansi-truecolor';
self.bg_truecolor = color;
}
}
}
}
}
})();
AnsiUp.prototype.ansi_to_html = function (txt) {
return this.ansi_to(txt, this.htmlFormatter);
};
AnsiUp.prototype.ansi_to_text = function (txt) {
return this.ansi_to(txt, this.textFormatter);
};
AnsiUp.prototype.with_state = function (text) {
return { bright: this.bright, fg: this.fg, bg: this.bg, text: text };
};
AnsiUp.prototype.handle_incomplete_sequences = function (chunks) {
var last_chunk = chunks[chunks.length - 1];
if ((last_chunk.length > 0) && this.detect_incomplete_ansi(last_chunk)) {
this._buffer = "\x1B[" + last_chunk;
chunks.pop();
chunks.push('');
}
}
if ((self.fg === null) && (self.bg === null)) {
return orig_txt;
} else {
var styles = [];
var classes = [];
var data = {};
var render_data = function (data) {
var fragments = [];
var key;
for (key in data) {
if (data.hasOwnProperty(key)) {
fragments.push('data-' + key + '="' + this.escape_for_html(data[key]) + '"');
else {
if (last_chunk.slice(-1) === "\x1B") {
this._buffer = "\x1B";
console.log("raw", chunks);
chunks.pop();
chunks.push(last_chunk.substr(0, last_chunk.length - 1));
console.log(chunks);
console.log(last_chunk);
}
}
return fragments.length > 0 ? ' ' + fragments.join(' ') : '';
};
if (self.fg) {
if (use_classes) {
classes.push(self.fg + "-fg");
if (self.fg_truecolor !== null) {
data['ansi-truecolor-fg'] = self.fg_truecolor;
self.fg_truecolor = null;
if (chunks.length === 2 &&
chunks[1] === "" &&
chunks[0].slice(-1) === "\x1B") {
this._buffer = "\x1B";
last_chunk = chunks.shift();
chunks.unshift(last_chunk.substr(0, last_chunk.length - 1));
}
} else {
styles.push("color:rgb(" + self.fg + ")");
}
}
if (self.bg) {
if (use_classes) {
classes.push(self.bg + "-bg");
if (self.bg_truecolor !== null) {
data['ansi-truecolor-bg'] = self.bg_truecolor;
self.bg_truecolor = null;
};
AnsiUp.prototype.process_ansi = function (block) {
if (!this._sgr_regex) {
this._sgr_regex = (_a = ["\n ^ # beginning of line\n ([!<-?]?) # a private-mode char (!, <, =, >, ?)\n ([d;]*) # any digits or semicolons\n ([ -/]? # an intermediate modifier\n [@-~]) # the command\n ([sS]*) # any text following this CSI sequence\n "], _a.raw = ["\n ^ # beginning of line\n ([!\\x3c-\\x3f]?) # a private-mode char (!, <, =, >, ?)\n ([\\d;]*) # any digits or semicolons\n ([\\x20-\\x2f]? # an intermediate modifier\n [\\x40-\\x7e]) # the command\n ([\\s\\S]*) # any text following this CSI sequence\n "], rgx(_a));
}
var matches = block.match(this._sgr_regex);
if (!matches) {
return this.with_state(block);
}
var orig_txt = matches[4];
if (matches[1] !== '' || matches[3] !== 'm') {
return this.with_state(orig_txt);
}
var sgr_cmds = matches[2].split(';');
while (sgr_cmds.length > 0) {
var sgr_cmd_str = sgr_cmds.shift();
var num = parseInt(sgr_cmd_str, 10);
if (isNaN(num) || num === 0) {
this.fg = this.bg = null;
this.bright = false;
}
} else {
styles.push("background-color:rgb(" + self.bg + ")");
}
else if (num === 1) {
this.bright = true;
}
else if (num === 39) {
this.fg = null;
}
else if (num === 49) {
this.bg = null;
}
else if ((num >= 30) && (num < 38)) {
var bidx = this.bright ? 1 : 0;
this.fg = this.ansi_colors[bidx][(num - 30)];
}
else if ((num >= 90) && (num < 98)) {
this.fg = this.ansi_colors[1][(num - 90)];
}
else if ((num >= 40) && (num < 48)) {
this.bg = this.ansi_colors[0][(num - 40)];
}
else if ((num >= 100) && (num < 108)) {
this.bg = this.ansi_colors[1][(num - 100)];
}
else if (num === 38 || num === 48) {
if (sgr_cmds.length > 0) {
var is_foreground = (num === 38);
var mode_cmd = sgr_cmds.shift();
if (mode_cmd === '5' && sgr_cmds.length > 0) {
var palette_index = parseInt(sgr_cmds.shift(), 10);
if (palette_index >= 0 && palette_index <= 255) {
if (is_foreground)
this.fg = this.palette_256[palette_index];
else
this.bg = this.palette_256[palette_index];
}
}
if (mode_cmd === '2' && sgr_cmds.length > 2) {
var r = parseInt(sgr_cmds.shift(), 10);
var g = parseInt(sgr_cmds.shift(), 10);
var b = parseInt(sgr_cmds.shift(), 10);
if ((r >= 0 && r <= 255) && (g >= 0 && g <= 255) && (b >= 0 && b <= 255)) {
var c = { rgb: [r, g, b], class_name: 'truecolor' };
if (is_foreground)
this.fg = c;
else
this.bg = c;
}
}
}
}
}
if (use_classes) {
return '<span class="' + classes.join(' ') + '"' + render_data.call(self, data) + '>' + orig_txt + '</span>';
} else {
return '<span style="' + styles.join(';') + '"' + render_data.call(self, data) + '>' + orig_txt + '</span>';
}
}
return this.with_state(orig_txt);
var _a;
};
// Module exports
ansi_up = {
escape_for_html: function (txt) {
var a2h = new Ansi_Up();
return a2h.escape_for_html(txt);
},
linkify: function (txt) {
var a2h = new Ansi_Up();
return a2h.linkify(txt);
},
ansi_to_html: function (txt, options) {
var a2h = new Ansi_Up();
return a2h.ansi_to_html(txt, options);
},
ansi_to_text: function (txt) {
var a2h = new Ansi_Up();
return a2h.ansi_to_text(txt);
},
ansi_to_html_obj: function () {
return new Ansi_Up();
}
};
// CommonJS module is defined
if (hasModule) {
module.exports = ansi_up;
}
/*global ender:false */
if (typeof window !== 'undefined' && typeof ender === 'undefined') {
window.ansi_up = ansi_up;
}
/*global define:false */
if (typeof define === "function" && define.amd) {
define("ansi_up", [], function () {
return ansi_up;
});
}
})(Date);
return AnsiUp;
}());
//# sourceMappingURL=ansi_up.js.map
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = AnsiUp;
});

@@ -1,3 +0,6 @@

require(['ansi_up',"/examples/jquery-1.7.2.min.js"], function(a2h) {
// For browser_amd.html
require(['ansi_up',"jquery-1.7.2.min.js"], function(au) {
var a2h = new au.default;
var txt = "\n\n\033[1;33;40m 33;40 \033[1;33;41m 33;41 \033[1;33;42m 33;42 \033[1;33;43m 33;43 \033[1;33;44m 33;44 \033[1;33;45m 33;45 \033[1;33;46m 33;46 \033[1m\033[0\n\n\033[1;33;42m >> Tests OK\n\n"

@@ -4,0 +7,0 @@

{
"name": "ansi_up",
"version": "1.3.0",
"description": "Convert ansi sequences in strings to colorful HTML",
"keywords": ["ansi", "html"],
"author": "drudru <drudru@gmail.com>",
"main": "./ansi_up.js",
"repository" :
{
"type" : "git",
"url" : "git://github.com/drudru/ansi_up.git"
},
"bugs" :
{
"url" : "http://github.com/drudru/ansi_up/issues"
},
"engines":
{
"node": "*"
},
"scripts":
{
"test": "make test",
"build": "make build"
},
"devDependencies":
{
"mocha": "*",
"should": "*",
"jshint": "*",
"jslint": "*"
}
"name": "ansi_up",
"version": "2.0.0",
"description": "Convert ansi sequences in strings to colorful HTML",
"keywords": [
"ansi",
"html"
],
"author": "drudru <drudru@gmail.com>",
"license": "MIT",
"main": "./ansi_up.js",
"repository": {
"type": "git",
"url": "git://github.com/drudru/ansi_up.git"
},
"bugs": {
"url": "http://github.com/drudru/ansi_up/issues"
},
"engines": {
"node": "*"
},
"scripts": {
"test": "make test",
"build": "make typescript"
},
"devDependencies": {
"mocha": "*",
"should": "*",
"typescript": "^2.1"
}
}
# ansi_up.js
__ansi_up__ is a simple library for converting text that contains [ANSI color escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) into equivalent HTML spans.
At the same, it also properly escapes HTML unsafe characters (&,<,>,etc.) into their proper HTML representation. It can also transform any text that looks like a URL into an HTML anchor tag.
__ansi_up__ is a simple, easy to use library that provides a streaming API to
transform text containing
[ANSI color escape codes](http://en.wikipedia.org/wiki/ANSI_escape_code#Colors) into proper HTML.
It can also transform any text that looks like a URL into an HTML anchor tag.
This is compliant with AMD (require.js). This code has been used in production since early 2012. This project is actively maintained and welcomes all feedback. Thanks for reading.
This module is a single Javascript file with no dependencies. It is a UMD style module so it
can be utilized in a browser, in node.js (CommonJS), or with AMD (require.js). The source code
was compiled from TypeScript and its type description ships with the NPM. This code has been used in production since 2011 and is actively maintained.
Turn this terminal output:
For example, turn this terminal output:

@@ -16,6 +20,7 @@ ESC[1;Foreground

Into this browser output:
...into this browser output:
![](https://raw.github.com/drudru/ansi_up/master/sample.png)
## Browser Example

@@ -29,2 +34,4 @@

var ansi_up = new AnsiUp;
var html = ansi_up.ansi_to_html(txt);

@@ -42,3 +49,4 @@

```JavaScript
var ansi_up = require('ansi_up');
var AU = require('ansi_up');
var ansi_up = new AU.default;

@@ -50,3 +58,3 @@ var txt = "\n\n\033[1;33;40m 33;40 \033[1;33;41m 33;41 \033[1;33;42m 33;42 \033[1;33;43m 33;43 \033[1;33;44m 33;44 \033[1;33;45m 33;45 \033[1;33;46m 33;46 \033[1m\033[0\n\n\033[1;33;42m >> Tests OK\n\n"

There are examples in the repo that demonstrate an AMD/require.js/ jQuery example as well as a simple browser example.
More examples are in the 'examples' directory in the repo.

@@ -57,8 +65,22 @@ ## Installation

## API
## Versions
_ansi_up_ should be called via the functions defined on the module. It is recommended that the HTML is rendered with a monospace font and black background. See the examples, for a basic theme as a CSS definition.
Version 2.x is the latest stateful, streaming version of the API. It is simpler and more correct.
Version 1.3.0 was the last of the older, deprecated API.
#### ansi_to_html (txt, options)
## Quick Start
1. Use whatever module system to import the _ansi_up_ module.
2. Instantiate the object.
3. For every piece of input that arrives, call **ansi_to_html**.
4. Append the emitted HTML to the previous HTML already emitted.
## API Methods
In order to use _ansi_up_, you must Instantiate an object using your given module
system.
#### ansi_to_html (txt)
This replaces ANSI terminal escape codes with SPAN tags that wrap the content. See the example output above.

@@ -70,19 +92,98 @@

#### escape_for_html (txt)
#### ansi_to_text (txt)
This does the minimum escaping of text to make it compliant with HTML. In particular, the '&','<', and '>' characters are escaped. This should be run prior to ansi_to_html.
This simply removes the ANSI escape codes from the stream.
No escaping is done.
#### linkify (txt)
#### linkify(txt)
This replaces any links in the text with anchor tags that display the link. The links should have at least one whitespace character surrounding it. Also, you should apply this after you have run ansi_to_html on the text.
This replaces any links in the text with anchor tags that display the link.
Only strings starting with 'http' or 'https', and surrounded by whitespace are
considered valid patterns.
You should only call this method if you can guarantee that the full URL
will be passed into ansi_to_html(). If the URL is split along a buffer
boundary, then the wrong URL will be 'linkified'.
## Properties
#### escape_for_html
(default: true)
This does the minimum escaping of text to make it compliant with HTML.
In particular, the '&','<', and '>' characters are escaped.
#### use_classes
(default: false)
This causes the SPAN tags to use class names for the color style instead
of specified RGB values.
## API Overview
On a high level, _ansi_up_ takes a stream of text and transforms it proper HTML with colors.
It does this by buffering the data and performing multiple passes over the
stream. Each time it consumes data, it may or may not emit HTML. This HTML will always be
proper HTML.
Because this process requires buffering (ie. stateful), you must insantiate an _ansi_up_ object
in order to begin. Also, text may be received later that is styled by a previous.
The first pass converts characters that are unsafe for HTML into their equivalents. It will only
convert '&', '<', and '>' characters. This pass is optional, and is on by default.
The second pass converts any ANSI color sequences to HTML spans. It does this by recognizing
what is termed as ANSI **SGR** codes. All ANSI sequences (SGR and non-SGR) are removed from the
output stream. The SGR codes create HTML **SPAN** tags to surround text that is styled by those
codes. If the ANSI sequence is incomplete, it will be held in _ansi_up_'s internal buffer
until new data is received to complete it.
The third and final pass transforms URLs to HTML anchors. This will also buffer output until a non URL
character is received. This pass is optional, and is off by default.
### Recommended Style of Use
There are two ways to stream this data to a web page. A push model or a pull model.
I have personally used a pull model to 'tail' a file.
In my 'pull' model, I had a process generating a log file on a remote machine.
I had a web server running on the same machine. I developed a simple page
that used AJAX to poll the web server periodically. Specifically I used an
HTTP/1.1 GET request with RFC 7233 Range query. The server would return
either range response.
I would then process each chunk received with _ansi_up_, and append the new
spans to the innerHTML of a PRE tag.
### UTF8 note
One last important note, _ansi_up_ takes its input in the form of a Javascript string.
These strings are UTF8. When you take the output of some program and send it to
Javascript, there will be buffering. Be sure to not send incomplete UTF8 sequences or
Javascript will ignore or drop the sequence from the stream when it converts it to a
string.
_ansi_up_ should be called via the functions defined on the module. It is recommended that the HTML is rendered with a monospace font and black background. See the examples, for a basic theme as a CSS definition.
At the same, it also properly escapes HTML unsafe characters (&,<,>,etc.) into their proper HTML representation.
## Building
This just uses 'make'. The result is just one file. Feel free to include the file in your asset minification process.
To build, a simple Makefile handles it all.
```shell
$ make
```
## Running tests
To run the tests for _ansi_up_, run `npm install` to install dependencies and then:
To run the tests for _ansi_up_, run `npm install` to install dev dependencies. Then:
```shell
$ make test
```

@@ -124,2 +225,3 @@ ## Credits

CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WIT
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

@@ -1,40 +0,45 @@

var ansi_up = require('../ansi_up');
var AU = require('../ansi_up');
var AnsiUp = AU.default;
var should = require('should');
describe('ansi_up', function() {
describe('ansi_up', function () {
describe('escape_for_html', function() {
describe('escape_for_html on', function () {
describe('ampersands', function() {
describe('ampersands', function () {
it('should escape a single ampersand', function() {
it('should escape a single ampersand', function () {
var start = "&";
var expected = "&amp;";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape some text with ampersands', function() {
it('should escape some text with ampersands', function () {
var start = "abcd&efgh";
var expected = "abcd&amp;efgh";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape multiple ampersands', function() {
it('should escape multiple ampersands', function () {
var start = " & & ";
var expected = " &amp; &amp; ";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape an already escaped ampersand', function() {
it('should escape an already escaped ampersand', function () {
var start = " &amp; ";
var expected = " &amp;amp; ";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -44,25 +49,28 @@ });

describe('less-than', function() {
describe('less-than', function () {
it('should escape a single less-than', function() {
it('should escape a single less-than', function () {
var start = "<";
var expected = "&lt;";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape some text with less-thans', function() {
it('should escape some text with less-thans', function () {
var start = "abcd<efgh";
var expected = "abcd&lt;efgh";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape multiple less-thans', function() {
it('should escape multiple less-thans', function () {
var start = " < < ";
var expected = " &lt; &lt; ";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -73,25 +81,28 @@ });

describe('greater-than', function() {
describe('greater-than', function () {
it('should escape a single greater-than', function() {
it('should escape a single greater-than', function () {
var start = ">";
var expected = "&gt;";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape some text with greater-thans', function() {
it('should escape some text with greater-thans', function () {
var start = "abcd>efgh";
var expected = "abcd&gt;efgh";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape multiple greater-thans', function() {
it('should escape multiple greater-thans', function () {
var start = " > > ";
var expected = " &gt; &gt; ";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -102,9 +113,10 @@ });

describe('mixed characters', function() {
describe('mixed characters', function () {
it('should escape a mix of characters that require escaping', function() {
it('should escape a mix of characters that require escaping', function () {
var start = "<&>/\\'\"";
var expected = "&lt;&amp;&gt;/\\'\"";
var l = ansi_up.escape_for_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -117,18 +129,228 @@ });

describe('linkify', function() {
describe('escape_for_html off', function () {
it('should linkify a url', function() {
var start = "http://link.to/me";
var expected = "<a href=\"http://link.to/me\">http://link.to/me</a>";
describe('ampersands', function () {
var l = ansi_up.linkify(start);
it('should escape a single ampersand', function () {
var start = "&";
var expected = "&";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape some text with ampersands', function () {
var start = "abcd&efgh";
var expected = "abcd&efgh";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape multiple ampersands', function () {
var start = " & & ";
var expected = " & & ";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape an already escaped ampersand', function () {
var start = " &amp; ";
var expected = " &amp; ";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
});
describe('less-than', function () {
it('should escape a single less-than', function () {
var start = "<";
var expected = "<";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape some text with less-thans', function () {
var start = "abcd<efgh";
var expected = "abcd<efgh";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape multiple less-thans', function () {
var start = " < < ";
var expected = " < < ";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
});
describe('greater-than', function () {
it('should escape a single greater-than', function () {
var start = ">";
var expected = ">";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape some text with greater-thans', function () {
var start = "abcd>efgh";
var expected = "abcd>efgh";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should escape multiple greater-thans', function () {
var start = " > > ";
var expected = " > > ";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
});
describe('mixed characters', function () {
it('should escape a mix of characters that require escaping', function () {
var start = "<&>/\\'\"";
var expected = "<&>/\\'\"";
var au = new AnsiUp();
au.escape_for_html = false;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
});
});
describe('ansi to html', function() {
/* Too difficult
describe('default colors', function() {
it('should transform a foreground to html', function() {
describe('linkify', function () {
it('should linkify a url', function () {
var start = "http://link.to/me ";
var expected = "<a href=\"http://link.to/me\">http://link.to/me</a> ";
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should linkify a url split into two chunks', function () {
var start1 = " https://link.to/m";
var start2 = "e ";
var expected = "<a href=\"https://link.to/me\">https://link.to/me</a> ";
var au = new AnsiUp();
var l = au.ansi_to_html(start1);
l.should.eql("");
l = au.ansi_to_html(start2);
l.should.eql(expected);
});
it('should linkify a url split into two chunks with color state', function () {
var start1 = "\033[32mhttps://link.to/m";
var start2 = "e ";
var expected = "<span style=\"color:rgb(0,187,0)\"><a href=\"https://link.to/me\">https://link.to/me</a> </span>";
var au = new AnsiUp();
var l = au.ansi_to_html(start1);
l.should.eql("");
l = au.ansi_to_html(start2);
l.should.eql(expected);
});
});
*/
describe("ansi_to()", function() {
// Prove that interaction between AnsiUp and the formatter is correct and that formatters
// can be completely isolated code.
it("accepts an arbitrary formatter and provides ANSI information related to text segments", function() {
var attr = 1; // bright
var fg = 32; // green fg
var bg = 41; // red bg
var lines = [
"should have no color",
"\033[" + attr + ";" + fg + "m " + "should be bright green foreground" + "\033[0m",
"\033[" + attr + ";" + bg + ";" + fg + "m " + "should have bright red background with bright green foreground" + "\033[0m"
];
var stats = {};
// A silly formatter that collects statistics about the text it receives.
var statsFormatter = {
transform: function(data) {
var text = data.text.replace(/^\s+|\s+$/, "");
if (text.length) {
if (!stats[text]) {
stats[text] = [];
}
if (data.fg) stats[text].push(data.fg.class_name);
if (data.bg) stats[text].push(data.bg.class_name);
}
return text;
},
compose: function(segments) {
return "processed: " + segments.filter(function (s) { return s.length; }).join(", ");
}
};
var au = new AnsiUp();
au.use_classes = true;
var plainText = au.ansi_to(lines.join(""), statsFormatter);
plainText.should.eql("processed: should have no color, should be bright green foreground, should have bright red background with bright green foreground");
stats.should.eql({
"should have no color": [],
"should be bright green foreground": ["ansi-bright-green"],
"should have bright red background with bright green foreground": ["ansi-bright-green", "ansi-red"]
});
});
});
describe('ansi to html', function () {
describe('default colors', function () {
it('should transform a foreground to html', function () {
var attr = 0;

@@ -138,5 +360,6 @@ var fg = 32;

var expected = "<span style=\"color:rgb(0, 187, 0)\"> " + fg + " </span>";
var expected = "<span style=\"color:rgb(0,187,0)\"> " + fg + " </span>";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -146,3 +369,3 @@ });

it('should transform a attr;foreground to html', function() {
it('should transform a attr;foreground to html', function () {
var attr = 0;

@@ -152,9 +375,10 @@ var fg = 32;

var expected = "<span style=\"color:rgb(0, 187, 0)\"> " + fg + " </span>";
var expected = "<span style=\"color:rgb(0,187,0)\"> " + fg + " </span>";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform an empty code to a normal/reset html', function() {
it('should transform an empty code to a normal/reset html', function () {
var attr = 0;

@@ -164,9 +388,10 @@ var fg = 32;

var expected = "<span style=\"color:rgb(0, 187, 0)\"> " + fg + " </span> x";
var expected = "<span style=\"color:rgb(0,187,0)\"> " + fg + " </span> x";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a bold attr;foreground to html', function() {
it('should transform a bold attr;foreground to html', function () {
var attr = 1;

@@ -176,19 +401,21 @@ var fg = 32;

var expected = "<span style=\"color:rgb(0, 255, 0)\"> " + attr + ";" + fg + " </span>";
var expected = "<span style=\"color:rgb(0,255,0)\"> " + attr + ";" + fg + " </span>";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a bold-foreground to html', function() {
it('should transform a bold-foreground to html', function () {
var fg = 92;
var start = "\033[" + fg + "m " + fg + " \033[0m";
var expected = "<span style=\"color:rgb(0, 255, 0)\"> " + fg + " </span>";
var expected = "<span style=\"color:rgb(0,255,0)\"> " + fg + " </span>";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a bold attr;background;foreground to html', function() {
it('should transform a bold attr;background;foreground to html', function () {
var attr = 1;

@@ -199,9 +426,10 @@ var fg = 33;

var expected = "<span style=\"color:rgb(255, 255, 85);background-color:rgb(0, 187, 0)\"> " + attr + ";" + bg + ";" + fg + " </span>";
var expected = "<span style=\"color:rgb(255,255,85);background-color:rgb(0,187,0)\"> " + attr + ";" + bg + ";" + fg + " </span>";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a bold-background;foreground to html', function() {
it('should transform a bold-background;foreground to html', function () {
var fg = 33;

@@ -211,5 +439,6 @@ var bg = 102;

var expected = "<span style=\"color:rgb(187, 187, 0);background-color:rgb(0, 255, 0)\"> " + bg + ";" + fg + " </span>";
var expected = "<span style=\"color:rgb(187,187,0);background-color:rgb(0,255,0)\"> " + bg + ";" + fg + " </span>";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -219,3 +448,3 @@ });

it('should transform a complex multi-line sequence to html', function() {
it('should transform a complex multi-line sequence to html', function () {
var attr = 1;

@@ -226,9 +455,10 @@ var fg = 32;

var expected = "\n <span style=\"color:rgb(0, 187, 0)\"> " + fg + " </span> \n <span style=\"background-color:rgb(0, 187, 0)\"> " + bg + " </span> \n zimpper ";
var expected = "\n <span style=\"color:rgb(0,187,0)\"> " + fg + " </span> \n <span style=\"background-color:rgb(0,187,0)\"> " + bg + " </span> \n zimpper ";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a foreground and background and reset foreground to html', function() {
it('should transform a foreground and background and reset foreground to html', function () {
var fg = 37;

@@ -238,9 +468,10 @@ var bg = 42;

var expected = "\n<span style=\"background-color:rgb(0, 0, 0)\"> </span><span style=\"color:rgb(255,255,255);background-color:rgb(0, 187, 0)\"> " + bg + " </span><span style=\"background-color:rgb(0, 187, 0)\"> foobar </span>";
var expected = "\n<span style=\"background-color:rgb(0,0,0)\"> </span><span style=\"color:rgb(255,255,255);background-color:rgb(0,187,0)\"> " + bg + " </span><span style=\"background-color:rgb(0,187,0)\"> foobar </span>";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a foreground and background and reset background to html', function() {
it('should transform a foreground and background and reset background to html', function () {
var fg = 37;

@@ -250,9 +481,10 @@ var bg = 42;

var expected = "\n<span style=\"background-color:rgb(0, 0, 0)\"> </span><span style=\"color:rgb(255,255,255);background-color:rgb(0, 187, 0)\"> " + fg + " </span><span style=\"color:rgb(255,255,255)\"> foobar </span>";
var expected = "\n<span style=\"background-color:rgb(0,0,0)\"> </span><span style=\"color:rgb(255,255,255);background-color:rgb(0,187,0)\"> " + fg + " </span><span style=\"color:rgb(255,255,255)\"> foobar </span>";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a foreground and background and reset them to html', function() {
it('should transform a foreground and background and reset them to html', function () {
var fg = 37;

@@ -262,62 +494,71 @@ var bg = 42;

var expected = "\n<span style=\"background-color:rgb(0, 0, 0)\"> </span><span style=\"color:rgb(255,255,255);background-color:rgb(0, 187, 0)\"> " + fg + ';' + bg + " </span> foobar ";
var expected = "\n<span style=\"background-color:rgb(0,0,0)\"> </span><span style=\"color:rgb(255,255,255);background-color:rgb(0,187,0)\"> " + fg + ';' + bg + " </span> foobar ";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
describe('transform extend colors (palette)', function() {
it('system color, foreground', function() {
describe('transform extended colors (palette)', function () {
it('system color, foreground', function () {
var start = "\033[38;5;1m" + "red" + "\033[0m";
var expected = '<span style="color:rgb(187, 0, 0)">red</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="color:rgb(187,0,0)">red</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('system color, foreground (bright)', function() {
it('system color, foreground (bright)', function () {
var start = "\033[38;5;9m" + "red" + "\033[0m";
var expected = '<span style="color:rgb(255, 85, 85)">red</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="color:rgb(255,85,85)">red</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('system color, background', function() {
it('system color, background', function () {
var start = "\033[48;5;1m" + "red" + "\033[0m";
var expected = '<span style="background-color:rgb(187, 0, 0)">red</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="background-color:rgb(187,0,0)">red</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('system color, background (bright)', function() {
it('system color, background (bright)', function () {
var start = "\033[48;5;9m" + "red" + "\033[0m";
var expected = '<span style="background-color:rgb(255, 85, 85)">red</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="background-color:rgb(255,85,85)">red</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('palette, foreground', function() {
it('palette, foreground', function () {
var start = "\033[38;5;171m" + "foo" + "\033[0m";
var expected = '<span style="color:rgb(215, 95, 255)">foo</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="color:rgb(215,95,255)">foo</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('palette, background', function() {
it('palette, background', function () {
var start = "\033[48;5;171m" + "foo" + "\033[0m";
var expected = '<span style="background-color:rgb(215, 95, 255)">foo</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="background-color:rgb(215,95,255)">foo</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('combination of bold and palette', function() {
it('combination of bold and palette', function () {
var start = "\033[1;38;5;171m" + "foo" + "\033[0m";
var expected = '<span style="color:rgb(215, 95, 255)">foo</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="color:rgb(215,95,255)">foo</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('combination of palette and bold', function() {
it('combination of palette and bold', function () {
var start = "\033[38;5;171;1m" + "foo" + "\033[0m";
var expected = '<span style="color:rgb(215, 95, 255)">foo</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="color:rgb(215,95,255)">foo</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -327,19 +568,22 @@ });

describe('transform extend colors (true color)', function() {
it('foreground', function() {
describe('transform extended colors (true color)', function () {
it('foreground', function () {
var start = "\033[38;2;42;142;242m" + "foo" + "\033[0m";
var expected = '<span style="color:rgb(42, 142, 242)">foo</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="color:rgb(42,142,242)">foo</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('background', function() {
it('background', function () {
var start = "\033[48;2;42;142;242m" + "foo" + "\033[0m";
var expected = '<span style="background-color:rgb(42, 142, 242)">foo</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="background-color:rgb(42,142,242)">foo</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('both foreground and background', function() {
it('both foreground and background', function () {
var start = "\033[38;2;42;142;242;48;2;1;2;3m" + "foo" + "\033[0m";
var expected = '<span style="color:rgb(42, 142, 242);background-color:rgb(1, 2, 3)">foo</span>';
var l = ansi_up.ansi_to_html(start);
var expected = '<span style="color:rgb(42,142,242);background-color:rgb(1,2,3)">foo</span>';
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -350,4 +594,4 @@ });

describe('themed colors', function() {
it('should transform a foreground to html', function() {
describe('themed colors', function () {
it('should transform a foreground to html', function () {
var attr = 0;

@@ -359,8 +603,9 @@ var fg = 32;

var l = ansi_up.ansi_to_html(start, {use_classes: true});
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a attr;foreground to html', function() {
it('should transform a attr;foreground to html', function () {
var attr = 0;

@@ -372,7 +617,9 @@ var fg = 32;

var l = ansi_up.ansi_to_html(start, {use_classes: true});
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a bold attr;foreground to html', function() {
it('should transform a bold attr;foreground to html', function () {
var attr = 1;

@@ -384,7 +631,9 @@ var fg = 32;

var l = ansi_up.ansi_to_html(start, {use_classes: true});
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a bold attr;background;foreground to html', function() {
it('should transform a bold attr;background;foreground to html', function () {
var attr = 1;

@@ -397,7 +646,9 @@ var fg = 33;

var l = ansi_up.ansi_to_html(start, {use_classes: true});
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a complex multi-line sequence to html', function() {
it('should transform a complex multi-line sequence to html', function () {
var attr = 1;

@@ -410,60 +661,78 @@ var fg = 32;

var l = ansi_up.ansi_to_html(start, {use_classes: true});
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
describe('transform extend colors (palette)', function() {
it('system color, foreground', function() {
describe('transform extended colors (palette)', function () {
it('system color, foreground', function () {
var start = "\033[38;5;1m" + "red" + "\033[0m";
var expected = '<span class="ansi-red-fg">red</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('system color, foreground (bright)', function() {
it('system color, foreground (bright)', function () {
var start = "\033[38;5;9m" + "red" + "\033[0m";
var expected = '<span class="ansi-bright-red-fg">red</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('system color, background', function() {
it('system color, background', function () {
var start = "\033[48;5;1m" + "red" + "\033[0m";
var expected = '<span class="ansi-red-bg">red</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('system color, background (bright)', function() {
it('system color, background (bright)', function () {
var start = "\033[48;5;9m" + "red" + "\033[0m";
var expected = '<span class="ansi-bright-red-bg">red</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('palette, foreground', function() {
it('palette, foreground', function () {
var start = "\033[38;5;171m" + "foo" + "\033[0m";
var expected = '<span class="ansi-palette-171-fg">foo</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var expected = '<span style="color:rgb(215,95,255)">foo</span>';
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('palette, background', function() {
it('palette, background', function () {
var start = "\033[48;5;171m" + "foo" + "\033[0m";
var expected = '<span class="ansi-palette-171-bg">foo</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var expected = '<span style="background-color:rgb(215,95,255)">foo</span>';
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('combination of bold and palette', function() {
it('combination of bold and palette', function () {
var start = "\033[1;38;5;171m" + "foo" + "\033[0m";
var expected = '<span class="ansi-palette-171-fg">foo</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var expected = '<span style="color:rgb(215,95,255)">foo</span>';
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('combination of palette and bold', function() {
it('combination of palette and bold', function () {
var start = "\033[38;5;171;1m" + "foo" + "\033[0m";
var expected = '<span class="ansi-palette-171-fg">foo</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var expected = '<span style="color:rgb(215,95,255)">foo</span>';
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -473,19 +742,25 @@ });

describe('transform extend colors (true color)', function() {
it('foreground', function() {
describe('transform extended colors (true color)', function () {
it('foreground', function () {
var start = "\033[38;2;42;142;242m" + "foo" + "\033[0m";
var expected = '<span class="ansi-truecolor-fg" data-ansi-truecolor-fg="42, 142, 242">foo</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var expected = '<span style="color:rgb(42,142,242)">foo</span>';
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('background', function() {
it('background', function () {
var start = "\033[48;2;42;142;242m" + "foo" + "\033[0m";
var expected = '<span class="ansi-truecolor-bg" data-ansi-truecolor-bg="42, 142, 242">foo</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var expected = '<span style="background-color:rgb(42,142,242)">foo</span>';
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);
});
it('both foreground and background', function() {
it('both foreground and background', function () {
var start = "\033[38;2;42;142;242;48;2;1;2;3m" + "foo" + "\033[0m";
var expected = '<span class="ansi-truecolor-fg ansi-truecolor-bg" data-ansi-truecolor-fg="42, 142, 242" data-ansi-truecolor-bg="1, 2, 3">foo</span>';
var l = ansi_up.ansi_to_html(start, {use_classes: true});
var expected = '<span style="color:rgb(42,142,242);background-color:rgb(1,2,3)">foo</span>';
var au = new AnsiUp();
au.use_classes = true;
var l = au.ansi_to_html(start);
l.should.eql(expected);

@@ -495,8 +770,10 @@ });

});
describe('ignore unsupported CSI', function() {
it('should correctly convert a string similar to CSI', function() {
describe('ignore unsupported CSI', function () {
it('should correctly convert a string similar to CSI', function () {
// https://github.com/drudru/ansi_up/pull/15
// "[1;31m" is a plain text. not an escape sequence.
var start = "foo\033[1@bar[1;31mbaz\033[0m";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);

@@ -509,53 +786,99 @@ // is all plain texts exist?

});
it('(italic)', function() {
it('(italic)', function () {
var start = "foo\033[3mbar\033[0mbaz";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql('foobarbaz');
});
it('(cursor-up)', function() {
it('(cursor-up)', function () {
var start = "foo\033[1Abar";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql('foobar');
});
it('(scroll-left)', function() {
it('(scroll-left)', function () {
// <ESC>[1 @ (including ascii space)
var start = "foo\033[1 @bar";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql('foobar');
});
it('(DECMC)', function() {
it('(DECMC)', function () {
var start = "foo\033[?11ibar";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql('foobar');
});
it('(RLIMGCP)', function() {
/* I cannot find this in the XTERM specs
it('(RLIMGCP)', function () {
var start = "foo\033[<!3ibar";
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql('foobar');
});
it('(DECSCL)', function() {
*/
it('(DECSCL)', function () {
var start = "foo\033[61;0\"pbar"
var l = ansi_up.ansi_to_html(start);
var au = new AnsiUp();
var l = au.ansi_to_html(start);
l.should.eql('foobar');
});
});
describe('buffering situations', function () {
it('should transform an incomplete prefix', function () {
var attr = 0;
var fg = 32;
var start1 = "\033[" + attr + ";";
var start2 = fg + "m " + fg + " \033[0m";
var expected = "<span style=\"color:rgb(0,187,0)\"> " + fg + " </span>";
var au = new AnsiUp();
var l = au.ansi_to_html(start1);
l.should.eql("");
l = au.ansi_to_html(start2);
l.should.eql(expected);
});
it('should transform a lonely escape', function () {
var attr = 0;
var fg = 32;
var start1 = "xyz \033";
var start2 = "[" + attr + ";" + fg + "m " + fg + " \033[0m";
var expected = "<span style=\"color:rgb(0,187,0)\"> " + fg + " </span>";
var au = new AnsiUp();
var l = au.ansi_to_html(start1);
l.should.eql("xyz ");
l = au.ansi_to_html(start2);
l.should.eql(expected);
});
});
});
describe('ansi to text', function() {
it('should remove color sequence', function() {
describe('ansi to text', function () {
it('should remove color sequence', function () {
var start = "foo \033[1;32mbar\033[0m baz";
var l = ansi_up.ansi_to_text(start);
var au = new AnsiUp();
var l = au.ansi_to_text(start);
l.should.eql("foo bar baz");
});
it('should remove unsupported sequence', function() {
it('should remove unsupported sequence', function () {
var start = "foo \033[1Abar";
var l = ansi_up.ansi_to_text(start);
var au = new AnsiUp();
var l = au.ansi_to_text(start);
l.should.eql('foo bar');
});
it('should keep multiline', function() {
it('should keep multiline', function () {
var start = "foo \033[1;32mbar\nbaz\033[0m qux";
var l = ansi_up.ansi_to_text(start);
var au = new AnsiUp();
var l = au.ansi_to_text(start);
l.should.eql("foo bar\nbaz qux");
});
});
});
});

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc