Files: fd6d70a30dab618ef7610aa8d4df18b38eed8c25 / lib / progress.js
979 bytesRaw
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 | var int = 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) |
42 | int.unref && int.unref() |
43 | } |
44 |
Built with git-ssb-web