npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

status-bar

v2.0.3

Published

A status bar for file transfers

Downloads

2,179

Readme

status-bar

A status bar for file transfers

npm david

Example

var path = require('path');
var http = require('http');
var statusBar = require('status-bar');

var url = 'http://nodejs.org/dist/latest/node.exe';
var bar;

http.get(url, function (res) {
  bar = statusBar.create({ total: res.headers['content-length'] })
      .on('render', function (stats) {
        process.stdout.write(
            path.basename(url) + ' ' +
            this.format.storage(stats.currentSize) + ' ' +
            this.format.speed(stats.speed) + ' ' +
            this.format.time(stats.elapsedTime) + ' ' +
            this.format.time(stats.remainingTime) + ' [' +
            this.format.progressBar(stats.percentage) + '] ' +
            this.format.percentage(stats.percentage));
        process.stdout.cursorTo(0);
      });
  
  res.pipe(bar);
}).on('error', function (err) {
  if (bar) bar.cancel();
  console.error(err);
});

/*
It will print something like this:

node.exe    2.8 MiB  617.5K/s 00:06 00:07 [############············]  51%
*/

Why you should try this module

  • It doesn't print anything, it just calculates and returns raw data and provides default formatting functions.
  • The status bar can be displayed wherever you want, it is simply a string, so you can render it in the console, in HTML (probably with your own progress bar) via websockets or NW.js, etc.
  • You decide how to format and arrange the elements. The default formatting functions have a fixed length, so you can format the status bar very easily.
  • It is very easy to use. Just pipe() things to it!

Render function examples

  • pacman from Arch Linux:

    a-file                  21.8 MiB   67.9M/s 00:03 [#####···················]  22%
    var statusBar = require('status-bar');
      
    var formatFilename = function (filename) {
      //80 - 59
      var filenameMaxLength = 21;
      if (filename.length > filenameMaxLength) {
        filename = filename.slice(0, filenameMaxLength - 3) + '...';
      } else {
        var remaining = filenameMaxLength - filename.length;
        while (remaining--) {
          filename += ' ';
        }
      }
      return filename;
    };
      
    filename = formatFilename(filename);
      
    var bar = statusBar.create({ total: ... })
        .on('render', function (stats) {
          process.stdout.write(filename + ' ' + 
              this.format.storage(stats.currentSize) + ' ' +
              this.format.speed(stats.speed) + ' ' +
              this.format.time(stats.remainingTime) + ' [' +
              this.format.progressBar(stats.percentage) + '] ' +
              this.format.percentage(stats.percentage));
          process.stdout.cursorTo(0);
        });
          
    readableStream.pipe(bar);
  • git clone:

    Receiving objects: 18% (56655992/311833402), 54.0 MiB | 26.7M/s
    var statusBar = require('status-bar');
      
    var bar = statusBar.create({ total: ...})
        .on('render', function (stats) {
          process.stdout.write('Receiving objects: ' +
              this.format.percentage(stats.percentage).trim() +
              ' (' + stats.currentSize + '/' + stats.totalSize + '), ' +
              this.format.storage(stats.currentSize).trim() + ' | ' +
              this.format.speed(stats.speed).trim());
          process.stdout.cursorTo(0);
        });
      
    readableStream.pipe(bar);

Functions

Objects


module.create(options) : StatusBar

Returns a new StatusBar instance.

Options:

  • frequency - Number
    The rendering frequency in milliseconds. It must be a positive value. Default is 200.
  • progressBarComplete - String
    The character that shows completion progress. Default is #.
  • progressBarIncomplete - String
    The character that shows the remaining progress. Default is ·.
  • progressBarLength - Number
    The length of the progress bar. Default is 24.
  • total - Number
    The total size of the file. This option is required.

StatusBar

The StatusBar object inherits from a writable stream.

Events

Methods

Properties


finish

Arguments: none.

Emitted when the transfer finishes.

hangup

Arguments: none.

Emitted when the transfer hangs up (3 seconds without receiving data). Can be emitted multiple times.

render

Arguments: stats.

Emitted when the status bar needs to be rendered. All the properties of the stats object contain raw data so you need to format them. You can use the default formatting functions.

Stats:

  • currentSize - Number
    The current size in bytes.
  • remainingSize - Number
    The remaining size in bytes.
  • totalSize - Number
    The total size in bytes.
  • percentage - Number
    The progress percentage (current/total size). A number between 0 and 1.
  • speed - Number
    The current speed in bytes per second.
  • elapsedTime - Number
    The elapsed time in seconds.
  • remainingTime - Number
    The estimated remaining time in seconds. If the remaining time cannot be estimated because the status bar needs at least 2 chunks of data or because the transfer it's hung up, it returns undefined.

StatusBar#cancel() : undefined

When you need to cancel the rendering of the status bar because the transfer has been aborted due to an error or any other reason, call to this function to clear the internal timers.


StatusBar#format : Formatter

Returns a Formatter instance.


Formatter

Methods


Formatter#percentage(percentage) : String

The percentage must be a number between 0 and 1. Result string length: 4.

console.log(this.format.percentage(0.5));
/*
50%
 */

Formatter#progressBar(percentage) : String

The percentage must be a number between 0 and 1. Result string length: the length configured with the option progressBarLength.

console.log(this.format.progressBar(0.06));
/*
#·······················
 */

Formatter#speed(bytesPerSecond[, decimals]) : String

By default it shows 1 decimal. Result string length: 8 + #decimals.

console.log(this.format.speed(30098226));
/*
  30.1M/s
 */

Formatter#storage(bytes[, decimals]) : String

By default it shows 1 decimal. Result string length: 9 + #decimals.

console.log(this.format.storage(38546744));
/*
  36.8 MiB
 */

Formatter#time(seconds) : String

Result string length: 5 (min:sec). If seconds is undefined it prints --:--.

console.log(this.format.time(63));
/*
01:03
 */