Layout.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // Generated by CoffeeScript 1.9.3
  2. var Block, Layout, SpecialString, fn, i, len, object, prop, ref, terminalWidth;
  3. Block = require('./layout/Block');
  4. object = require('utila').object;
  5. SpecialString = require('./layout/SpecialString');
  6. terminalWidth = require('./tools').getCols();
  7. module.exports = Layout = (function() {
  8. var self;
  9. self = Layout;
  10. Layout._rootBlockDefaultConfig = {
  11. linePrependor: {
  12. options: {
  13. amount: 0
  14. }
  15. },
  16. lineAppendor: {
  17. options: {
  18. amount: 0
  19. }
  20. },
  21. blockPrependor: {
  22. options: {
  23. amount: 0
  24. }
  25. },
  26. blockAppendor: {
  27. options: {
  28. amount: 0
  29. }
  30. }
  31. };
  32. Layout._defaultConfig = {
  33. terminalWidth: terminalWidth
  34. };
  35. function Layout(config, rootBlockConfig) {
  36. var rootConfig;
  37. if (config == null) {
  38. config = {};
  39. }
  40. if (rootBlockConfig == null) {
  41. rootBlockConfig = {};
  42. }
  43. this._written = [];
  44. this._activeBlock = null;
  45. this._config = object.append(self._defaultConfig, config);
  46. rootConfig = object.append(self._rootBlockDefaultConfig, rootBlockConfig);
  47. this._root = new Block(this, null, rootConfig, '__root');
  48. this._root._open();
  49. }
  50. Layout.prototype.getRootBlock = function() {
  51. return this._root;
  52. };
  53. Layout.prototype._append = function(text) {
  54. return this._written.push(text);
  55. };
  56. Layout.prototype._appendLine = function(text) {
  57. var s;
  58. this._append(text);
  59. s = SpecialString(text);
  60. if (s.length < this._config.terminalWidth) {
  61. this._append('<none>\n</none>');
  62. }
  63. return this;
  64. };
  65. Layout.prototype.get = function() {
  66. this._ensureClosed();
  67. if (this._written[this._written.length - 1] === '<none>\n</none>') {
  68. this._written.pop();
  69. }
  70. return this._written.join("");
  71. };
  72. Layout.prototype._ensureClosed = function() {
  73. if (this._activeBlock !== this._root) {
  74. throw Error("Not all the blocks have been closed. Please call block.close() on all open blocks.");
  75. }
  76. if (this._root.isOpen()) {
  77. this._root.close();
  78. }
  79. };
  80. return Layout;
  81. })();
  82. ref = ['openBlock', 'write'];
  83. fn = function() {
  84. var method;
  85. method = prop;
  86. return Layout.prototype[method] = function() {
  87. return this._root[method].apply(this._root, arguments);
  88. };
  89. };
  90. for (i = 0, len = ref.length; i < len; i++) {
  91. prop = ref[i];
  92. fn();
  93. }