Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions lib/node-progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ function ProgressBar(fmt, options) {
this.curr = options.curr || 0;
this.total = options.total;
this.width = options.width || this.total;
this.clear = options.clear
this.clear = options.clear;
this.smooth = options.smooth || 0;
this.chars = {
complete : options.complete || '=',
incomplete : options.incomplete || '-',
Expand Down Expand Up @@ -154,13 +155,37 @@ ProgressBar.prototype.render = function (tokens) {

/* TODO: the following assumes the user has one ':bar' token */
completeLength = Math.round(width * ratio);
complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);
incomplete = Array(Math.max(0, width - completeLength + 1)).join(this.chars.incomplete);

/* add head to the complete string */
if(completeLength > 0)
complete = complete.slice(0, -1) + this.chars.head;

if (this.smooth) //smooth overwrites complete,incomplete,head (unicode)
{
completeLength = parseInt(width * ratio);
this.chars.complete = '█';
this.chars.incomplete = ' ';

switch (parseInt(((this.curr-(completeLength*(this.total/width)))/((this.total/width)/8))))
{
case 0: this.chars.head="▏";break;
case 1: this.chars.head="▎";break;
case 2: this.chars.head="▍";break;
case 3: this.chars.head="▌";break;
case 4: this.chars.head="▋";break;
case 5: this.chars.head="▊";break;
case 6: this.chars.head="▉";break;
case 7: this.chars.head="█";break;
}
complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);
incomplete = Array(Math.max(0, width - completeLength + 1)).join(this.chars.incomplete);

complete += this.chars.head;
} else
{
complete = Array(Math.max(0, completeLength + 1)).join(this.chars.complete);
incomplete = Array(Math.max(0, width - completeLength + 1)).join(this.chars.incomplete);
/* add head to the complete string */
if(completeLength > 0)
complete = complete.slice(0, -1) + this.chars.head;
}

/* fill in the actual progress bar */
str = str.replace(':bar', complete + incomplete);

Expand Down