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.2.1 to 1.3.0

notes.txt

161

ansi_up.js
// ansi_up.js
// version : 1.2.1
// version : 1.3.0
// author : Dru Nelson

@@ -10,3 +10,3 @@ // license : MIT

var ansi_up,
VERSION = "1.2.1",
VERSION = "1.3.0",

@@ -38,9 +38,50 @@ // check for nodeJS

]
];
],
// 256 Colors Palette
PALETTE_COLORS;
function Ansi_Up() {
this.fg = this.bg = null;
this.fg = this.bg = this.fg_truecolor = this.bg_truecolor = null;
this.bright = 0;
}
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 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));
}
}
}
})();
// 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));
}
})();
};
Ansi_Up.prototype.escape_for_html = function (txt) {

@@ -61,4 +102,12 @@ return txt.replace(/[&<>]/gm, function(str) {

Ansi_Up.prototype.ansi_to_html = function (txt, options) {
return this.process(txt, options, true);
};
Ansi_Up.prototype.ansi_to_text = function (txt) {
var options = {};
return this.process(txt, options, false);
};
Ansi_Up.prototype.process = function (txt, options, markup) {
var self = this;
var raw_text_chunks = txt.split(/\033\[/);

@@ -68,3 +117,3 @@ 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);
return self.process_chunk(chunk, options, markup);
});

@@ -77,3 +126,3 @@

Ansi_Up.prototype.process_chunk = function (text, options) {
Ansi_Up.prototype.process_chunk = function (text, options, markup) {

@@ -111,5 +160,10 @@ // Are we using classes or styles?

if (!markup) {
return orig_txt;
}
var self = this;
nums.map(function (num_str) {
while (nums.length > 0) {
var num_str = nums.shift();
var num = parseInt(num_str);

@@ -122,2 +176,6 @@

self.bright = 1;
} else if (num == 39) {
self.fg = null;
} else if (num == 49) {
self.bg = null;
} else if ((num >= 30) && (num < 38)) {

@@ -131,4 +189,57 @@ self.fg = ANSI_COLORS[self.bright][(num % 10)][key];

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;
}
}
}
}
}
})();
}
});
}

@@ -138,6 +249,23 @@ if ((self.fg === null) && (self.bg === null)) {

} else {
var styles = classes = [];
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]) + '"');
}
}
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;
}
} else {

@@ -150,2 +278,6 @@ styles.push("color:rgb(" + self.fg + ")");

classes.push(self.bg + "-bg");
if (self.bg_truecolor !== null) {
data['ansi-truecolor-bg'] = self.bg_truecolor;
self.bg_truecolor = null;
}
} else {

@@ -156,5 +288,5 @@ styles.push("background-color:rgb(" + self.bg + ")");

if (use_classes) {
return "<span class=\"" + classes.join(' ') + "\">" + orig_txt + "</span>";
return '<span class="' + classes.join(' ') + '"' + render_data.call(self, data) + '>' + orig_txt + '</span>';
} else {
return "<span style=\"" + styles.join(';') + "\">" + orig_txt + "</span>";
return '<span style="' + styles.join(';') + '"' + render_data.call(self, data) + '>' + orig_txt + '</span>';
}

@@ -182,2 +314,7 @@ }

