From c0155688cf57edb6bee5b97dcc3548f47454e645 Mon Sep 17 00:00:00 2001 From: Josh Junon Date: Thu, 29 Jun 2017 16:00:51 -0700 Subject: [PATCH] add rainbow example --- examples/rainbow.js | 39 ++++++++++++++++++++++++++++ example.js => examples/screenshot.js | 5 ++-- 2 files changed, 42 insertions(+), 2 deletions(-) create mode 100644 examples/rainbow.js rename example.js => examples/screenshot.js (68%) diff --git a/examples/rainbow.js b/examples/rainbow.js new file mode 100644 index 0000000..c4ed456 --- /dev/null +++ b/examples/rainbow.js @@ -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()); diff --git a/example.js b/examples/screenshot.js similarity index 68% rename from example.js rename to examples/screenshot.js index e2bf751..7d195a6 100644 --- a/example.js +++ b/examples/screenshot.js @@ -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') {