Block.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. // Generated by CoffeeScript 1.9.3
  2. var Block, SpecialString, object, terminalWidth;
  3. SpecialString = require('./SpecialString');
  4. object = require('utila').object;
  5. terminalWidth = require('../tools').getCols();
  6. module.exports = Block = (function() {
  7. var self;
  8. self = Block;
  9. Block.defaultConfig = {
  10. blockPrependor: {
  11. fn: require('./block/blockPrependor/Default'),
  12. options: {
  13. amount: 0
  14. }
  15. },
  16. blockAppendor: {
  17. fn: require('./block/blockAppendor/Default'),
  18. options: {
  19. amount: 0
  20. }
  21. },
  22. linePrependor: {
  23. fn: require('./block/linePrependor/Default'),
  24. options: {
  25. amount: 0
  26. }
  27. },
  28. lineAppendor: {
  29. fn: require('./block/lineAppendor/Default'),
  30. options: {
  31. amount: 0
  32. }
  33. },
  34. lineWrapper: {
  35. fn: require('./block/lineWrapper/Default'),
  36. options: {
  37. lineWidth: null
  38. }
  39. },
  40. width: terminalWidth,
  41. prefixRaw: '',
  42. suffixRaw: ''
  43. };
  44. function Block(_layout, _parent, config, _name) {
  45. this._layout = _layout;
  46. this._parent = _parent;
  47. if (config == null) {
  48. config = {};
  49. }
  50. this._name = _name != null ? _name : '';
  51. this._config = object.append(self.defaultConfig, config);
  52. this._closed = false;
  53. this._wasOpenOnce = false;
  54. this._active = false;
  55. this._buffer = '';
  56. this._didSeparateBlock = false;
  57. this._linePrependor = new this._config.linePrependor.fn(this._config.linePrependor.options);
  58. this._lineAppendor = new this._config.lineAppendor.fn(this._config.lineAppendor.options);
  59. this._blockPrependor = new this._config.blockPrependor.fn(this._config.blockPrependor.options);
  60. this._blockAppendor = new this._config.blockAppendor.fn(this._config.blockAppendor.options);
  61. }
  62. Block.prototype._activate = function(deactivateParent) {
  63. if (deactivateParent == null) {
  64. deactivateParent = true;
  65. }
  66. if (this._active) {
  67. throw Error("This block is already active. This is probably a bug in RenderKid itself");
  68. }
  69. if (this._closed) {
  70. throw Error("This block is closed and cannot be activated. This is probably a bug in RenderKid itself");
  71. }
  72. this._active = true;
  73. this._layout._activeBlock = this;
  74. if (deactivateParent) {
  75. if (this._parent != null) {
  76. this._parent._deactivate(false);
  77. }
  78. }
  79. return this;
  80. };
  81. Block.prototype._deactivate = function(activateParent) {
  82. if (activateParent == null) {
  83. activateParent = true;
  84. }
  85. this._ensureActive();
  86. this._flushBuffer();
  87. if (activateParent) {
  88. if (this._parent != null) {
  89. this._parent._activate(false);
  90. }
  91. }
  92. this._active = false;
  93. return this;
  94. };
  95. Block.prototype._ensureActive = function() {
  96. if (!this._wasOpenOnce) {
  97. throw Error("This block has never been open before. This is probably a bug in RenderKid itself.");
  98. }
  99. if (!this._active) {
  100. throw Error("This block is not active. This is probably a bug in RenderKid itself.");
  101. }
  102. if (this._closed) {
  103. throw Error("This block is already closed. This is probably a bug in RenderKid itself.");
  104. }
  105. };
  106. Block.prototype._open = function() {
  107. if (this._wasOpenOnce) {
  108. throw Error("Block._open() has been called twice. This is probably a RenderKid bug.");
  109. }
  110. this._wasOpenOnce = true;
  111. if (this._parent != null) {
  112. this._parent.write(this._whatToPrependToBlock());
  113. }
  114. this._activate();
  115. return this;
  116. };
  117. Block.prototype.close = function() {
  118. this._deactivate();
  119. this._closed = true;
  120. if (this._parent != null) {
  121. this._parent.write(this._whatToAppendToBlock());
  122. }
  123. return this;
  124. };
  125. Block.prototype.isOpen = function() {
  126. return this._wasOpenOnce && !this._closed;
  127. };
  128. Block.prototype.write = function(str) {
  129. this._ensureActive();
  130. if (str === '') {
  131. return;
  132. }
  133. str = String(str);
  134. this._buffer += str;
  135. return this;
  136. };
  137. Block.prototype.openBlock = function(config, name) {
  138. var block;
  139. this._ensureActive();
  140. block = new Block(this._layout, this, config, name);
  141. block._open();
  142. return block;
  143. };
  144. Block.prototype._flushBuffer = function() {
  145. var str;
  146. if (this._buffer === '') {
  147. return;
  148. }
  149. str = this._buffer;
  150. this._buffer = '';
  151. this._writeInline(str);
  152. };
  153. Block.prototype._toPrependToLine = function() {
  154. var fromParent;
  155. fromParent = '';
  156. if (this._parent != null) {
  157. fromParent = this._parent._toPrependToLine();
  158. }
  159. return this._linePrependor.render(fromParent);
  160. };
  161. Block.prototype._toAppendToLine = function() {
  162. var fromParent;
  163. fromParent = '';
  164. if (this._parent != null) {
  165. fromParent = this._parent._toAppendToLine();
  166. }
  167. return this._lineAppendor.render(fromParent);
  168. };
  169. Block.prototype._whatToPrependToBlock = function() {
  170. return this._blockPrependor.render();
  171. };
  172. Block.prototype._whatToAppendToBlock = function() {
  173. return this._blockAppendor.render();
  174. };
  175. Block.prototype._writeInline = function(str) {
  176. var i, j, k, l, lineBreaksToAppend, m, ref, ref1, ref2, remaining;
  177. if (SpecialString(str).isOnlySpecialChars()) {
  178. this._layout._append(str);
  179. return;
  180. }
  181. remaining = str;
  182. lineBreaksToAppend = 0;
  183. if (m = remaining.match(/^\n+/)) {
  184. for (i = j = 1, ref = m[0].length; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
  185. this._writeLine('');
  186. }
  187. remaining = remaining.substr(m[0].length, remaining.length);
  188. }
  189. if (m = remaining.match(/\n+$/)) {
  190. lineBreaksToAppend = m[0].length;
  191. remaining = remaining.substr(0, remaining.length - m[0].length);
  192. }
  193. while (remaining.length > 0) {
  194. if (m = remaining.match(/^[^\n]+/)) {
  195. this._writeLine(m[0]);
  196. remaining = remaining.substr(m[0].length, remaining.length);
  197. } else if (m = remaining.match(/^\n+/)) {
  198. for (i = k = 1, ref1 = m[0].length; 1 <= ref1 ? k < ref1 : k > ref1; i = 1 <= ref1 ? ++k : --k) {
  199. this._writeLine('');
  200. }
  201. remaining = remaining.substr(m[0].length, remaining.length);
  202. }
  203. }
  204. if (lineBreaksToAppend > 0) {
  205. for (i = l = 1, ref2 = lineBreaksToAppend; 1 <= ref2 ? l <= ref2 : l >= ref2; i = 1 <= ref2 ? ++l : --l) {
  206. this._writeLine('');
  207. }
  208. }
  209. };
  210. Block.prototype._writeLine = function(str) {
  211. var line, lineContent, lineContentLength, remaining, roomLeft, toAppend, toAppendLength, toPrepend, toPrependLength;
  212. remaining = SpecialString(str);
  213. while (true) {
  214. toPrepend = this._toPrependToLine();
  215. toPrependLength = SpecialString(toPrepend).length;
  216. toAppend = this._toAppendToLine();
  217. toAppendLength = SpecialString(toAppend).length;
  218. roomLeft = this._layout._config.terminalWidth - (toPrependLength + toAppendLength);
  219. lineContentLength = Math.min(this._config.width, roomLeft);
  220. lineContent = remaining.cut(0, lineContentLength, true);
  221. line = toPrepend + lineContent.str + toAppend;
  222. this._layout._appendLine(line);
  223. if (remaining.isEmpty()) {
  224. break;
  225. }
  226. }
  227. };
  228. return Block;
  229. })();