-
Notifications
You must be signed in to change notification settings - Fork 43
/
index.js
197 lines (157 loc) · 5.97 KB
/
index.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
import chalk from 'chalk';
import pad from 'pad-component';
import wrapAnsi from 'wrap-ansi';
import stringWidth from 'string-width';
import stripAnsi from 'strip-ansi';
import ansiStyles from 'ansi-styles';
import ansiRegex from 'ansi-regex';
import cliBoxes from 'cli-boxes';
const border = cliBoxes.round;
const leftOffset = 17;
const defaultGreeting
= '\n _-----_ '
+ '\n | | '
+ '\n |' + chalk.red('--(o)--') + '| '
+ '\n `---------´ '
+ '\n ' + chalk.yellow('(') + ' _' + chalk.yellow('´U`') + '_ ' + chalk.yellow(')') + ' '
+ '\n /___A___\\ /'
+ '\n ' + chalk.yellow('| ~ |') + ' '
+ '\n __' + chalk.yellow('\'.___.\'') + '__ '
+ '\n ´ ' + chalk.red('` |') + '° ' + chalk.red('´ Y') + ' ` ';
export default function yosay(message, options = {}) {
message = (message ?? 'Welcome to Yeoman, ladies and gentlemen!').trim();
/*
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 :)
*/
const styledIndexes = {};
let maxLength = 24;
let topOffset = 4;
let completedString = '';
// Amount of characters of the yeoman character »column« → ` /___A___\ /`
const YEOMAN_CHARACTER_WIDTH = 17;
// Amount of characters of the default top frame of the speech bubble → `╭──────────────────────────╮`
const DEFAULT_TOP_FRAME_WIDTH = 28;
// The speech bubble will overflow the Yeoman character if the message is too long.
const MAX_MESSAGE_LINES_BEFORE_OVERFLOW = 7;
// Amount of characters of a total line
let TOTAL_CHARACTERS_PER_LINE = YEOMAN_CHARACTER_WIDTH + DEFAULT_TOP_FRAME_WIDTH;
if (options.maxLength) {
maxLength = stripAnsi(message).toLowerCase().split(' ').sort()[0].length;
if (maxLength < options.maxLength) {
maxLength = options.maxLength;
TOTAL_CHARACTERS_PER_LINE = maxLength + YEOMAN_CHARACTER_WIDTH + topOffset;
}
}
const regexNewline = new RegExp(`\\s{${maxLength}}`);
const borderHorizontal = border.top.repeat(maxLength + 2);
const frame = {
top: border.topLeft + borderHorizontal + border.topRight,
side: ansiStyles.reset.open + border.left + ansiStyles.reset.open,
bottom: ansiStyles.reset.open + border.bottomLeft + borderHorizontal + border.bottomRight,
};
message.replace(ansiRegex(), (match, offset) => {
for (const value of Object.values(styledIndexes)) {
offset -= value.length;
}
styledIndexes[offset] = styledIndexes[offset] ? styledIndexes[offset] + match : match;
});
const strippedMessage = stripAnsi(message);
const spacesIndex = [];
// TODO: Remove `.reduce`.
// eslint-disable-next-line unicorn/no-array-reduce
strippedMessage.split(' ').reduce((accumulator, currentValue) => {
spacesIndex.push(accumulator + currentValue.length);
return spacesIndex.at(-1) + 1;
}, 0);
return wrapAnsi(strippedMessage, maxLength, {hard: true})
.split(/\n/)
// TODO: Remove `.reduce`.
// eslint-disable-next-line unicorn/no-array-reduce
.reduce((greeting, string_, index, array) => {
if (!regexNewline.test(string_)) {
string_ = string_.trim();
}
completedString += string_;
let offset = 0;
for (const element of spacesIndex) {
const character = completedString[element - offset];
if (character) {
if (character !== ' ') {
offset += 1;
}
} else {
break;
}
}
string_ = completedString
.slice(completedString.length - string_.length)
.replaceAll(/./g, (character, characterIndex) => {
characterIndex += completedString.length - string_.length + offset;
let hasContinuedStyle = 0;
let continuedStyle;
for (const offset of Object.keys(styledIndexes)) {
if (characterIndex > offset) {
hasContinuedStyle++;
continuedStyle = styledIndexes[offset];
}
if (hasContinuedStyle === 1 && characterIndex < offset) {
hasContinuedStyle++;
}
}
if (styledIndexes[characterIndex]) {
return styledIndexes[characterIndex] + character;
}
if (hasContinuedStyle >= 2) {
return continuedStyle + character;
}
return character;
})
.trim();
const paddedString = pad({
length: stringWidth(string_),
valueOf() {
return ansiStyles.reset.open + string_ + ansiStyles.reset.open;
},
}, maxLength);
if (index === 0) {
// Need to adjust the top position of the speech bubble depending on the
// amount of lines of the message.
if (array.length === 2) {
topOffset -= 1;
}
if (array.length >= 3) {
topOffset -= 2;
}
// The speech bubble will overflow the Yeoman character if the message
// is too long. So we vertically center the bubble by adding empty lines
// on top of the greeting.
if (array.length > MAX_MESSAGE_LINES_BEFORE_OVERFLOW) {
const emptyLines = Math.ceil((array.length - MAX_MESSAGE_LINES_BEFORE_OVERFLOW) / 2);
for (let i = 0; i < emptyLines; i++) {
greeting.unshift('');
}
frame.top = pad.left(frame.top, TOTAL_CHARACTERS_PER_LINE);
}
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';
}