Layout.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 Block, Layout, SpecialString, cloneAndMergeDeep, i, len, prop, ref, terminalWidth;
  7. Block = require('./layout/Block');
  8. var _require = require('./tools');
  9. cloneAndMergeDeep = _require.cloneAndMergeDeep;
  10. SpecialString = require('./layout/SpecialString');
  11. terminalWidth = require('./tools').getCols();
  12. module.exports = Layout = function () {
  13. var self;
  14. var Layout = /*#__PURE__*/function () {
  15. function Layout() {
  16. var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  17. var rootBlockConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
  18. _classCallCheck(this, Layout);
  19. var rootConfig;
  20. this._written = [];
  21. this._activeBlock = null;
  22. this._config = cloneAndMergeDeep(self._defaultConfig, config); // Every layout has a root block
  23. rootConfig = cloneAndMergeDeep(self._rootBlockDefaultConfig, rootBlockConfig);
  24. this._root = new Block(this, null, rootConfig, '__root');
  25. this._root._open();
  26. }
  27. _createClass(Layout, [{
  28. key: "getRootBlock",
  29. value: function getRootBlock() {
  30. return this._root;
  31. }
  32. }, {
  33. key: "_append",
  34. value: function _append(text) {
  35. return this._written.push(text);
  36. }
  37. }, {
  38. key: "_appendLine",
  39. value: function _appendLine(text) {
  40. var s;
  41. this._append(text);
  42. s = new SpecialString(text);
  43. if (s.length < this._config.terminalWidth) {
  44. this._append('<none>\n</none>');
  45. }
  46. return this;
  47. }
  48. }, {
  49. key: "get",
  50. value: function get() {
  51. this._ensureClosed();
  52. if (this._written[this._written.length - 1] === '<none>\n</none>') {
  53. this._written.pop();
  54. }
  55. return this._written.join("");
  56. }
  57. }, {
  58. key: "_ensureClosed",
  59. value: function _ensureClosed() {
  60. if (this._activeBlock !== this._root) {
  61. throw Error("Not all the blocks have been closed. Please call block.close() on all open blocks.");
  62. }
  63. if (this._root.isOpen()) {
  64. this._root.close();
  65. }
  66. }
  67. }]);
  68. return Layout;
  69. }();
  70. ;
  71. self = Layout;
  72. Layout._rootBlockDefaultConfig = {
  73. linePrependor: {
  74. options: {
  75. amount: 0
  76. }
  77. },
  78. lineAppendor: {
  79. options: {
  80. amount: 0
  81. }
  82. },
  83. blockPrependor: {
  84. options: {
  85. amount: 0
  86. }
  87. },
  88. blockAppendor: {
  89. options: {
  90. amount: 0
  91. }
  92. }
  93. };
  94. Layout._defaultConfig = {
  95. terminalWidth: terminalWidth
  96. };
  97. return Layout;
  98. }.call(void 0);
  99. ref = ['openBlock', 'write'];
  100. for (i = 0, len = ref.length; i < len; i++) {
  101. prop = ref[i];
  102. (function () {
  103. var method;
  104. method = prop;
  105. return Layout.prototype[method] = function () {
  106. return this._root[method].apply(this._root, arguments);
  107. };
  108. })();
  109. }