var bytesjs = require('component-bytes.js'); exports.getCounter = (fileLength, dotStep) => { var total = 0; var step = dotStep || (1024 * 64); var percentPrintFlag = {}; var percentN = (fileLength > (60 * 1024 * 1024)) ? 100 : 10; return (len) => { var t = total; total += len; var n = parseInt(total / step) - parseInt(t / step); if (n > 0) { n.times(() => { print('.') }); } if (fileLength) { var updatePercentN = parseInt((total * percentN) / fileLength); if (updatePercentN && (!(percentPrintFlag[updatePercentN]))) { percentPrintFlag[updatePercentN] = 1; print('[' + parseInt((total * 100) / fileLength) + '%]'); } } }; }; exports.getCounter2 = (dotStep) => { var total = 0; var step = dotStep || (1024 * 64); var percentPrintFlag = {}; return (len, fileLength) => { var t = total; total += len; var n = parseInt(total / step) - parseInt(t / step); if (n > 0) { n.times(() => { print('.') }); } if (fileLength > 0) { var updatePercent10 = parseInt((total * 10) / fileLength); if (updatePercent10 && (!(percentPrintFlag[updatePercent10]))) { percentPrintFlag[updatePercent10] = 1; print('[' + parseInt((total * 100) / fileLength) + '%]'); } } }; }; exports.getCounter2Bar = (dotStep) => { Packages.me.hatter.tools.jssp.main.StandaloneMainInteractive.initJLine(); // require jline var total = 0; var start = $$.date().millis(); var step = dotStep || (1024 * 64); var percentPrintFlag = {}; return (len, fileLength) => { var t = total; total += len; var diff = $$.date().millis() - start; var download = bytesjs.showBytes(total); var speed = 'unknown'; if (diff > 0) { speed = bytesjs.showBytes(parseInt(total * 1000 / diff)) + '/s'; } if (fileLength > 0) { var width = Packages.jline.TerminalFactory.get().getWidth(); var per = ' ' + $$.num(total).mul(100).divr(fileLength, 2, null) + '% [' + download + ' - ' + speed + ']'; var w = $$.num(total).divr((fileLength), 10, null).mul(width - 2 - per.length).floor().aint(); reprint(repeat('>', w) + per); } else { reprint('Downloaded: ' + download + ', speed: ' + speed); } }; }; exports.getCounterBar = (fileLength) => { Packages.me.hatter.tools.jssp.main.StandaloneMainInteractive.initJLine(); // require jline var total = 0; return (len) => { var width = Packages.jline.TerminalFactory.get().getWidth(); total += len; var per = ' ' + $$.num(total).mul(100).divr(fileLength, 2, null) + '%'; var w = $$.num(total).divr((fileLength), 10, null).mul(width - 1 - per.length).floor().aint(); print('\r' + repeat('>', w) + per + '\033[K'); }; };