-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathMagicPen.js
731 lines (624 loc) · 23.1 KB
/
MagicPen.js
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*global window*/
var utils = require('./utils');
var extend = utils.extend;
var duplicateText = require('./duplicateText');
var rgbRegexp = require('./rgbRegexp');
var cssStyles = require('./cssStyles');
var builtInStyleNames = [
'bold', 'dim', 'italic', 'underline', 'inverse', 'hidden',
'strikeThrough', 'black', 'red', 'green', 'yellow', 'blue',
'magenta', 'cyan', 'white', 'gray', 'bgBlack', 'bgRed',
'bgGreen', 'bgYellow', 'bgBlue', 'bgMagenta', 'bgCyan',
'bgWhite'
];
function MagicPen(options) {
if (!(this instanceof MagicPen)) {
return new MagicPen(options);
}
options = options || {};
if (typeof options === "string") {
options = {format: options };
}
var indentationWidth = 'indentationWidth' in options ?
options.indentationWidth : 2;
this.indentationWidth = Math.max(indentationWidth, 0);
this.indentationLevel = 0;
this.output = [[]];
this.styles = Object.create(null);
this.installedPlugins = [];
// Ready to be cloned individually:
this._themes = {};
Object.keys(MagicPen.serializers).forEach(function (serializerName) {
this._themes[serializerName] = { styles: {} };
}, this);
this.preferredWidth = (typeof process !== 'undefined' && !process.browser && process.stdout.columns) || 80;
if (options.format) {
this.format = options.format;
}
}
if (typeof exports === 'object' && typeof exports.nodeName !== 'string' && require('./supportsColor').stdout) {
MagicPen.defaultFormat = 'ansi';
} else if (typeof window !== 'undefined' && typeof window.navigator !== 'undefined') {
if (window._phantom || window.mochaPhantomJS || (window.__karma__ && window.__karma__.config.captureConsole)) {
MagicPen.defaultFormat = 'ansi'; // colored console
} else {
MagicPen.defaultFormat = 'html'; // Browser
}
} else {
MagicPen.defaultFormat = 'text'; // Plain text
}
MagicPen.prototype.newline = MagicPen.prototype.nl = function (count) {
if (typeof count === 'undefined') {
count = 1;
}
if (count === 0) {
return this;
}
for (var i = 0; i < count; i += 1) {
this.output.push([]);
}
return this;
};
MagicPen.serializers = {};
[
require('./TextSerializer'),
require('./HtmlSerializer'),
require('./AnsiSerializer'),
require('./ColoredConsoleSerializer')
].forEach(function (serializer) {
MagicPen.serializers[serializer.prototype.format] = serializer;
});
function hasSameTextStyling(a, b) {
if (!a || !b || a.style !== 'text' || b.style !== 'text') {
return false;
}
return utils.arrayEquals(a.args.styles, b.args.styles) && a.themes === b.themes;
}
function normalizeLine(line) {
if (line.length === 0) {
return line;
}
var result = [line[0]];
for (var i = 1; i < line.length; i += 1) {
var lastEntry = result[result.length - 1];
var entry = line[i];
if (entry.style === 'text' && entry.args.content === '') {
continue;
}
if (hasSameTextStyling(lastEntry, entry)) {
result[result.length - 1] = {
style: lastEntry.style,
args: {
content: lastEntry.args.content + entry.args.content,
styles: lastEntry.args.styles
},
themes: lastEntry.themes
};
} else {
result.push(entry);
}
}
return result;
}
MagicPen.prototype.write = function (options) {
if (this.styles[options.style]) {
this.styles[options.style].apply(this, options.args);
return this;
}
var lastLine = this.output[this.output.length - 1];
var lastEntry = lastLine[lastLine.length - 1];
if (hasSameTextStyling(lastEntry, options)) {
lastLine[lastLine.length - 1] = {
style: lastEntry.style,
args: {
content: lastEntry.args.content + options.args.content,
styles: lastEntry.args.styles
},
themes: this._themes
};
} else {
lastLine.push(options);
}
return this;
};
MagicPen.prototype.indentLines = function () {
this.indentationLevel += 1;
return this;
};
MagicPen.prototype.indent = MagicPen.prototype.i = function () {
for (var i = 0; i < this.indentationLevel; i += 1) {
this.space(this.indentationWidth);
}
return this;
};
MagicPen.prototype.outdentLines = function () {
this.indentationLevel = Math.max(0, this.indentationLevel - 1);
return this;
};
MagicPen.prototype.addStyle = function (style, handler, allowRedefinition) {
if (this[style] === false || ((this.hasOwnProperty(style) || MagicPen.prototype[style]) && !Object.prototype.hasOwnProperty.call(this.styles, style) && builtInStyleNames.indexOf(style) === -1)) {
throw new Error('"' + style + '" style cannot be defined, it clashes with a built-in attribute');
}
// Refuse to redefine a built-in style or a style already defined directly on this pen unless allowRedefinition is true:
if (this.hasOwnProperty(style) || builtInStyleNames.indexOf(style) !== -1) {
var existingType = typeof this[style];
if (existingType === 'function') {
if (!allowRedefinition) {
throw new Error('"' + style + '" style is already defined, set 3rd arg (allowRedefinition) to true to define it anyway');
}
}
}
if (this._stylesHaveNotBeenClonedYet) {
this.styles = Object.create(this.styles);
this._stylesHaveNotBeenClonedYet = false;
}
this.styles[style] = handler;
this[style] = function () {
handler.apply(this, arguments);
return this;
};
return this;
};
MagicPen.prototype.toString = function (format) {
if (format && this.format && format !== this.format) {
throw new Error('A pen with format: ' + this.format + ' cannot be serialized to: ' + format);
}
format = this.format || format || 'text';
if (format === 'auto') {
format = MagicPen.defaultFormat;
}
var theme = this._themes[format] || {};
var serializer = new MagicPen.serializers[format]();
return serializer.serialize(this.output);
};
MagicPen.prototype.text = function () {
var content = arguments[0];
if (content === '') {
return this;
}
var styles = new Array(arguments.length - 1);
for (var i = 1; i < arguments.length; i += 1) {
styles[i - 1] = arguments[i];
}
content = String(content);
if (content.indexOf('\n') !== -1) {
var lines = content.split(/\n/);
lines.forEach(function (lineContent, index) {
if (lineContent.length) {
this.write({
style: 'text',
args: { content: lineContent, styles: styles },
themes: this._themes
});
}
if (index < lines.length - 1) {
this.nl();
}
}, this);
return this;
} else {
return this.write({
style: 'text',
args: { content: content, styles: styles },
themes: this._themes
});
}
};
MagicPen.prototype.removeFormatting = function () {
var result = this.clone();
this.output.forEach(function (line, index) {
result.output[index] = normalizeLine(line.map(function (outputEntry) {
return outputEntry.style === 'text' ?
{ style: 'text', args: { content: outputEntry.args.content, styles: [] }, themes: outputEntry.themes } :
outputEntry;
}));
});
result.indentationLevel = this.indentationLevel;
return result;
};
MagicPen.prototype.getContentFromArguments = function (args) {
var clone;
if (args[0].isMagicPen) {
this.ensureCompatibleFormat(args[0].format);
return args[0];
} else if (typeof args[0] === 'function') {
clone = this.clone();
args[0].call(clone, clone);
return clone;
} else if (typeof args[0] === 'string' && args.length === 1) {
clone = this.clone();
clone.text(args[0]);
return clone;
} else if (typeof args[0] === 'string') {
clone = this.clone();
clone[args[0]].apply(clone, Array.prototype.slice.call(args, 1));
return clone;
} else {
throw new Error('Requires the arguments to be:\n' +
'a pen or\n' +
'a callback appending content to a pen or\n' +
'a style and arguments for that style or\n' +
'just a string.');
}
};
MagicPen.prototype.isMultiline = function () {
return this.output.length > 1 || this.size().height > 1;
};
MagicPen.prototype.isAtStartOfLine = function () {
return this.output.length === 0 || this.output[this.output.length - 1].length === 0;
};
MagicPen.prototype.isBlock = function () {
return this.output.length === 1 &&
this.output[0].length === 1 &&
this.output[0][0].style === 'block';
};
MagicPen.prototype.ensureCompatibleFormat = function (format) {
if (format && this.format && format !== this.format) {
throw new Error('This pen is only compatible with the format: ' + this.format);
}
};
MagicPen.prototype.block = function () {
var pen = this.getContentFromArguments(arguments);
var blockOutput = pen.output.map(function (line) {
return [].concat(line);
});
return this.write({ style: 'block', args: blockOutput, themes: this._themes });
};
function isRawOutput(options) {
return options &&
typeof options === 'object' &&
typeof options.width === 'number' &&
typeof options.height === 'number' && (
typeof options.content === 'function' ||
typeof options.content === 'string'
);
}
MagicPen.prototype.alt = function (options) {
var format = this.format;
if (!format) {
throw new Error('The alt method is only supported on a pen where the format has already been set');
}
var outputProperty = options[format];
if (typeof outputProperty === 'undefined') {
if (options.fallback) {
return this.append(options.fallback);
} else {
// Nothing to do for this format, just NOOP:
return this;
}
}
if (typeof outputProperty === 'string' || isRawOutput(outputProperty)) {
return this.raw(outputProperty);
} else {
return this.append(outputProperty);
}
};
MagicPen.prototype.raw = function (options) {
var format = this.format;
if (!format) {
throw new Error('The raw method is only supported on a pen where the format has already been set');
}
if (typeof options === 'string') {
return this.write({
style: 'raw',
args: {
height: 0,
width: 0,
content: function () {
return options;
}
},
themes: this._themes
});
}
if (isRawOutput(options)) {
if (typeof options.content === 'string') {
options = extend({}, options);
var content = options.content;
options.content = function () {
return content;
};
}
return this.write({ style: 'raw', args: options, themes: this._themes });
}
throw new Error('Raw ' + this.format + ' content needs to adhere to one of the following forms:\n' +
'a string of raw content\n' +
'a function returning a string of raw content or\n' +
'an object with the following form { width: <number>, height: <number>, content: <string function() {}|string> }');
};
function amend(output, pen) {
var lastLine = output[output.length - 1].slice();
var newOutput = output.slice(0, -1);
var lastEntry = lastLine[lastLine.length - 1];
if (lastEntry && lastEntry.style === 'block') {
lastLine[lastLine.length - 1] = {
style: 'block',
args: amend(lastEntry.args, pen),
themes: pen._themes
};
newOutput[output.length - 1] = lastLine;
} else {
Array.prototype.push.apply(lastLine, pen.output[0]);
newOutput[output.length - 1] = normalizeLine(lastLine);
newOutput.push.apply(newOutput, pen.output.slice(1));
}
return newOutput;
}
MagicPen.prototype.amend = function () {
var pen = this.getContentFromArguments(arguments);
if (pen.isEmpty()) {
return this;
}
this.output = amend(this.output, pen);
return this;
};
MagicPen.prototype.append = function () {
var pen = this.getContentFromArguments(arguments);
if (pen.isEmpty()) {
return this;
}
var lastLine = this.output[this.output.length - 1];
Array.prototype.push.apply(lastLine, pen.output[0]);
this.output[this.output.length - 1] = normalizeLine(lastLine);
this.output.push.apply(this.output, pen.output.slice(1));
return this;
};
MagicPen.prototype.prependLinesWith = function () {
var pen = this.getContentFromArguments(arguments);
if (pen.isEmpty()) {
return this;
}
if (pen.output.length > 1) {
throw new Error('PrependLinesWith only supports a pen with single line content');
}
var height = this.size().height;
var output = this.clone();
output.block(function () {
for (var i = 0; i < height; i += 1) {
if (i > 0) {
this.nl();
}
this.append(pen);
}
});
output.block(this);
this.output = output.output;
return this;
};
MagicPen.prototype.space = MagicPen.prototype.sp = function (count) {
if (count === 0) {
return this;
}
if (typeof count === 'undefined') {
count = 1;
}
return this.text(duplicateText(' ', count));
};
builtInStyleNames.forEach(function (textStyle) {
MagicPen.prototype[textStyle] = MagicPen.prototype[textStyle.toLowerCase()] = function (content) {
return this.text(content, textStyle);
};
});
MagicPen.prototype.clone = function (format) {
if (!this.isEmpty()) {
this.ensureCompatibleFormat(format);
}
function MagicPenClone() {}
MagicPenClone.prototype = this;
var clonedPen = new MagicPenClone();
clonedPen.styles = this.styles;
clonedPen._stylesHaveNotBeenClonedYet = true;
clonedPen.indentationLevel = 0;
clonedPen.output = [[]];
clonedPen.installedPlugins = [];
clonedPen._themes = this._themes;
clonedPen._themesHaveNotBeenClonedYet = true;
clonedPen.format = format || this.format;
clonedPen.parent = this;
return clonedPen;
};
MagicPen.prototype.isMagicPen = true;
MagicPen.prototype.size = function () {
return utils.calculateSize(this.output);
};
MagicPen.prototype.use = function (plugin) {
var existingPlugin = utils.findFirst(this.installedPlugins, function (installedPlugin) {
if (installedPlugin === plugin) {
return true;
} else if (typeof plugin === 'function' && typeof installedPlugin === 'function') {
var pluginName = utils.getFunctionName(plugin);
return pluginName !== '' && pluginName === utils.getFunctionName(installedPlugin);
} else {
return installedPlugin.name === plugin.name;
}
});
if (existingPlugin) {
if (existingPlugin === plugin || (typeof plugin.version !== 'undefined' && plugin.version === existingPlugin.version)) {
// No-op
return this;
} else {
throw new Error("Another instance of the plugin '" + plugin.name + "' " +
"is already installed" +
(typeof existingPlugin.version !== 'undefined' ?
' (version ' + existingPlugin.version +
(typeof plugin.version !== 'undefined' ?
', trying to install ' + plugin.version : '') +
')' : '') +
". Please check your node_modules folder for unmet peerDependencies.");
}
}
if ((typeof plugin !== 'function' && (typeof plugin !== 'object' || typeof plugin.installInto !== 'function')) ||
(typeof plugin.name !== 'undefined' && typeof plugin.name !== 'string') ||
(typeof plugin.dependencies !== 'undefined' && !Array.isArray(plugin.dependencies))) {
throw new Error('Plugins must be functions or adhere to the following interface\n' +
'{\n' +
' name: <an optional plugin name>,\n' +
' version: <an optional semver version string>,\n' +
' dependencies: <an optional list of dependencies>,\n' +
' installInto: <a function that will update the given magicpen instance>\n' +
'}');
}
if (plugin.dependencies) {
var instance = this;
var thisAndParents = [];
do {
thisAndParents.push(instance);
instance = instance.parent;
} while (instance);
var unfulfilledDependencies = plugin.dependencies.filter(function (dependency) {
return !thisAndParents.some(function (instance) {
return instance.installedPlugins.some(function (plugin) {
return plugin.name === dependency;
});
});
});
if (unfulfilledDependencies.length === 1) {
throw new Error(plugin.name + ' requires plugin ' + unfulfilledDependencies[0]);
} else if (unfulfilledDependencies.length > 1) {
throw new Error(plugin.name + ' requires plugins ' +
unfulfilledDependencies.slice(0, -1).join(', ') +
' and ' + unfulfilledDependencies[unfulfilledDependencies.length - 1]);
}
}
this.installedPlugins.push(plugin);
if (typeof plugin === 'function') {
plugin(this);
} else {
plugin.installInto(this);
}
return this; // for chaining
};
MagicPen.prototype.installPlugin = MagicPen.prototype.use; // Legacy alias
function replaceText(output, outputArray, regexp, cb) {
var replacedOutput = output;
outputArray.forEach(function (line, i) {
if (i > 0) {
replacedOutput.nl();
}
line.forEach(function (outputEntry, j) {
if (outputEntry.style === 'block') {
return replacedOutput.output[replacedOutput.output.length - 1].push({
style: 'block',
args: replaceText(output.clone(), outputEntry.args, regexp, cb),
themes: outputEntry.themes
});
} else if (outputEntry.style !== 'text') {
return replacedOutput.output[replacedOutput.output.length - 1].push(outputEntry);
}
if (regexp.global) {
regexp.lastIndex = 0;
}
var m;
var first = true;
var lastIndex = 0;
var text = outputEntry.args.content;
var styles = outputEntry.args.styles;
while ((m = regexp.exec(text)) !== null && (regexp.global || first)) {
if (lastIndex < m.index) {
replacedOutput.text.apply(replacedOutput, [text.substring(lastIndex, m.index)].concat(styles));
}
cb.apply(replacedOutput, [styles].concat(m));
first = false;
lastIndex = m.index + m[0].length;
}
if (lastIndex === 0) {
var lastLine;
if (replacedOutput.output.length === 0) {
lastLine = replacedOutput.output[0] = [];
} else {
lastLine = replacedOutput.output[replacedOutput.output.length - 1];
}
lastLine.push(outputEntry);
} else if (lastIndex < text.length) {
replacedOutput.text.apply(replacedOutput, [text.substring(lastIndex, text.length)].concat(styles));
}
}, this);
}, this);
return replacedOutput.output.map(normalizeLine);
}
MagicPen.prototype.isEmpty = function () {
return this.output.length === 1 && this.output[0].length === 0;
};
MagicPen.prototype.replaceText = function (regexp, cb) {
if (this.isEmpty()) {
return this;
}
if (typeof regexp === 'string') {
regexp = new RegExp(utils.escapeRegExp(regexp), 'g');
}
if (typeof cb === 'string') {
var text = cb;
cb = function (styles, _) {
var args = [text].concat(styles);
this.text.apply(this, args);
};
}
if (arguments.length === 1) {
cb = regexp;
regexp = /.*/;
}
this.output = replaceText(this.clone(), this.output, regexp, cb);
return this;
};
MagicPen.prototype.theme = function (format) {
format = format || this.format;
if (!format) {
throw new Error("Could not detect which format you want to retrieve " +
"theme information for. Set the format of the pen or " +
"provide it as an argument to the theme method.");
}
return this._themes[format];
};
MagicPen.prototype.installTheme = function (formats, theme) {
var that = this;
if (arguments.length === 1) {
theme = formats;
formats = Object.keys(MagicPen.serializers);
}
if (typeof formats === 'string') {
formats = [formats];
}
if (
typeof theme !== 'object' ||
!Array.isArray(formats) ||
formats.some(function (format) {
return typeof format !== 'string';
})
) {
throw new Error("Themes must be installed the following way:\n" +
"Install theme for all formats: pen.installTheme({ comment: 'gray' })\n" +
"Install theme for a specific format: pen.installTheme('ansi', { comment: 'gray' }) or\n" +
"Install theme for a list of formats: pen.installTheme(['ansi', 'html'], { comment: 'gray' })");
}
if (!theme.styles || typeof theme.styles !== 'object') {
theme = {
styles: theme
};
}
if (that._themesHaveNotBeenClonedYet) {
var clonedThemes = {};
Object.keys(that._themes).forEach(function (format) {
clonedThemes[format] = Object.create(that._themes[format]);
});
that._themes = clonedThemes;
that._themesHaveNotBeenClonedYet = false;
}
Object.keys(theme.styles).forEach(function (themeKey) {
if (rgbRegexp.test(themeKey) || cssStyles[themeKey]) {
throw new Error("Invalid theme key: '" + themeKey + "' you can't map build styles.");
}
if (!that[themeKey]) {
that.addStyle(themeKey, function (content) {
this.text(content, themeKey);
});
}
});
formats.forEach(function (format) {
var baseTheme = that._themes[format] || { styles: {} };
var extendedTheme = extend({}, baseTheme, theme);
extendedTheme.styles = extend({}, baseTheme.styles, theme.styles);
that._themes[format] = extendedTheme;
});
return this;
};
module.exports = MagicPen;