UNPKG

4.84 kBJavaScriptView Raw
1"use strict";
2
3function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
4
5function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
6
7function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
8
9// Generated by CoffeeScript 2.5.1
10var AnsiPainter,
11 styles,
12 tags,
13 tools,
14 hasProp = {}.hasOwnProperty;
15tools = require('./tools');
16tags = require('./ansiPainter/tags');
17styles = require('./ansiPainter/styles');
18
19module.exports = AnsiPainter = function () {
20 var self;
21
22 var AnsiPainter = /*#__PURE__*/function () {
23 function AnsiPainter() {
24 _classCallCheck(this, AnsiPainter);
25 }
26
27 _createClass(AnsiPainter, [{
28 key: "paint",
29 value: function paint(s) {
30 return this._replaceSpecialStrings(this._renderDom(this._parse(s)));
31 }
32 }, {
33 key: "_replaceSpecialStrings",
34 value: function _replaceSpecialStrings(str) {
35 return str.replace(/&sp;/g, ' ').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&amp;/g, '&');
36 }
37 }, {
38 key: "_parse",
39 value: function _parse(string) {
40 var injectFakeRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
41
42 if (injectFakeRoot) {
43 string = '<none>' + string + '</none>';
44 }
45
46 return tools.toDom(string);
47 }
48 }, {
49 key: "_renderDom",
50 value: function _renderDom(dom) {
51 var parentStyles;
52 parentStyles = {
53 bg: 'none',
54 color: 'none'
55 };
56 return this._renderChildren(dom, parentStyles);
57 }
58 }, {
59 key: "_renderChildren",
60 value: function _renderChildren(children, parentStyles) {
61 var child, n, ret;
62 ret = '';
63
64 for (n in children) {
65 if (!hasProp.call(children, n)) continue;
66 child = children[n];
67 ret += this._renderNode(child, parentStyles);
68 }
69
70 return ret;
71 }
72 }, {
73 key: "_renderNode",
74 value: function _renderNode(node, parentStyles) {
75 if (node.type === 'text') {
76 return this._renderTextNode(node, parentStyles);
77 } else {
78 return this._renderTag(node, parentStyles);
79 }
80 }
81 }, {
82 key: "_renderTextNode",
83 value: function _renderTextNode(node, parentStyles) {
84 return this._wrapInStyle(node.data, parentStyles);
85 }
86 }, {
87 key: "_wrapInStyle",
88 value: function _wrapInStyle(str, style) {
89 return styles.color(style.color) + styles.bg(style.bg) + str + styles.none();
90 }
91 }, {
92 key: "_renderTag",
93 value: function _renderTag(node, parentStyles) {
94 var currentStyles, tagStyles;
95 tagStyles = this._getStylesForTagName(node.name);
96 currentStyles = this._mixStyles(parentStyles, tagStyles);
97 return this._renderChildren(node.children, currentStyles);
98 }
99 }, {
100 key: "_mixStyles",
101 value: function _mixStyles() {
102 var final, i, key, len, style, val;
103 final = {};
104
105 for (var _len = arguments.length, styles = new Array(_len), _key = 0; _key < _len; _key++) {
106 styles[_key] = arguments[_key];
107 }
108
109 for (i = 0, len = styles.length; i < len; i++) {
110 style = styles[i];
111
112 for (key in style) {
113 if (!hasProp.call(style, key)) continue;
114 val = style[key];
115
116 if (final[key] == null || val !== 'inherit') {
117 final[key] = val;
118 }
119 }
120 }
121
122 return final;
123 }
124 }, {
125 key: "_getStylesForTagName",
126 value: function _getStylesForTagName(name) {
127 if (tags[name] == null) {
128 throw Error("Unknown tag name `".concat(name, "`"));
129 }
130
131 return tags[name];
132 }
133 }], [{
134 key: "getInstance",
135 value: function getInstance() {
136 if (self._instance == null) {
137 self._instance = new self();
138 }
139
140 return self._instance;
141 }
142 }, {
143 key: "paint",
144 value: function paint(str) {
145 return self.getInstance().paint(str);
146 }
147 }, {
148 key: "strip",
149 value: function strip(s) {
150 return s.replace(/\x1b\[[0-9]+m/g, '');
151 }
152 }]);
153
154 return AnsiPainter;
155 }();
156
157 ;
158 AnsiPainter.tags = tags;
159 self = AnsiPainter;
160 return AnsiPainter;
161}.call(void 0);
\No newline at end of file