UNPKG

hi_viral

Version:

This is my introduction package

76 lines (66 loc) 2.52 kB
#!/usr/bin/env node import https from 'https'; import chalk from 'chalk'; import readline from 'readline'; // Readline interface setup for user input const rl = readline.createInterface({ input: process.stdin, output: process.stdout }); // Function to clear the terminal screen function clearScreen() { process.stdout.write('\x1B[2J\x1B[3J\x1B[H'); } // Function to print a single frame of the progress bar with rocket function printProgressBar(progress, rocketPosition) { const progressBarWidth = 20; const progressBar = '▇'.repeat(Math.floor(progress * progressBarWidth)); const emptySpace = ' '.repeat(progressBarWidth - progressBar.length); let progressBarWithRocket = progressBar.slice(0, rocketPosition) + '🚀' + progressBar.slice(rocketPosition); console.log(chalk.green(`[${progressBarWithRocket}${emptySpace}] ${Math.floor(progress * 100)}%`)); } // Function to update the progress and print the progress bar with rocket function updateProgress(progress) { clearScreen(); const rocketPosition = Math.floor(progress * 20); // 20 is the width of the progress bar printProgressBar(progress, rocketPosition); } // Function to fetch and print the data function fetchData() { const url = 'https://viralvaghela.com/hi.txt'; https.get(url, (response) => { let data = ''; response.on('data', (chunk) => { data += chunk; }); response.on('end', () => { clearScreen(); console.log(chalk.green(data)); rl.close(); // Close the readline interface after displaying the data }); }).on('error', (err) => { console.log(chalk.red('Error: ' + err.message)); }); } // Function to simulate fetching data with progress function simulateFetchData() { const totalTime = 1000; // 10 seconds const interval = 100; // 100 milliseconds let currentTime = 0; const timer = setInterval(() => { const progress = currentTime / totalTime; updateProgress(progress); currentTime += interval; if (currentTime >= totalTime) { clearInterval(timer); fetchData(); } }, interval); // Ask user if they want to stop the fetching process rl.question(chalk.yellow('Press Enter to stop fetching...\n'), (answer) => { clearInterval(timer); fetchData(); }); } // Start fetching data simulateFetchData();