status-bar
A status bar for file transfers
Example
var path = ;var http = ;var statusBar = ; var url = 'http://nodejs.org/dist/latest/node.exe';var bar; http; /*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 = ;var {//80 - 59var filenameMaxLength = 21;if filenamelength > filenameMaxLengthfilename = filename + '...';elsevar remaining = filenameMaxLength - filenamelength;while remaining--filename += ' ';return filename;};filename = ;var bar = statusBar;readableStream; -
git clone
:Receiving objects: 18% (56655992/311833402), 54.0 MiB | 26.7M/s
var statusBar = ;var bar = statusBar;readableStream;
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.
The StatusBar
object inherits from a writable stream.
Events
Methods
Properties
Arguments: none.
Emitted when the transfer finishes.
Arguments: none.
Emitted when the transfer hangs up (3 seconds without receiving data). Can be emitted multiple times.
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 returnsundefined
.
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.
Returns a Formatter instance.
Methods
- Formatter#percentage(percentage) : String
- Formatter#progressBar(percentage) : String
- Formatter#speed(bytesPerSecond) : String
- Formatter#storage(bytes) : String
- Formatter#time(seconds) : String
Formatter#percentage(percentage) : String
The percentage must be a number between 0 and 1. Result string length: 4.
console;/*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;/*#······················· */
Formatter#speed(bytesPerSecond[, decimals]) : String
By default it shows 1 decimal. Result string length: 8 + #decimals.
console;/* 30.1M/s */
Formatter#storage(bytes[, decimals]) : String
By default it shows 1 decimal. Result string length: 9 + #decimals.
console;/* 36.8 MiB */
Formatter#time(seconds) : String
Result string length: 5 (min:sec). If seconds
is undefined it prints --:--
.
console;/*01:03 */