-
-
Notifications
You must be signed in to change notification settings - Fork 848
/
rainbow.js
39 lines (31 loc) 路 835 Bytes
/
rainbow.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
const chalk = require('..');
const ignoreChars = /[^!-~]/;
function rainbow(str, offset) {
if (!str || str.length === 0) {
return str;
}
const hueStep = 360 / str.replace(ignoreChars, '').length;
let hue = offset % 360;
const chars = [];
for (const c of str) {
if (c.match(ignoreChars)) {
chars.push(c);
} else {
chars.push(chalk.hsl(hue, 100, 50)(c));
hue = (hue + hueStep) % 360;
}
}
return chars.join('');
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function animateString(str) {
console.log();
for (let i = 0; i < 360 * 5; i++) {
console.log('\x1b[1F\x1b[G ', rainbow(str, i));
await sleep(50); // eslint-disable-line no-await-in-loop
}
}
console.log();
animateString('We hope you enjoy the new version of Chalk 2! <3').then(() => console.log());