Socket
Socket
Sign inDemoInstall

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.0.0 to 1.1.1

examples/theme.css

75

ansi_up.js
// ansi_up.js
// version : 1.0.0
// version : 1.1.0
// author : Dru Nelson

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

var ansi_up,
VERSION = "1.0.0",
VERSION = "1.1.0",

@@ -18,4 +18,22 @@ // check for nodeJS

ANSI_COLORS = [
["0,0,0", "187, 0, 0", "0, 187, 0", "187, 187, 0", "0, 0, 187", "187, 0, 187", "0, 187, 187", "255,255,255" ],
["85,85,85", "255, 85, 85", "0, 255, 0", "255, 255, 85", "85, 85, 255", "255, 85, 255", "85, 255, 255", "255,255,255" ]
[
{ 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" }
]
];

@@ -42,3 +60,3 @@

Ansi_Up.prototype.ansi_to_html = function (txt) {
Ansi_Up.prototype.ansi_to_html = function (txt, options) {

@@ -51,3 +69,3 @@ var data4 = txt.split(/\033\[/);

var data5 = data4.map(function (chunk) {
return self.process_chunk(chunk);
return self.process_chunk(chunk, options);
});

@@ -70,7 +88,12 @@

Ansi_Up.prototype.process_chunk = function (text) {
Ansi_Up.prototype.process_chunk = function (text, options) {
// 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';
// Do proper handling of sequences (aka - injest vi split(';') into state machine
//match,codes,txt = text.match(/([\d;]+)m(.*)/m);
var matches = text.match(/([\d;]+?)m([^]*)/m);
var matches = text.match(/([\d;]*)m([^]*)/m);

@@ -87,3 +110,3 @@ if (!matches) return text;

if (num === 0) {
if (isNaN(num) || num === 0) {
self.fg = self.bg = null;

@@ -94,5 +117,5 @@ self.bright = 0;

} else if ((num >= 30) && (num < 38)) {
self.fg = "rgb(" + ANSI_COLORS[self.bright][(num % 10)] + ")";
self.fg = ANSI_COLORS[self.bright][(num % 10)][key];
} else if ((num >= 40) && (num < 48)) {
self.bg = "rgb(" + ANSI_COLORS[0][(num % 10)] + ")";
self.bg = ANSI_COLORS[0][(num % 10)][key];
}

@@ -104,8 +127,22 @@ });

} else {
var style = [];
if (self.fg)
style.push("color:" + self.fg);
if (self.bg)
style.push("background-color:" + self.bg);
return ["<span style=\"" + style.join(';') + "\">", orig_txt, "</span>"];
var styles = classes = [];
if (self.fg) {
if (use_classes) {
classes.push(self.fg + "-fg");
} else {
styles.push("color:rgb(" + self.fg + ")");
}
}
if (self.bg) {
if (use_classes) {
classes.push(self.bg + "-bg");
} else {
styles.push("background-color:rgb(" + self.bg + ")");
}
}
if (use_classes) {
return ["<span class=\"" + classes.join(' ') + "\">", orig_txt, "</span>"];
} else {
return ["<span style=\"" + styles.join(';') + "\">", orig_txt, "</span>"];
}
}

@@ -127,5 +164,5 @@ };

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

@@ -132,0 +169,0 @@

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

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

@@ -0,6 +1,10 @@

# ansi_up.js
_ansi_up_ is a simple library for converting text embedded with ANSI terminal color commands into HTML spans that render the proper coloring. This is compliant with AMD (require.js). This code has been used in production since early 2012. This is a new project, but I will actively maintain it and welcome all feedback. Thanks for reading.
__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.
Turn this:
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.
Turn this terminal output:
ESC[1;Foreground

@@ -12,8 +16,9 @@  30  30  30  30  30  30  30  30 

Into this:
Into this browser output:
![](http://github.com/drudru/ansi_up/raw/master/sample.png)
![](https://raw.github.com/drudru/ansi_up/master/sample.png)
## Browser Example
```HTML
<script src="ansi_up.js" type="text/javascript"></script>

@@ -31,5 +36,7 @@ <script type="text/javascript">

</script>
```
## Node Example
```JavaScript
var ansi_up = require('ansi_up');

@@ -40,4 +47,4 @@

var html = ansi_up.ansi_to_html(txt);
```
There are examples in the repo that demonstrate an AMD/require.js/ jQuery example as well as a simple browser example.

@@ -59,10 +66,13 @@

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.
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.
#### ansi_to_html (txt)
#### ansi_to_html (txt, options)
This replaces ANSI terminal escape codes with SPAN tags that wrap the content. The styles are inline on the SPAN tags.
This replaces ANSI terminal escape codes with SPAN tags that wrap the content. By default the styles are inline on the SPAN tags.
The options parameter is optional and if you pass an object with the key/value pair 'use_classes: true' classes will be set on the SPAN tag instead of inline styles. The classes used are of the format ````ansi-*-fg/bg```` and ````ansi-bright-*-fg/bg```` where * is the colour name, i.e black/red/green/yellow/blue/magenta/cyan/white.
## Building
Currently we are not using a build system, so there is just one file. Feel free to include the file in your asset minification process.
This just uses 'make'. The result is just one file. Feel free to include the file in your asset minification process.

@@ -75,7 +85,17 @@ ## Running tests

## License
## Credits
This code was developed by Dru Nelson (<https://github.com/drudru>).
Thanks goes to the following contributors for their patches:
- James R. White (<https://github.com/jamesrwhite>)
- Aaron Stone (<https://github.com/sodabrew>)
## License
(The MIT License)
Copyright (c) 2011 Dru Nelson
Copyright (c) 2011 Dru Nelson

@@ -82,0 +102,0 @@ Permission is hereby granted, free of charge, to any person obtaining

@@ -8,3 +8,3 @@ var ansi_up = require('../ansi_up');

describe('escape_for_html', function() {
describe('ampersands', function() {

@@ -128,3 +128,3 @@

describe('linkify', function() {
it('should linkify a url', function() {

@@ -142,68 +142,147 @@ this.timeout(1);

describe('ansi to html', function() {
it('should transform a foreground to html', function() {
this.timeout(1);
var attr = 0;
var fg = 32;
var start = "\033[" + fg + "m " + fg + " \033[0m";
var expected = "<span style=\"color:rgb(0, 187, 0)\"> " + fg + " </span>";
describe('default colors', function() {
it('should transform a foreground to html', function() {
this.timeout(1);
var attr = 0;
var fg = 32;
var start = "\033[" + fg + "m " + fg + " \033[0m";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
});
var expected = "<span style=\"color:rgb(0, 187, 0)\"> " + fg + " </span>";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a attr;foreground to html', function() {
this.timeout(1);
var attr = 0;
var fg = 32;
var start = "\033[" + attr + ";" + fg + "m " + fg + " \033[0m";
var expected = "<span style=\"color:rgb(0, 187, 0)\"> " + fg + " </span>";
it('should transform a attr;foreground to html', function() {
this.timeout(1);
var attr = 0;
var fg = 32;
var start = "\033[" + attr + ";" + fg + "m " + fg + " \033[0m";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
});
var expected = "<span style=\"color:rgb(0, 187, 0)\"> " + fg + " </span>";
it('should transform a bold attr;foreground to html', function() {
this.timeout(1);
var attr = 1;
var fg = 32;
var start = "\033[" + attr + ";" + fg + "m " + attr + ";" + fg + " \033[0m";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
});
var expected = "<span style=\"color:rgb(0, 255, 0)\"> " + attr + ";" + fg + " </span>";
it('should transform an empty code to a normal/reset html', function() {
this.timeout(1);
var attr = 0;
var fg = 32;
var start = "\033[" + attr + ";" + fg + "m " + fg + " \033[m x";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
});
var expected = "<span style=\"color:rgb(0, 187, 0)\"> " + fg + " </span> x";
it('should transform a bold attr;background;foreground to html', function() {
this.timeout(1);
var attr = 1;
var fg = 33;
var bg = 42;
var start = "\033[" + attr + ";" + bg + ";" + fg + "m " + attr + ";" + bg + ";" + fg + " \033[0m";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
});
var expected = "<span style=\"color:rgb(255, 255, 85);background-color:rgb(0, 187, 0)\"> " + attr + ";" + bg + ";" + fg + " </span>";
it('should transform a bold attr;foreground to html', function() {
this.timeout(1);
var attr = 1;
var fg = 32;
var start = "\033[" + attr + ";" + fg + "m " + attr + ";" + fg + " \033[0m";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
var expected = "<span style=\"color:rgb(0, 255, 0)\"> " + attr + ";" + fg + " </span>";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
});
it('should transform a bold attr;background;foreground to html', function() {
this.timeout(1);
var attr = 1;
var fg = 33;
var bg = 42;
var start = "\033[" + attr + ";" + bg + ";" + fg + "m " + attr + ";" + bg + ";" + fg + " \033[0m";
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);
l.should.eql(expected);
});
it('should transform a complex multi-line sequence to html', function() {
this.timeout(1);
var attr = 1;
var fg = 32;
var bg = 42;
var start = "\n \033[" + fg + "m " + fg + " \033[0m \n \033[" + bg + "m " + bg + " \033[0m \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);
l.should.eql(expected);
});
});
it('should transform a complex multi-line sequence to html', function() {
this.timeout(1);
var attr = 1;
var fg = 32;
var bg = 42;
var start = "\n \033[" + fg + "m " + fg + " \033[0m \n \033[" + bg + "m " + bg + " \033[0m \n zimpper ";
describe('themed colors', function() {
it('should transform a foreground to html', function() {
this.timeout(1);
var attr = 0;
var fg = 32;
var start = "\033[" + fg + "m " + fg + " \033[0m";
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 = "<span class=\"ansi-green-fg\"> " + fg + " </span>";
var l = ansi_up.ansi_to_html(start);
l.should.eql(expected);
var l = ansi_up.ansi_to_html(start, {use_classes: true});
l.should.eql(expected);
});
it('should transform a attr;foreground to html', function() {
this.timeout(1);
var attr = 0;
var fg = 32;
var start = "\033[" + attr + ";" + fg + "m " + fg + " \033[0m";
var expected = "<span class=\"ansi-green-fg\"> " + fg + " </span>";
var l = ansi_up.ansi_to_html(start, {use_classes: true});
l.should.eql(expected);
});
it('should transform a bold attr;foreground to html', function() {
this.timeout(1);
var attr = 1;
var fg = 32;
var start = "\033[" + attr + ";" + fg + "m " + attr + ";" + fg + " \033[0m";
var expected = "<span class=\"ansi-bright-green-fg\"> " + attr + ";" + fg + " </span>";
var l = ansi_up.ansi_to_html(start, {use_classes: true});
l.should.eql(expected);
});
it('should transform a bold attr;background;foreground to html', function() {
this.timeout(1);
var attr = 1;
var fg = 33;
var bg = 42;
var start = "\033[" + attr + ";" + bg + ";" + fg + "m " + attr + ";" + bg + ";" + fg + " \033[0m";
var expected = "<span class=\"ansi-bright-yellow-fg ansi-green-bg\"> " + attr + ";" + bg + ";" + fg + " </span>";
var l = ansi_up.ansi_to_html(start, {use_classes: true});
l.should.eql(expected);
});
it('should transform a complex multi-line sequence to html', function() {
this.timeout(1);
var attr = 1;
var fg = 32;
var bg = 42;
var start = "\n \033[" + fg + "m " + fg + " \033[0m \n \033[" + bg + "m " + bg + " \033[0m \n zimpper ";
var expected = "\n <span class=\"ansi-green-fg\"> " + fg + " </span> \n <span class=\"ansi-green-bg\"> " + bg + " </span> \n zimpper ";
var l = ansi_up.ansi_to_html(start, {use_classes: true});
l.should.eql(expected);
});
});
});
});
});

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