smooth-progress
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -1,3 +0,3 @@ | ||
'use strict'; | ||
'use strict' | ||
module.exports = require('./lib/progress'); | ||
module.exports = require('./lib/progress') |
@@ -1,26 +0,27 @@ | ||
'use strict'; | ||
'use strict' | ||
let chalk = require('chalk'); | ||
let throttle = require('./throttle'); | ||
function progress (opts) { | ||
const chalk = require('chalk') | ||
const throttle = require('./throttle') | ||
const ansi = require('ansi-escapes') | ||
const partials = ['▏', '▎', '▍', '▌', '▋', '▊', '▉'].map((p) => chalk.blue(p)) | ||
const partials = ['▏', '▎', '▍', '▌', '▋', '▊', '▉'].map((p) => chalk.blue(p)); | ||
function progress(opts) { | ||
if (typeof(opts) === 'number') { | ||
opts = {total: opts}; | ||
if (typeof (opts) === 'number') { | ||
opts = {total: opts} | ||
} | ||
let stderr = process.stderr; | ||
let stderr = process.stderr | ||
let bar = { | ||
total: opts.total, | ||
width: opts.width || 25, | ||
tmpl: opts.tmpl || ':bar :percent :eta', | ||
cur: 0, | ||
percent: 0, | ||
total: opts.total, | ||
width: opts.width || 25, | ||
tmpl: opts.tmpl || ':bar :percent :eta', | ||
cur: 0, | ||
percent: 0, | ||
complete: false, | ||
start: new Date(), | ||
tokens: {}, | ||
}; | ||
start: new Date(), | ||
tokens: {} | ||
} | ||
let termWidth = () => stderr.getWindowSize()[0]; | ||
let pad = (num) => num < 10 ? '0' + num.toString() : num.toString(); | ||
let termWidth = () => stderr.getWindowSize()[0] | ||
let pad = (num) => num < 10 ? '0' + num.toString() : num.toString() | ||
stderr.write(ansi.cursorHide) | ||
@@ -31,6 +32,6 @@ function line () { | ||
.replace(':percent', pad(Math.round(bar.percent * 100)) + '%') | ||
.replace(':eta', eta()); | ||
.replace(':eta', eta()) | ||
if (bar.tokens) { | ||
for (let token of Object.keys(bar.tokens)) { | ||
line = line.replace(':' + token, bar.tokens[token]); | ||
line = line.replace(':' + token, bar.tokens[token]) | ||
} | ||
@@ -40,61 +41,61 @@ } | ||
// so we don't have to clear it causing a flicker | ||
line = line + ' '.repeat(Math.max(termWidth() - chalk.stripColor(line).length, 0)); | ||
return line; | ||
line = line + ' '.repeat(Math.max(termWidth() - chalk.stripColor(line).length, 0)) | ||
return line | ||
} | ||
function renderBar () { | ||
let output = ''; | ||
let ticks = bar.percent * bar.width - 1; | ||
if (ticks < 0) ticks = 0; | ||
let filled = Math.floor(ticks); | ||
let open = bar.width - filled - 1; | ||
output += chalk.blue('█').repeat(filled); | ||
output += partials[Math.floor((ticks - filled)*partials.length)]; | ||
output += ' '.repeat(open) + ' '; | ||
return output; | ||
let output = '' | ||
let ticks = bar.percent * bar.width - 1 | ||
if (ticks < 0) ticks = 0 | ||
let filled = Math.floor(ticks) | ||
let open = bar.width - filled - 1 | ||
output += chalk.blue('█').repeat(filled) | ||
output += partials[Math.floor((ticks - filled) * partials.length)] | ||
output += ' '.repeat(open) + ' ' | ||
return output | ||
} | ||
function eta () { | ||
let elapsed = new Date() - bar.start; | ||
let eta = Math.abs(((bar.percent === 100) ? 0 : elapsed * (bar.total / bar.cur - 1))/1000); | ||
let hours = Math.floor(eta/3600); | ||
let minutes = Math.floor((eta-(hours*3600))/60); | ||
let seconds = Math.round(eta-(hours*3600)-(minutes*60)); | ||
let elapsed = new Date() - bar.start | ||
let eta = Math.abs(((bar.percent === 100) ? 0 : elapsed * (bar.total / bar.cur - 1)) / 1000) | ||
let hours = Math.floor(eta / 3600) | ||
let minutes = Math.floor((eta - (hours * 3600)) / 60) | ||
let seconds = Math.round(eta - (hours * 3600) - (minutes * 60)) | ||
if (hours) { | ||
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}`; | ||
return `${pad(hours)}:${pad(minutes)}:${pad(seconds)}` | ||
} else { | ||
return `${pad(minutes)}:${pad(seconds)}` | ||
} | ||
else { | ||
return `${pad(minutes)}:${pad(seconds)}`; | ||
} | ||
} | ||
function render () { | ||
if (!stderr.isTTY) return; | ||
if (bar.complete) return; | ||
stderr.write(line()); | ||
stderr.cursorTo(0); | ||
if (!stderr.isTTY) return | ||
if (bar.complete) return | ||
stderr.write(line()) | ||
stderr.cursorTo(0) | ||
} | ||
let throttledRender = throttle(render, 16); | ||
let throttledRender = throttle(render, 16) | ||
function done () { | ||
render(); | ||
bar.complete = true; | ||
stderr.write('\n'); | ||
render() | ||
bar.complete = true | ||
stderr.write('\n') | ||
stderr.write(ansi.cursorShow) | ||
} | ||
bar.tick = function (inc, tokens) { | ||
bar.tokens = tokens; | ||
bar.cur += inc; | ||
bar.percent = parseFloat(bar.cur)/bar.total; | ||
if (bar.percent > 1) bar.percent = 1; | ||
if (bar.percent < 0) bar.percent = 0; | ||
bar.tokens = tokens | ||
bar.cur += inc | ||
bar.percent = parseFloat(bar.cur) / bar.total | ||
if (bar.percent > 1) bar.percent = 1 | ||
if (bar.percent < 0) bar.percent = 0 | ||
throttledRender(); | ||
throttledRender() | ||
if (bar.cur >= bar.total) done(); | ||
}; | ||
if (bar.cur >= bar.total) done() | ||
} | ||
return bar; | ||
return bar | ||
} | ||
module.exports = progress; | ||
module.exports = progress |
@@ -1,26 +0,26 @@ | ||
'use strict'; | ||
'use strict' | ||
function throttle(fn, threshhold, scope) { | ||
threshhold || (threshhold = 250); | ||
function throttle (fn, threshhold, scope) { | ||
threshhold || (threshhold = 250) | ||
var last, | ||
deferTimer; | ||
deferTimer | ||
return function () { | ||
var context = scope || this; | ||
var context = scope || this | ||
var now = +new Date, | ||
args = arguments; | ||
var now = +new Date() | ||
var args = arguments | ||
if (last && now < last + threshhold) { | ||
// hold on to it | ||
clearTimeout(deferTimer); | ||
clearTimeout(deferTimer) | ||
deferTimer = setTimeout(function () { | ||
last = now; | ||
fn.apply(context, args); | ||
}, threshhold); | ||
last = now | ||
fn.apply(context, args) | ||
}, threshhold) | ||
} else { | ||
last = now; | ||
fn.apply(context, args); | ||
last = now | ||
fn.apply(context, args) | ||
} | ||
}; | ||
} | ||
} | ||
module.exports = throttle; | ||
module.exports = throttle |
{ | ||
"name": "smooth-progress", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "pixel-by-pixel CLI progress bar", | ||
@@ -10,3 +10,3 @@ "main": "index.js", | ||
"scripts": { | ||
"test": "node test.js" | ||
"test": "node test.js && standard" | ||
}, | ||
@@ -27,4 +27,8 @@ "repository": { | ||
"dependencies": { | ||
"ansi-escapes": "1.4.0", | ||
"chalk": "^1.1.1" | ||
}, | ||
"devDependencies": { | ||
"standard": "8.5.0" | ||
} | ||
} |
14
test.js
#!/usr/bin/env node | ||
'use strict'; | ||
let progress = require('./index'); | ||
'use strict' | ||
let progress = require('./index') | ||
let bar = progress(512*1024); | ||
let bar = progress(512 * 1024) | ||
let interval = setInterval(function () { | ||
let chunk = Math.random() * 1024; | ||
bar.tick(chunk); | ||
if (bar.complete) clearInterval(interval); | ||
}, 5); | ||
let chunk = Math.random() * 1024 | ||
bar.tick(chunk) | ||
if (bar.complete) clearInterval(interval) | ||
}, 5) |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
4776
120
2
1
+ Addedansi-escapes@1.4.0
+ Addedansi-escapes@1.4.0(transitive)