Get a progress bar as a string
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
example
.gitignore
.npmignore
.travis.yml
LICENSE
README.md
index.js
package.json
test.js

README.md

progress-string

Generate a CLI progress bar as a string that you can then output in any way you like.

Build status js-standard-style

Installation

npm install progress-string --save

Usage

var progress = require('progress-string')
var diff = require('ansi-diff-stream')()

var value = 0
var total = 42
var bar = progress({width: 50, total: total})

setInterval(function () {
  diff.write(
    'The progress of the program is:\n' +
    bar(++value)
  )
  if (value === total) process.exit()
}, 250)

diff.pipe(process.stdout)

API

var bar = progress(options)

This module exposes a function that takes a single options argument and retuns a bar function.

These are the options:

  • total - (integer) The max value of the progress bar
  • width - (integer, default: 42) The width of the progress bar in chars
  • incomplete - (string, default: -) The char used to indicate the incomplete part of the progress bar
  • complete - (string, default: =) The char used to indicate the completed part of the progress bar
  • style - (function, optional) See options.style below for details

options.style

You can provide a custom styling function to style the progress bar returned by the bar function.

It will be called with two arguments: complete and incomplete. Each a string representing its part of the progress bar.

Whatever the style function returns will be returned by the bar function.

var bar = progress({
  width: 10,
  total: 100,
  style: function (complete, incomplete) {
    // add an arrow at the head of the completed part
    return complete + '>' + incomplete
  }
})

console.log(bar(50)) // =====>-----

var str = bar(value)

Call the bar function with the value you want to the generated progress bar to have.

The bar function will return a string representation of the progress bar.

License

MIT