2024-01-20 03:36:11 +13:00
|
|
|
import {setTimeout as delay} from 'node:timers/promises';
|
2021-04-22 15:54:42 +12:00
|
|
|
import convertColor from 'color-convert';
|
|
|
|
|
import updateLog from 'log-update';
|
2021-07-30 17:30:19 +02:00
|
|
|
import chalk from '../source/index.js';
|
2017-06-29 16:00:51 -07:00
|
|
|
|
2018-10-27 16:09:54 +02:00
|
|
|
const ignoreChars = /[^!-~]/g;
|
2017-06-29 16:00:51 -07:00
|
|
|
|
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) {
|
2021-04-16 15:23:29 +07:00
|
|
|
if (ignoreChars.test(character)) {
|
2019-03-12 20:11:31 +07:00
|
|
|
characters.push(character);
|
2017-06-29 16:00:51 -07:00
|
|
|
} else {
|
2021-04-22 15:54:42 +12:00
|
|
|
characters.push(chalk.hex(convertColor.hsl.hex(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) {
|
2021-04-16 15:23:29 +07:00
|
|
|
for (let index = 0; index < 360 * 5; index++) {
|
2021-04-22 15:54:42 +12:00
|
|
|
updateLog(rainbow(string, index));
|
2019-03-12 20:11:31 +07:00
|
|
|
await delay(2); // eslint-disable-line no-await-in-loop
|
2017-06-29 16:00:51 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-17 01:56:01 +07:00
|
|
|
console.log();
|
|
|
|
|
await animateString('We hope you enjoy Chalk! <3');
|
|
|
|
|
console.log();
|