AnsiPainter.js 4.7 KB

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