Commit 5580e6a2549e71500ea6cc7af3b4c5b5f21684fa
simple ascii progress bar
Dominic Tarr committed on 6/9/2017, 11:31:49 PMParent: 707a39608eb88fea931761a44972491e378d34b3
Files changed
lib/progress.js | added |
lib/progress.js | ||
---|---|---|
@@ -1,0 +1,42 @@ | ||
1 | +//poll the progress() function and report how much waiting there is... | |
2 | +//just whipped this up, obviously room for improvement here. | |
3 | +module.exports = function (progress) { | |
4 | + function bar (r) { | |
5 | + var s = '\r', M = 50 | |
6 | + for(var i = 0; i < M; i++) | |
7 | + s += i < M*r ? '*' : '.' | |
8 | + | |
9 | + return s | |
10 | + } | |
11 | + | |
12 | + function round (n, p) { | |
13 | + return Math.round(n * p) / p | |
14 | + } | |
15 | + | |
16 | + function percent (n) { | |
17 | + return (round(n, 1000)*100).toString()+'%' | |
18 | + } | |
19 | + | |
20 | + function rate (prog) { | |
21 | + if(prog.target == prog.current) return 1 | |
22 | + return (prog.current - prog.start) / (prog.target - prog.start) | |
23 | + } | |
24 | + | |
25 | + var prog = -1 | |
26 | + setInterval(function () { | |
27 | + var p = progress() | |
28 | + var r = 0, c = 0 | |
29 | + var tasks = [] | |
30 | + for(var k in p) { | |
31 | + var _r = rate(p[k]) | |
32 | + tasks.push(k+':'+percent(_r)) | |
33 | + r += _r | |
34 | + c++ | |
35 | + } | |
36 | + if(r != prog) { | |
37 | + prog = r | |
38 | + var msg = tasks.join(', ') | |
39 | + process.stdout.write(bar(prog) + ' ('+msg+')') | |
40 | + } | |
41 | + }, 333).unref() | |
42 | +} |
Built with git-ssb-web