diff --git a/lib/node-progress.js b/lib/node-progress.js index 8eb0740..b4d7d83 100644 --- a/lib/node-progress.js +++ b/lib/node-progress.js @@ -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); @@ -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(' '); + };