Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

human readable RATE and ETA #196

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
33 changes: 30 additions & 3 deletions lib/node-progress.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,9 @@ ProgressBar.prototype.render = function (tokens, force) {
.replace(':current', this.curr)
.replace(':total', this.total)
.replace(':elapsed', isNaN(elapsed) ? '0.0' : (elapsed / 1000).toFixed(1))
.replace(':eta', (isNaN(eta) || !isFinite(eta)) ? '0.0' : (eta / 1000)
.toFixed(1))
.replace(':eta', this.humanETA(eta))
.replace(':percent', percent.toFixed(0) + '%')
.replace(':rate', Math.round(rate));
.replace(':rate', this.humanFileSize(rate));

/* compute the available space (non-zero) for the bar */
var availableSpace = Math.max(0, this.stream.columns - str.replace(':bar', '').length);
Expand Down Expand Up @@ -234,3 +233,31 @@ ProgressBar.prototype.terminate = function () {
this.stream.write('\n');
}
};

ProgressBar.prototype.humanFileSize = function(size) {
var i = size == 0 ? 0 : Math.floor( Math.log(size) / Math.log(1024) );
return ( size / Math.pow(1024, i) ).toFixed(2) + ' ' + ['B', 'kB', 'MB', 'GB', 'TB'][i] + '/s';
}

ProgressBar.prototype.humanETA = function(ms) {
if(!isFinite(ms)) return 0+'ms'
if (ms < 0) ms = -ms;
if(ms===0) return 'done'
if(ms<1000) return parseInt(ms)+'ms'
const time = {
day: Math.floor(ms / 86400000),
hour: Math.floor(ms / 3600000) % 24,
minute: Math.floor(ms / 60000) % 60,
s: Math.floor(ms / 1000) % 60
};
return Object.entries(time)
.filter(val => val[1] !== 0)
.map(val => {
if(val[0] !== 's') {
return val[1] + ' ' + (val[1] !== 1 ? val[0] + 's' : val[0])
} else {
return val[1] + 's'
}
})
.join(' ');
};