### Generating Random Spinners in JavaScript Source: https://context7.com/sindresorhus/cli-spinners/llms.txt Shows how to use the `randomSpinner` function from `cli-spinners` to get a random spinner configuration. It then demonstrates implementing a basic animation using this random spinner, similar to other examples. Requires Node.js. ```javascript import {randomSpinner} from 'cli-spinners'; import process from 'node:process'; // Get a random spinner const spinner = randomSpinner(); console.log(spinner); // Example output (varies each run): // { // interval: 80, // frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] // } // Use in an animation let frameIndex = 0; const interval = setInterval(() => { const frame = spinner.frames[frameIndex]; process.stdout.write(`\r${frame} Random animation!`); frameIndex = (frameIndex + 1) % spinner.frames.length; }, spinner.interval); // Clean up after 2 seconds setTimeout(() => { clearInterval(interval); process.stdout.write('\r✓ Complete! \n'); }, 2000); ``` -------------------------------- ### Install cli-spinners Package Source: https://github.com/sindresorhus/cli-spinners/blob/main/readme.md Install the cli-spinners package using npm. This command fetches and installs the package and its dependencies into your project. ```sh npm install cli-spinners ``` -------------------------------- ### Listing All Available Spinner Names in JavaScript Source: https://context7.com/sindresorhus/cli-spinners/llms.txt Provides a JavaScript code snippet to enumerate all available spinner names exported by the `cli-spinners` library. It uses `Object.keys()` to get an array of names and then prints them. Requires Node.js. ```javascript import cliSpinners from 'cli-spinners'; // Get all spinner names const spinnerNames = Object.keys(cliSpinners); console.log(`Available spinners (${spinnerNames.length} total):`); console.log(spinnerNames.join(', ')); // Output: dots, dots2, dots3, dots4, dots5, dots6, dots7, dots8, dots9, dots10, // dots11, dots12, dots13, dots14, dots8Bit, dotsCircle, sand, line, line2, // rollingLine, pipe, simpleDots, simpleDotsScrolling, star, star2, flip, // hamburger, growVertical, growHorizontal, balloon, balloon2, noise, bounce, // boxBounce, boxBounce2, binary, triangle, arc, circle, squareCorners, // circleQuarters, circleHalves, squish, toggle, toggle2, toggle3, toggle4, // toggle5, toggle6, toggle7, toggle8, toggle9, toggle10, toggle11, toggle12, // toggle13, arrow, arrow2, arrow3, bouncingBar, bouncingBall, smiley, monkey, // hearts, clock, earth, material, moon, runner, pong, shark, dqpb, weather, // christmas, grenade, point, layer, betaWave, fingerDance, fistBump, // soccerHeader, mindblown, speaker, orangePulse, bluePulse, orangeBluePulse, // timeTravel, aesthetic, dwarfFortress ``` -------------------------------- ### Get a Random Spinner using JavaScript Source: https://github.com/sindresorhus/cli-spinners/blob/main/readme.md Import the 'randomSpinner' function from the cli-spinners library to get a randomly selected spinner. The output demonstrates the structure of a spinner object, similar to accessing a specific one. ```javascript import {randomSpinner} from 'cli-spinners'; console.log(randomSpinner()); /* { interval: 80, frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] } */ ``` -------------------------------- ### Interactive Spinner Demo with Node.js Source: https://context7.com/sindresorhus/cli-spinners/llms.txt Creates an interactive demo that cycles through all available spinners with user control. It uses Node.js built-in modules like 'process' and 'readline', along with the 'log-update' library for flicker-free rendering. User input (Enter key) advances to the next spinner, and Ctrl+C exits the application. ```javascript import process from 'node:process'; import readline from 'node:readline'; import logUpdate from 'log-update'; import cliSpinners from 'cli-spinners'; const spinners = Object.keys(cliSpinners); let frame = 0; let spinner = 0; let frameInterval; let spinnerTimeout; const showNextFrame = () => { const {frames} = cliSpinners[spinners[spinner]]; logUpdate(frames[frame++ % frames.length] + ' ' + spinners[spinner]); }; const showNextSpinner = () => { if (frameInterval) { clearInterval(frameInterval); spinner++; } if (spinner < spinners.length) { const s = cliSpinners[spinners[spinner]]; frameInterval = setInterval(showNextFrame, s.interval); spinnerTimeout = setTimeout(showNextSpinner, Math.max(s.interval * s.frames.length, 1000)); } else { process.exit(0); } }; // Setup keyboard controls readline.emitKeypressEvents(process.stdin); process.stdin.setRawMode(true); process.stdin.on('keypress', (string_, key) => { if (key.ctrl && key.name === 'c') { process.exit(130); } // Press Enter to skip to next spinner if (key.name === 'return' && spinnerTimeout) { clearTimeout(spinnerTimeout); showNextSpinner(); } }); console.log(`${spinners.length} spinners\n`); showNextSpinner(); ``` -------------------------------- ### Implementing a Basic Spinner Animation in JavaScript Source: https://context7.com/sindresorhus/cli-spinners/llms.txt Shows how to create a functional terminal spinner animation using `setInterval` and the `cli-spinners` data. It updates the terminal output in place using `process.stdout.write` and stops after a specified duration. Requires Node.js. ```javascript import process from 'node:process'; import cliSpinners from 'cli-spinners'; // Simple spinner animation (without log-update) const spinner = cliSpinners.dots; let frameIndex = 0; const interval = setInterval(() => { const frame = spinner.frames[frameIndex]; process.stdout.write(`\r${frame} Loading...`); frameIndex = (frameIndex + 1) % spinner.frames.length; }, spinner.interval); // Stop after 3 seconds setTimeout(() => { clearInterval(interval); process.stdout.write('\r✓ Done! \n'); }, 3000); ``` -------------------------------- ### Accessing Specific Spinners in JavaScript Source: https://context7.com/sindresorhus/cli-spinners/llms.txt Demonstrates how to import and access individual spinner configurations (frames and interval) from the `cli-spinners` library in JavaScript. This allows direct use of predefined animations like 'dots', 'line', or 'bouncingBall'. ```javascript import cliSpinners from 'cli-spinners'; // Access the 'dots' spinner const spinner = cliSpinners.dots; console.log(spinner); // Output: // { // interval: 80, // frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] // } // Use different spinners console.log(cliSpinners.line); // { interval: 130, frames: ['-', '\\', '|', '/'] } console.log(cliSpinners.bouncingBall); // { interval: 80, frames: ['( ● )', '( ● )', '( ● )', '( ● )', '( ●)', '( ● )', '( ● )', '( ● )', '( ● )', '(● )'] } ``` -------------------------------- ### Direct JSON Access for Custom Spinner Processing Source: https://context7.com/sindresorhus/cli-spinners/llms.txt Shows how to access the raw spinner data directly from the 'spinners.json' file using dynamic import with the 'json' type. This allows for custom processing, filtering, and sorting of spinners based on their properties like interval speed and frame count. ```javascript import spinners from './spinners.json' with {type: 'json'}; // Direct JSON access console.log(spinners.dots); // { interval: 80, frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] } // Filter spinners by interval speed const fastSpinners = Object.entries(spinners) .filter(([name, config]) => config.interval < 100) .map(([name]) => name); console.log('Fast spinners:', fastSpinners); // Find spinners with most frames const complex = Object.entries(spinners) .sort((a, b) => b[1].frames.length - a[1].frames.length) .slice(0, 5) .map(([name, config]) => ({name, frameCount: config.frames.length})); console.log('Most complex spinners:', complex); ``` -------------------------------- ### TypeScript Integration for Type-Safe Spinners Source: https://context7.com/sindresorhus/cli-spinners/llms.txt Demonstrates how to use TypeScript definitions for type-safe spinner access and autocomplete support. It shows how to access spinner properties like 'interval' and 'frames' and utilize the 'randomSpinner' function. A 'displaySpinner' function is provided for type-safe spinner rendering. ```typescript import cliSpinners, {randomSpinner, Spinner, SpinnerName} from 'cli-spinners'; // Type-safe spinner access const spinnerName: SpinnerName = 'dots'; const spinner: Spinner = cliSpinners[spinnerName]; // Spinner object has typed properties console.log(spinner.interval); // number console.log(spinner.frames); // string[] // randomSpinner returns Spinner type const random: Spinner = randomSpinner(); // Function to display any spinner with type safety function displaySpinner(name: SpinnerName, message: string): void { const spinner = cliSpinners[name]; let index = 0; const interval = setInterval(() => { const frame = spinner.frames[index]; process.stdout.write(`\r${frame} ${message}`); index = (index + 1) % spinner.frames.length; }, spinner.interval); setTimeout(() => { clearInterval(interval); process.stdout.write('\r✓ Done!\n'); }, 3000); } displaySpinner('earth', 'Saving planet...'); ``` -------------------------------- ### Access a Specific Spinner (dots) using JavaScript Source: https://github.com/sindresorhus/cli-spinners/blob/main/readme.md Import the cli-spinners library and access a specific spinner, such as 'dots'. The output shows the structure of a spinner object, including its interval and frames. This is useful for custom spinner implementations. ```javascript import cliSpinners from 'cli-spinners'; console.log(cliSpinners.dots); /* { interval: 80, frames: ['⠋', '⠙', '⠹', '⠸', '⠼', '⠴', '⠦', '⠧', '⠇', '⠏'] } */ ``` -------------------------------- ### Animating Spinners with log-update in JavaScript Source: https://context7.com/sindresorhus/cli-spinners/llms.txt Integrates `cli-spinners` with the `log-update` package to provide flicker-free terminal spinner animations. It allows selecting a spinner by command-line argument or defaults to 'dots'. Requires Node.js and `log-update` package. ```javascript import logUpdate from 'log-update'; import cliSpinners from 'cli-spinners'; // Choose spinner from command line argument or default to 'dots' const spinnerName = process.argv[2] || 'dots'; const spinner = cliSpinners[spinnerName]; let index = 0; setInterval(() => { const {frames} = spinner; logUpdate(frames[index = ++index % frames.length] + ' Processing unicorns...'); }, spinner.interval); // Usage: node script.js dots // Usage: node script.js arrow // Usage: node script.js earth ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.