forked from drudru/ansi_up
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ansi_up.js
143 lines (113 loc) · 3.72 KB
/
ansi_up.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
// ansi_up.js
// version : 1.0.0
// author : Dru Nelson
// license : MIT
// http://github.com/drudru/ansi_up
(function (Date, undefined) {
var ansi_up,
VERSION = "1.0.0",
// check for nodeJS
hasModule = (typeof module !== 'undefined'),
// Normal and then Bright
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" ]
];
function Ansi_Up() {
this.fg = this.bg = null;
this.bright = 0;
}
Ansi_Up.prototype.escape_for_html = function (txt) {
return txt.replace(/[&<>]/gm, function(str) {
if (str == "&") return "&";
if (str == "<") return "<";
if (str == ">") return ">";
});
};
Ansi_Up.prototype.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) {
var data4 = txt.split(/\033\[/);
var first = data4.shift(); // the first chunk is not the result of the split
var self = this;
var data5 = data4.map(function (chunk) {
return self.process_chunk(chunk);
});
data5.unshift(first);
var flattened_data = data5.reduce( function (a, b) {
if (Array.isArray(b))
return a.concat(b);
a.push(b);
return a;
}, []);
var escaped_data = flattened_data.join('');
return escaped_data;
};
Ansi_Up.prototype.process_chunk = function (text) {
// 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);
if (!matches) return text;
var orig_txt = matches[2];
var nums = matches[1].split(';');
var self = this;
nums.map(function (num_str) {
var num = parseInt(num_str);
if (num === 0) {
self.fg = self.bg = null;
self.bright = 0;
} else if (num === 1) {
self.bright = 1;
} else if ((num >= 30) && (num < 38)) {
self.fg = "rgb(" + ANSI_COLORS[self.bright][(num % 10)] + ")";
} else if ((num >= 40) && (num < 48)) {
self.bg = "rgb(" + ANSI_COLORS[0][(num % 10)] + ")";
}
});
if ((self.fg === null) && (self.bg === null)) {
return orig_txt;
} 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>"];
}
};
// 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) {
var a2h = new Ansi_Up();
return a2h.ansi_to_html(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);