Console Examples
The ASCII Progress Bar can be used in Node.js console applications. Here are several examples showing different ways to use it.
Basic Usage
The simplest way to use the progress bar is to import the AsciiProgressRenderer
and call its render method:
import { AsciiProgressRenderer } from '@yacosta738/ascii-progress-bar';
console.log(AsciiProgressRenderer.render(75, 'default')); // ■■■■■■■□□□ 75%
Available Patterns
The library comes with several built-in patterns:
// Default patternconsole.log(AsciiProgressRenderer.render(75, 'default')); // ■■■■■■■□□□ 75%
// Dots patternconsole.log(AsciiProgressRenderer.render(60, 'dots')); // oooooooooo.......... 60%
// Stars patternconsole.log(AsciiProgressRenderer.render(40, 'stars')); // **** 40%
// Hashes patternconsole.log(AsciiProgressRenderer.render(50, 'hashes')); // ##### 50%
// Braille patternconsole.log(AsciiProgressRenderer.render(80, 'braille')); // ⣿⣿⣿⣿⣿⣿⣀⣀ 80%
// Minimal patternconsole.log(AsciiProgressRenderer.render(50, 'minimal')); // ▰▰▱▱▱ 50%
// Blocks patternconsole.log(AsciiProgressRenderer.render(60, 'blocks')); // ▮▮▮▮▮▮▯▯▯▯ 60%
Animated Progress Bar
Here’s an example of how to create an animated progress bar in the console:
import { AsciiProgressRenderer } from '@yacosta738/ascii-progress-bar';
// Helper function to simulate delayconst sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
async function animateProgress() { for (let i = 0; i <= 100; i += 5) { // Clear previous line process.stdout.clearLine(); process.stdout.cursorTo(0); // Render the bar process.stdout.write(AsciiProgressRenderer.render(i, 'blocks')); await sleep(200); } console.log('\n');}
animateProgress();
Custom Patterns
You can create your own patterns using the addPattern
method:
import { AsciiProgressRenderer } from '@yacosta738/ascii-progress-bar';
// Add custom patternAsciiProgressRenderer.addPattern('custom', { empty: '○', filled: '●', length: 15});
// Use custom patternconsole.log(AsciiProgressRenderer.render(70, 'custom')); // ●●●●●●●●●●○○○○○ 70%
Tips for Console Usage
- Use
process.stdout.clearLine()
andprocess.stdout.cursorTo(0)
for smooth animations - For real-time progress updates, consider using
\r
to return to the start of the line - The progress value should be between 0 and 100
- If an invalid pattern is specified, it will fall back to the default pattern
Error Handling
The renderer handles invalid inputs gracefully:
// Invalid progress (>100) falls back to 100%console.log(AsciiProgressRenderer.render(150, 'default')); // ■■■■■■■■■■ 100%
// Invalid pattern falls back to defaultconsole.log(AsciiProgressRenderer.render(50, 'invalid')); // ■■■■■□□□□□ 50%