forked from orbit-oss/chalk
add rainbow example
This commit is contained in:
parent
09fb2d8606
commit
c0155688cf
2 changed files with 42 additions and 2 deletions
39
examples/rainbow.js
Normal file
39
examples/rainbow.js
Normal file
|
|
@ -0,0 +1,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(2);
|
||||
}
|
||||
}
|
||||
|
||||
console.log();
|
||||
animateString('We hope you enjoy the new version of Chalk 2! <3').then(() => console.log());
|
||||
|
|
@ -1,8 +1,9 @@
|
|||
'use strict';
|
||||
const chalk = require('.');
|
||||
const chalk = require('..');
|
||||
const styles = require('ansi-styles');
|
||||
|
||||
// Generates screenshot
|
||||
for (const key of Object.keys(chalk.styles)) {
|
||||
for (const key of Object.keys(styles)) {
|
||||
let ret = key;
|
||||
|
||||
if (key === 'reset' || key === 'hidden' || key === 'grey') {
|
||||
Loading…
Add table
Add a link
Reference in a new issue