Block.js
10.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
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); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
// Generated by CoffeeScript 2.5.1
var Block, SpecialString, cloneAndMergeDeep, terminalWidth;
SpecialString = require('./SpecialString');
terminalWidth = require('../tools').getCols();
var _require = require('../tools');
cloneAndMergeDeep = _require.cloneAndMergeDeep;
module.exports = Block = function () {
var self;
var Block = /*#__PURE__*/function () {
function Block(_layout, _parent) {
var config = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
var _name = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : '';
_classCallCheck(this, Block);
this._layout = _layout;
this._parent = _parent;
this._name = _name;
this._config = cloneAndMergeDeep(self.defaultConfig, config);
this._closed = false;
this._wasOpenOnce = false;
this._active = false;
this._buffer = '';
this._didSeparateBlock = false;
this._linePrependor = new this._config.linePrependor.fn(this._config.linePrependor.options);
this._lineAppendor = new this._config.lineAppendor.fn(this._config.lineAppendor.options);
this._blockPrependor = new this._config.blockPrependor.fn(this._config.blockPrependor.options);
this._blockAppendor = new this._config.blockAppendor.fn(this._config.blockAppendor.options);
}
_createClass(Block, [{
key: "_activate",
value: function _activate() {
var deactivateParent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
if (this._active) {
throw Error("This block is already active. This is probably a bug in RenderKid itself");
}
if (this._closed) {
throw Error("This block is closed and cannot be activated. This is probably a bug in RenderKid itself");
}
this._active = true;
this._layout._activeBlock = this;
if (deactivateParent) {
if (this._parent != null) {
this._parent._deactivate(false);
}
}
return this;
}
}, {
key: "_deactivate",
value: function _deactivate() {
var activateParent = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
this._ensureActive();
this._flushBuffer();
if (activateParent) {
if (this._parent != null) {
this._parent._activate(false);
}
}
this._active = false;
return this;
}
}, {
key: "_ensureActive",
value: function _ensureActive() {
if (!this._wasOpenOnce) {
throw Error("This block has never been open before. This is probably a bug in RenderKid itself.");
}
if (!this._active) {
throw Error("This block is not active. This is probably a bug in RenderKid itself.");
}
if (this._closed) {
throw Error("This block is already closed. This is probably a bug in RenderKid itself.");
}
}
}, {
key: "_open",
value: function _open() {
if (this._wasOpenOnce) {
throw Error("Block._open() has been called twice. This is probably a RenderKid bug.");
}
this._wasOpenOnce = true;
if (this._parent != null) {
this._parent.write(this._whatToPrependToBlock());
}
this._activate();
return this;
}
}, {
key: "close",
value: function close() {
this._deactivate();
this._closed = true;
if (this._parent != null) {
this._parent.write(this._whatToAppendToBlock());
}
return this;
}
}, {
key: "isOpen",
value: function isOpen() {
return this._wasOpenOnce && !this._closed;
}
}, {
key: "write",
value: function write(str) {
this._ensureActive();
if (str === '') {
return;
}
str = String(str);
this._buffer += str;
return this;
}
}, {
key: "openBlock",
value: function openBlock(config, name) {
var block;
this._ensureActive();
block = new Block(this._layout, this, config, name);
block._open();
return block;
}
}, {
key: "_flushBuffer",
value: function _flushBuffer() {
var str;
if (this._buffer === '') {
return;
}
str = this._buffer;
this._buffer = '';
this._writeInline(str);
}
}, {
key: "_toPrependToLine",
value: function _toPrependToLine() {
var fromParent;
fromParent = '';
if (this._parent != null) {
fromParent = this._parent._toPrependToLine();
}
return this._linePrependor.render(fromParent);
}
}, {
key: "_toAppendToLine",
value: function _toAppendToLine() {
var fromParent;
fromParent = '';
if (this._parent != null) {
fromParent = this._parent._toAppendToLine();
}
return this._lineAppendor.render(fromParent);
}
}, {
key: "_whatToPrependToBlock",
value: function _whatToPrependToBlock() {
return this._blockPrependor.render();
}
}, {
key: "_whatToAppendToBlock",
value: function _whatToAppendToBlock() {
return this._blockAppendor.render();
}
}, {
key: "_writeInline",
value: function _writeInline(str) {
var i, j, k, l, lineBreaksToAppend, m, ref, ref1, ref2, remaining; // special characters (such as <bg-white>) don't require
// any wrapping...
if (new SpecialString(str).isOnlySpecialChars()) {
// ... and directly get appended to the layout.
this._layout._append(str);
return;
} // we'll be removing from the original string till it's empty
remaining = str; // we might need to add a few line breaks at the end of the text.
lineBreaksToAppend = 0; // if text starts with line breaks...
if (m = remaining.match(/^\n+/)) {
// ... we want to write the exact same number of line breaks
// to the layout.
for (i = j = 1, ref = m[0].length; 1 <= ref ? j <= ref : j >= ref; i = 1 <= ref ? ++j : --j) {
this._writeLine('');
}
remaining = remaining.substr(m[0].length, remaining.length);
} // and if the text ends with line breaks...
if (m = remaining.match(/\n+$/)) {
// we want to write the exact same number of line breaks
// to the end of the layout.
lineBreaksToAppend = m[0].length;
remaining = remaining.substr(0, remaining.length - m[0].length);
} // now let's parse the body of the text:
while (remaining.length > 0) {
// anything other than a break line...
if (m = remaining.match(/^[^\n]+/)) {
// ... should be wrapped as a block of text.
this._writeLine(m[0]);
remaining = remaining.substr(m[0].length, remaining.length); // for any number of line breaks we find inside the text...
} else if (m = remaining.match(/^\n+/)) {
// ... we write one less break line to the layout.
for (i = k = 1, ref1 = m[0].length; 1 <= ref1 ? k < ref1 : k > ref1; i = 1 <= ref1 ? ++k : --k) {
this._writeLine('');
}
remaining = remaining.substr(m[0].length, remaining.length);
}
} // if we had line breaks to append to the layout...
if (lineBreaksToAppend > 0) {
// ... we append the exact same number of line breaks to the layout.
for (i = l = 1, ref2 = lineBreaksToAppend; 1 <= ref2 ? l <= ref2 : l >= ref2; i = 1 <= ref2 ? ++l : --l) {
this._writeLine('');
}
}
} // wraps a line into multiple lines if necessary, adds horizontal margins,
// etc, and appends it to the layout.
}, {
key: "_writeLine",
value: function _writeLine(str) {
var line, lineContent, lineContentLength, remaining, roomLeft, toAppend, toAppendLength, toPrepend, toPrependLength; // we'll be cutting from our string as we go
remaining = new SpecialString(str);
while (true) {
// left margin...
// this will continue until nothing is left of our block.
toPrepend = this._toPrependToLine(); // ... and its length
toPrependLength = new SpecialString(toPrepend).length; // right margin...
toAppend = this._toAppendToLine(); // ... and its length
toAppendLength = new SpecialString(toAppend).length; // how much room is left for content
roomLeft = this._layout._config.terminalWidth - (toPrependLength + toAppendLength); // how much room each line of content will have
lineContentLength = Math.min(this._config.width, roomLeft); // cut line content, only for the amount needed
lineContent = remaining.cut(0, lineContentLength, true); // line will consist of both margins and the content
line = toPrepend + lineContent.str + toAppend; // send it off to layout
this._layout._appendLine(line);
if (remaining.isEmpty()) {
break;
}
}
}
}]);
return Block;
}();
;
self = Block;
Block.defaultConfig = {
blockPrependor: {
fn: require('./block/blockPrependor/Default'),
options: {
amount: 0
}
},
blockAppendor: {
fn: require('./block/blockAppendor/Default'),
options: {
amount: 0
}
},
linePrependor: {
fn: require('./block/linePrependor/Default'),
options: {
amount: 0
}
},
lineAppendor: {
fn: require('./block/lineAppendor/Default'),
options: {
amount: 0
}
},
lineWrapper: {
fn: require('./block/lineWrapper/Default'),
options: {
lineWidth: null
}
},
width: terminalWidth,
prefixRaw: '',
suffixRaw: ''
};
return Block;
}.call(void 0);