ansi_to_text: function (txt) {
var a2h = new Ansi_Up();
return a2h.ansi_to_text(txt);
},
ansi_to_html_obj: function () {

@@ -184,0 +321,0 @@ return new Ansi_Up();

2

bower.json
{
"name": "ansi_up",
"main": "ansi_up.js",
"version": "1.2.1",
"version": "1.3.0",
"homepage": "https://github.com/drudru/ansi_up",

@@ -6,0 +6,0 @@ "authors": [

{
"name": "ansi_up",
"version": "1.2.1",
"version": "1.3.0",
"description": "Convert ansi sequences in strings to colorful HTML",

@@ -5,0 +5,0 @@ "keywords": ["ansi", "html"],

@@ -93,4 +93,5 @@ # ansi_up.js

- Maximilian Antoni (<https://github.com/mantoni>)
- Jim Bauwens (<https://github.com/jimbauwens>)
- Jacek Jędrzejewski (<https://github.com/eXtreme>)
## License

@@ -97,0 +98,0 @@

@@ -7,122 +7,122 @@ var ansi_up = require('../ansi_up');

describe('escape_for_html', function() {
describe('escape_for_html', function() {
describe('ampersands', function() {
describe('ampersands', function() {
it('should escape a single ampersand', function() {
var start = "&";
var expected = "&amp;";
it('should escape a single ampersand', function() {
var start = "&";
var expected = "&amp;";
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
it('should escape some text with ampersands', function() {
var start = "abcd&efgh";
var expected = "abcd&amp;efgh";
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);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
it('should escape multiple ampersands', function() {
var start = " & & ";
var expected = " &amp; &amp; ";
it('should escape multiple ampersands', function() {
var start = " & & ";
var expected = " &amp; &amp; ";
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
it('should escape an already escaped ampersand', function() {
var start = " &amp; ";
var expected = " &amp;amp; ";
it('should escape an already escaped ampersand', function() {
var start = " &amp; ";
var expected = " &amp;amp; ";
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
});
describe('less-than', function() {
describe('less-than', function() {
it('should escape a single less-than', function() {
var start = "<";
var expected = "&lt;";
it('should escape a single less-than', function() {
var start = "<";
var expected = "&lt;";
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
it('should escape some text with less-thans', function() {
var start = "abcd<efgh";
var expected = "abcd&lt;efgh";
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);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
it('should escape multiple less-thans', function() {
var start = " < < ";
var expected = " &lt; &lt; ";
it('should escape multiple less-thans', function() {
var start = " < < ";
var expected = " &lt; &lt; ";
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
});
});
describe('greater-than', function() {
describe('greater-than', function() {
it('should escape a single greater-than', function() {
var start = ">";
var expected = "&gt;";
it('should escape a single greater-than', function() {
var start = ">";
var expected = "&gt;";
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
it('should escape some text with greater-thans', function() {
var start = "abcd>efgh";
var expected = "abcd&gt;efgh";
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);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
it('should escape multiple greater-thans', function() {
var start = " > > ";
var expected = " &gt; &gt; ";
it('should escape multiple greater-thans', function() {
var start = " > > ";
var expected = " &gt; &gt; ";
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
});
});
describe('mixed characters', function() {
describe('mixed characters', function() {
it('should escape a mix of characters that require escaping', function() {
var start = "<&>/\\'\"";
var expected = "&lt;&amp;&gt;/\\'\"";
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);
l.should.eql(expected);
});
var l = ansi_up.escape_for_html(start);
l.should.eql(expected);
});
});
});
});
});
describe('linkify', 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>";
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 l = ansi_up.linkify(start);
l.should.eql(expected);
});
var l = ansi_up.linkify(start);
l.should.eql(expected);
});
});
});
describe('ansi to html', function() {
describe('ansi to html', function() {

@@ -220,2 +220,114 @@ describe('default colors', function() {

});
it('should transform a foreground and background and reset foreground to html', function() {
var fg = 37;
var bg = 42;
var start = "\n\033[40m \033[49m\033[" + fg + ";" + bg + "m " + bg + " \033[39m 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)\"> " + bg + " </span><span style=\"background-color:rgb(0, 187, 0)\"> foobar </span>";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a foreground and background and reset background to html', function() {
var fg = 37;
var bg = 42;
var start = "\n\033[40m \033[49m\033[" + fg + ";" + bg + "m " + fg + " \033[49m 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 + " </span><span style=\"color:rgb(255,255,255)\"> foobar </span>";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a foreground and background and reset them to html', function() {
var fg = 37;
var bg = 42;
var start = "\n\033[40m \033[49m\033[" + fg + ";" + bg + "m " + fg + ';' + bg + " \033[39;49m 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);
l.should.eql(expected);
});
describe('transform extend 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);
l.should.eql(expected);
});
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);
l.should.eql(expected);
});
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);
l.should.eql(expected);
});
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);
l.should.eql(expected);
});
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);
l.should.eql(expected);
});
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);
l.should.eql(expected);
});
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);
l.should.eql(expected);
});
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);
l.should.eql(expected);
});
});
describe('transform extend 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);
l.should.eql(expected);
});
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);
l.should.eql(expected);
});
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);
l.should.eql(expected);
});
});
});

@@ -281,2 +393,81 @@

});
describe('transform extend 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});
l.should.eql(expected);
});
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});
l.should.eql(expected);
});
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});
l.should.eql(expected);
});
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});
l.should.eql(expected);
});
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});
l.should.eql(expected);
});
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});
l.should.eql(expected);
});
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});
l.should.eql(expected);
});
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});
l.should.eql(expected);
});
});
describe('transform extend 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});
l.should.eql(expected);
});
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});
l.should.eql(expected);
});
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});
l.should.eql(expected);
});
});
});

@@ -329,3 +520,20 @@ describe('ignore unsupported CSI', 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);
l.should.eql("foo bar baz");
});
it('should remove unsupported sequence', function() {
var start = "foo \033[1Abar";
var l = ansi_up.ansi_to_text(start);
l.should.eql('foo bar');
});
it('should keep multiline', function() {
var start = "foo \033[1;32mbar\nbaz\033[0m qux";
var l = ansi_up.ansi_to_text(start);
l.should.eql("foo bar\nbaz qux");
});
});
});

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