-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogressbar.js
67 lines (54 loc) · 2.08 KB
/
progressbar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const { SingleBar } = require('cli-progress');
const readline = require('readline');
const { spawn } = require('child_process');
const progressBarWidth = 40; // Width of the progress bar
const animationFrames = [' ', '█ ', '██ ', '███ ', ' ███', ' ██', ' █', ' ']; // Animation frames
let currentFrameIndex = 0; // Current animation frame index
const total = 100; // Total number of items (arbitrary value for demonstration)
let progress = 0; // Current progress
// Create a readline interface
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Hide the cursor
rl.output.write('\x1B[?25l');
const progressBar = new SingleBar({
format: `Progress {bar} {percentage}% | {value}/{total}`,
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
hideCursor: false
});
// Function to update the progress bar
const updateProgressBar = () => {
const animationFrame = animationFrames[currentFrameIndex];
const progressRatio = progress % progressBarWidth;
const filledBlocks = '█'.repeat(progressRatio);
const emptyBlocks = ' '.repeat(progressBarWidth - progressRatio);
rl.output.write(`\rProgress ${filledBlocks}${emptyBlocks}${animationFrame.slice(-1)}`);
currentFrameIndex = (currentFrameIndex + 1) % animationFrames.length;
progress++;
};
// Start the progress bar
progressBar.start(total, 0);
// Start updating the progress bar every 100 milliseconds
const interval = setInterval(updateProgressBar, 100);
// Spawn the ls command and pipe its output to the progress bar
const ls = spawn('ls', ['-l']);
ls.stdout.on('data', (data) => {
// Process the data received from ls command
// Update the progress bar accordingly
// ...
// In this example, we're incrementing the progress for demonstration purposes
const lines = data.toString().split('\n').filter(Boolean).length;
for (let i = 0; i < lines; i++) {
updateProgressBar();
}
});
ls.on('close', () => {
// Stop the progress bar and show the cursor
progressBar.stop();
rl.output.write('\x1B[?25h');
clearInterval(interval);
process.exit();
});