-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathshellAnsiFormat.js
83 lines (62 loc) · 2.35 KB
/
shellAnsiFormat.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
/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true,
indent: 4, maxerr: 50, unparam: true, node: true */
/*global define, $, document */
/*
Reference Links:
http://en.wikipedia.org/wiki/ANSI_escape_code#graphics
http://bluesock.org/~willg/dev/ansi.html
*/
define(function (require, exports) {
"use strict";
var _regExp = /\u001B\[(3|4)(0|1|2|3|4|5|6|7|9)m/i;
function _hasAceptedAnsiFormat(value) {
return (value) ? _regExp.test(value) : false;
}
// function _removeNonSupportedSpecialChars(data) {
//
// var text = data.concat("");
// text = text.replace(/\u001B\[((1|2|4|5|6|7|8|9|10)?(1|2|4|5|6|7|8|9))m/ig, "");
//
// return text;
//
// }
function _formattedText(data, currentCommandResult) {
var path = /\u001B\[(3|4)(0|1|2|3|4|5|6|7|9)m/i,
text = data.concat(""),
index = text.search(path),
index2 = -1,
code = "",
prevText = "",
span;
while(index > -1) {
prevText = text.substr(0, index);
if(prevText && prevText.length) {
$("pre", currentCommandResult).append(document.createTextNode(prevText));
}
code = text.substr(index, 5);
text = text.substr(index + 5);
span = document.createElement("span");
span.className = _getFormat(code);
index2 = text.search(path);
$("pre", currentCommandResult).append(document.createTextNode(prevText));
if(index2 > -1) {
span.textContent += text.substr(0, index2);
text = text.substr(index2);
index2 = 0;
} else {
span.textContent += text;
}
$("pre", currentCommandResult).append(span);
index = index2;
}
}
function _getFormat(data) {
var color = ["black", "red", "green", "yellow", "blue", "magenta", "cyan", "white", "default", "default"],
n = parseInt(data.charAt(3)),
nn = parseInt(data.charAt(2)),
txt = (nn == 4) ? "bgr-" : "";
return "hdy-" + txt + color[n];
}
exports.hasAceptedAnsiFormat = _hasAceptedAnsiFormat;
exports.formattedText = _formattedText;
});