Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (125 loc) · 4.55 KB
/
Copy pathindex.js
File metadata and controls
152 lines (125 loc) · 4.55 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
/*
|¯¨´ `¨¯| .-----------------------.
\ ` ´ / | This script |
\ ` ´ / | will output |
' `´ ' | something like this |
/ ´ ` \ | dummy-team.github.io |
| dummy `| '-----------------------'
`~------~´
*/
'use strict';
var chalk = require('chalk');
var pad = require('pad-component');
var wrap = require('word-wrap');
var stringWidth = require('string-width');
var stripAnsi = require('strip-ansi');
var ansiStyles = require('ansi-styles');
var ansiRegex = require('ansi-regex')();
var repeating = require('repeating');
var topOffset = 3;
var leftOffset = 17;
var defaultGreeting =
'\n __ ' +
'\n |¯¨´ `¨¯| ' +
'\n \\ ' + chalk.blue('`') + ' ' + chalk.blue('´') + ' / ' +
'\n \\ ' + chalk.blue('`') + ' ' + chalk.blue('´') + ' / ' +
'\n \\ ' + chalk.blue('`') + chalk.blue('´') + ' / ' +
'\n / ' + chalk.blue('´') + ' ' + chalk.blue('`') + ' \\ ' +
'\n | ' + chalk.blue('dummy') + ' ' + chalk.blue('`') + '| ' +
'\n `~------~´';
module.exports = function (message, options) {
message = (message || 'Welcome to the Dummy-team, ladies and gentlemen!').trim();
options = options || {};
/*
* What you're about to see may confuse you. And rightfully so. Here's an
* explanation.
*
* When yosay is given a string, we create a duplicate with the ansi styling
* sucked out. This way, the true length of the string is read by `pad` and
* `wrap`, so they can correctly do their job without getting tripped up by
* the "invisible" ansi. Along with the duplicated, non-ansi string, we store
* the character position of where the ansi was, so that when we go back over
* each line that will be printed out in the message box, we check the
* character position to see if it needs any styling, then re-insert it if
* necessary.
*
* Better implementations welcome :)
*/
var maxLength = 24;
var frame;
var styledIndexes = {};
var completedString = '';
var regExNewLine;
if (options.maxLength) {
maxLength = stripAnsi(message).toLowerCase().split(' ').sort()[0].length;
if (maxLength < options.maxLength) {
maxLength = options.maxLength;
}
}
regExNewLine = new RegExp('\\s{' + maxLength + '}');
frame = {
top: '.' + repeating('-', maxLength + 2) + '.',
side: ansiStyles.reset.open + '|' + ansiStyles.reset.open,
bottom: ansiStyles.reset.open + '\'' + repeating('-', maxLength + 2) + '\''
};
message.replace(ansiRegex, function (match, offset) {
Object.keys(styledIndexes).forEach(function (key) {
offset -= styledIndexes[key].length;
});
styledIndexes[offset] = styledIndexes[offset] ? styledIndexes[offset] + match : match;
});
return wrap(stripAnsi(message), { width: maxLength })
.split(/\n/)
.reduce(function (greeting, str, index, array) {
var paddedString;
if (!regExNewLine.test(str)) {
str = str.trim();
}
completedString += str;
str = completedString
.substr(completedString.length - str.length)
.replace(/./g, function (char, charIndex) {
if (index > 0) {
charIndex += completedString.length - str.length + index;
}
var hasContinuedStyle = 0;
var continuedStyle;
Object.keys(styledIndexes).forEach(function (offset) {
if (charIndex > offset) {
hasContinuedStyle++;
continuedStyle = styledIndexes[offset];
}
if (hasContinuedStyle === 1 && charIndex < offset) {
hasContinuedStyle++;
}
});
if (styledIndexes[charIndex]) {
return styledIndexes[charIndex] + char;
} else if (hasContinuedStyle >= 2) {
return continuedStyle + char;
} else {
return char;
}
})
.trim();
paddedString = pad({
length: stringWidth(str),
valueOf: function () {
return ansiStyles.reset.open + str + ansiStyles.reset.open;
}
}, maxLength);
if (index === 0) {
greeting[topOffset - 1] += frame.top;
}
greeting[index + topOffset] =
(greeting[index + topOffset] || pad.left('', leftOffset)) +
frame.side + ' ' + paddedString + ' ' + frame.side;
if (array.length === index + 1) {
greeting[index + topOffset + 1] =
(greeting[index + topOffset + 1] || pad.left('', leftOffset)) +
frame.bottom;
}
return greeting;
}, defaultGreeting.split(/\n/))
.join('\n') + '\n';
};