• Stars
    star
    2,913
  • Rank 14,935 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created about 13 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Flexible ascii progress bar for nodejs

Flexible ascii progress bar.

Installation

$ npm install progress

Usage

First we create a ProgressBar, giving it a format string as well as the total, telling the progress bar when it will be considered complete. After that all we need to do is tick() appropriately.

var ProgressBar = require('progress');

var bar = new ProgressBar(':bar', { total: 10 });
var timer = setInterval(function () {
  bar.tick();
  if (bar.complete) {
    console.log('\ncomplete\n');
    clearInterval(timer);
  }
}, 100);

Options

These are keys in the options object you can pass to the progress bar along with total as seen in the example above.

  • curr current completed index
  • total total number of ticks to complete
  • width the displayed width of the progress bar defaulting to total
  • stream the output stream defaulting to stderr
  • head head character defaulting to complete character
  • complete completion character defaulting to "="
  • incomplete incomplete character defaulting to "-"
  • renderThrottle minimum time between updates in milliseconds defaulting to 16
  • clear option to clear the bar on completion defaulting to false
  • callback optional function to call when the progress bar completes

Tokens

These are tokens you can use in the format of your progress bar.

  • :bar the progress bar itself
  • :current current tick number
  • :total total ticks
  • :elapsed time elapsed in seconds
  • :percent completion percentage
  • :eta estimated completion time in seconds
  • :rate rate of ticks per second

Custom Tokens

You can define custom tokens by adding a {'name': value} object parameter to your method (tick(), update(), etc.) calls.

var bar = new ProgressBar(':current: :token1 :token2', { total: 3 })
bar.tick({
  'token1': "Hello",
  'token2': "World!\n"
})
bar.tick(2, {
  'token1': "Goodbye",
  'token2': "World!"
})

The above example would result in the output below.

1: Hello World!
3: Goodbye World!

Examples

Download

In our download example each tick has a variable influence, so we pass the chunk length which adjusts the progress bar appropriately relative to the total length.

var ProgressBar = require('progress');
var https = require('https');

var req = https.request({
  host: 'download.github.com',
  port: 443,
  path: '/visionmedia-node-jscoverage-0d4608a.zip'
});

req.on('response', function(res){
  var len = parseInt(res.headers['content-length'], 10);

  console.log();
  var bar = new ProgressBar('  downloading [:bar] :rate/bps :percent :etas', {
    complete: '=',
    incomplete: ' ',
    width: 20,
    total: len
  });

  res.on('data', function (chunk) {
    bar.tick(chunk.length);
  });

  res.on('end', function () {
    console.log('\n');
  });
});

req.end();

The above example result in a progress bar like the one below.

downloading [=====             ] 39/bps 29% 3.7s

Interrupt

To display a message during progress bar execution, use interrupt()

var ProgressBar = require('progress');

var bar = new ProgressBar(':bar :current/:total', { total: 10 });
var timer = setInterval(function () {
  bar.tick();
  if (bar.complete) {
    clearInterval(timer);
  } else if (bar.curr === 5) {
      bar.interrupt('this message appears above the progress bar\ncurrent progress is ' + bar.curr + '/' + bar.total);
  }
}, 1000);

You can see more examples in the examples folder.

License

MIT

More Repositories

1

page.js

Micro client-side router inspired by the Express router
JavaScript
7,621
star
2

move.js

CSS3 backed JavaScript animation framework
JavaScript
4,712
star
3

masteringnode

Open source eBook for nodejs - written w/ markdown, outputs to various formats (pdf, mobi, epub, html, etc)
JavaScript
1,834
star
4

express-resource

Resourceful routing for Express
JavaScript
1,414
star
5

uikit

UIKit - modern ui components for the modern web
JavaScript
1,320
star
6

deploy

Minimalistic deployment shell script
Shell
1,148
star
7

screenshot-app

website screenshot service & app - powered by node.js, express, redis, node-canvas, phantomjs and more
JavaScript
850
star
8

expresso

use mocha
JavaScript
760
star
9

bytes.js

node byte string parser
JavaScript
440
star
10

node-jscoverage

jscoverage modified to support SSJS
C++
400
star
11

batch

Simple async batch with concurrency control and progress reporting
JavaScript
318
star
12

stack

Minimalist provisioning tool written in Go.
Go
310
star
13

express-messages

Express flash notification message rendering
JavaScript
261
star
14

google-search

Ruby - Google Search API
Ruby
258
star
15

express-params

Express param pre-condition functions
JavaScript
146
star
16

stats

JavaScript source statistics with simple API & CLI written with node.js
JavaScript
78
star
17

histogram

JavaScript canvas histogram component
JavaScript
68
star
18

go-debug

Old anti-pattern go-debug thing
Go
56
star
19

minstache

Mini mustache compiling to stand-alone functions
JavaScript
53
star
20

go-hpc

HTTP RPC codec for Gorilla RPC v2.
Go
29
star
21

connect-render

Template Render helper for connect: res.render(view, options).
JavaScript
17
star
22

readme

repos are now at /tj/*
1
star