AnsiPainter.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. // Generated by CoffeeScript 1.9.3
  2. var AnsiPainter, object, styles, tags, tools,
  3. hasProp = {}.hasOwnProperty,
  4. slice = [].slice;
  5. tools = require('./tools');
  6. tags = require('./ansiPainter/tags');
  7. styles = require('./ansiPainter/styles');
  8. object = require('utila').object;
  9. module.exports = AnsiPainter = (function() {
  10. var self;
  11. function AnsiPainter() {}
  12. AnsiPainter.tags = tags;
  13. AnsiPainter.prototype.paint = function(s) {
  14. return this._replaceSpecialStrings(this._renderDom(this._parse(s)));
  15. };
  16. AnsiPainter.prototype._replaceSpecialStrings = function(str) {
  17. return str.replace(/&sp;/g, ' ').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&amp;/g, '&');
  18. };
  19. AnsiPainter.prototype._parse = function(string, injectFakeRoot) {
  20. if (injectFakeRoot == null) {
  21. injectFakeRoot = true;
  22. }
  23. if (injectFakeRoot) {
  24. string = '<none>' + string + '</none>';
  25. }
  26. return tools.toDom(string);
  27. };
  28. AnsiPainter.prototype._renderDom = function(dom) {
  29. var parentStyles;
  30. parentStyles = {
  31. bg: 'none',
  32. color: 'none'
  33. };
  34. return this._renderChildren(dom, parentStyles);
  35. };
  36. AnsiPainter.prototype._renderChildren = function(children, parentStyles) {
  37. var child, n, ret;
  38. ret = '';
  39. for (n in children) {
  40. if (!hasProp.call(children, n)) continue;
  41. child = children[n];
  42. ret += this._renderNode(child, parentStyles);
  43. }
  44. return ret;
  45. };
  46. AnsiPainter.prototype._renderNode = function(node, parentStyles) {
  47. if (node.type === 'text') {
  48. return this._renderTextNode(node, parentStyles);
  49. } else {
  50. return this._renderTag(node, parentStyles);
  51. }
  52. };
  53. AnsiPainter.prototype._renderTextNode = function(node, parentStyles) {
  54. return this._wrapInStyle(node.data, parentStyles);
  55. };
  56. AnsiPainter.prototype._wrapInStyle = function(str, style) {
  57. return styles.color(style.color) + styles.bg(style.bg) + str + styles.none();
  58. };
  59. AnsiPainter.prototype._renderTag = function(node, parentStyles) {
  60. var currentStyles, tagStyles;
  61. tagStyles = this._getStylesForTagName(node.name);
  62. currentStyles = this._mixStyles(parentStyles, tagStyles);
  63. return this._renderChildren(node.children, currentStyles);
  64. };
  65. AnsiPainter.prototype._mixStyles = function() {
  66. var final, i, key, len, style, styles, val;
  67. styles = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  68. final = {};
  69. for (i = 0, len = styles.length; i < len; i++) {
  70. style = styles[i];
  71. for (key in style) {
  72. if (!hasProp.call(style, key)) continue;
  73. val = style[key];
  74. if ((final[key] == null) || val !== 'inherit') {
  75. final[key] = val;
  76. }
  77. }
  78. }
  79. return final;
  80. };
  81. AnsiPainter.prototype._getStylesForTagName = function(name) {
  82. if (tags[name] == null) {
  83. throw Error("Unkown tag name `" + name + "`");
  84. }
  85. return tags[name];
  86. };
  87. self = AnsiPainter;
  88. AnsiPainter.getInstance = function() {
  89. if (self._instance == null) {
  90. self._instance = new self;
  91. }
  92. return self._instance;
  93. };
  94. AnsiPainter.paint = function(str) {
  95. return self.getInstance().paint(str);
  96. };
  97. AnsiPainter.strip = function(s) {
  98. return s.replace(/\x1b\[[0-9]+m/g, '');
  99. };
  100. return AnsiPainter;
  101. })();