NPM progress Package


Progress Package for Node.js

Usage

The progress package is a progress bar for Node.js applications. It is easy to use and can be customized to fit your needs.

To use the progress package, first install it using npm:

npm install progress

Once installed, you can use the Progress class to create a progress bar. The Progress class takes two arguments: the total number of ticks and the current number of ticks.

const Progress = require('progress');

const bar = new Progress('[:bar] :percent', { total: 100 });

for (let i = 0; i < 100; i++) {
  bar.tick();
}

The tick() method increments the progress bar by one tick. You can also pass a number to the tick() method to increment the progress bar by a specific number of ticks.

bar.tick(10);

The Progress class also has a number of other methods that you can use to customize the progress bar. For example, you can change the format of the progress bar, the color of the progress bar, and the width of the progress bar.

bar.format = '[:bar] (:percent%)';
bar.color = 'green';
bar.width = 20;

Configuration Options

The Progress class has a number of configuration options that you can use to customize the progress bar. These options are listed in the table below.

| Option | Default Value | Description |
|—|—|—|
| total | 100 | The total number of ticks in the progress bar. |
| current | 0 | The current number of ticks in the progress bar. |
| format | ‘[:bar]’ | The format of the progress bar. |
| color | ‘white’ | The color of the progress bar. |
| width | 20 | The width of the progress bar. |

Best Practices

Here are some best practices for using the progress package:

  • Use the progress package to display the progress of long-running tasks.
  • Customize the progress bar to fit your needs.
  • Use the tick() method to increment the progress bar by a specific number of ticks.
  • Use the render() method to render the progress bar to the console.

Examples

Here are some examples of how to use the progress package:

  • Display the progress of a file download:
const fs = require('fs');
const Progress = require('progress');

const bar = new Progress('downloading [:bar] :percent', { total: 100 });

fs.createReadStream('file.txt')
  .pipe(fs.createWriteStream('file.txt.download'))
  .on('data', function (chunk) {
    bar.tick(chunk.length);
  })
  .on('close', function () {
    bar.render();
    console.log('File downloaded!');
  });
  • Display the progress of a compression task:
const zlib = require('zlib');
const Progress = require('progress');

const bar = new Progress('compressing [:bar] :percent', { total: 100 });

zlib.createGzip()
  .pipe(fs.createWriteStream('file.txt.gz'))
  .on('data', function (chunk) {
    bar.tick(chunk.length);
  })
  .on('close', function () {
    bar.render();
    console.log('File compressed!');
  });
  • Display the progress of a calculation:
const Progress = require('progress');

const bar = new Progress('calculating [:bar] :percent', { total: 100 });

for (let i = 0; i < 100; i++) {
  bar.tick();
}

bar.render();
console.log('Calculation complete!');