2017-06-30 12:42:24 +02:00
|
|
|
'use strict';
|
2017-06-29 16:00:51 -07:00
|
|
|
const chalk = require('..');
|
|
|
|
|
|
2018-10-27 16:09:54 +02:00
|
|
|
const ignoreChars = /[^!-~]/g;
|
2017-06-29 16:00:51 -07:00
|
|
|
|
2020-10-04 10:56:24 +13:00
|
|
|
const delay = milliseconds => new Promise(resolve => {
|
|
|
|
|
setTimeout(resolve, milliseconds);
|
|
|
|
|
});
|
2019-03-12 20:11:31 +07:00
|
|
|
|
|
|
|
|
function rainbow(string, offset) {
|
|
|
|
|
if (!string || string.length === 0) {
|
|
|
|
|
return string;
|
2017-06-29 16:00:51 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-12 20:11:31 +07:00
|
|
|
const hueStep = 360 / string.replace(ignoreChars, '').length;
|
2017-06-29 16:00:51 -07:00
|
|
|
|
|
|
|
|
let hue = offset % 360;
|
2019-03-12 20:11:31 +07:00
|
|
|
const characters = [];
|
|
|
|
|
for (const character of string) {
|
|
|
|
|
if (character.match(ignoreChars)) {
|
|
|
|
|
characters.push(character);
|
2017-06-29 16:00:51 -07:00
|
|
|
} else {
|
2019-03-12 20:11:31 +07:00
|
|
|
characters.push(chalk.hsl(hue, 100, 50)(character));
|
2017-06-29 16:00:51 -07:00
|
|
|
hue = (hue + hueStep) % 360;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-12 20:11:31 +07:00
|
|
|
return characters.join('');
|
2017-06-29 16:00:51 -07:00
|
|
|
}
|
|
|
|
|
|
2019-03-12 20:11:31 +07:00
|
|
|
async function animateString(string) {
|
2017-06-29 16:00:51 -07:00
|
|
|
console.log();
|
2017-06-29 16:05:28 -07:00
|
|
|
for (let i = 0; i < 360 * 5; i++) {
|
2019-03-12 20:11:31 +07:00
|
|
|
console.log('\u001B[1F\u001B[G', rainbow(string, i));
|
|
|
|
|
await delay(2); // eslint-disable-line no-await-in-loop
|
2017-06-29 16:00:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-03-12 20:11:31 +07:00
|
|
|
(async () => {
|
|
|
|
|
console.log();
|
|
|
|
|
await animateString('We hope you enjoy Chalk! <3');
|
|
|
|
|
console.log();
|
|
|
|
|
})();
|