]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/ansi/examples/progress/index.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / ansi / examples / progress / index.js
1 #!/usr/bin/env node
2
3 var assert = require('assert')
4   , ansi = require('../../')
5
6 function Progress (stream, width) {
7   this.cursor = ansi(stream)
8   this.delta = this.cursor.newlines
9   this.width = width | 0 || 10
10   this.open = '['
11   this.close = ']'
12   this.complete = '█'
13   this.incomplete = '_'
14
15   // initial render
16   this.progress = 0
17 }
18
19 Object.defineProperty(Progress.prototype, 'progress', {
20     get: get
21   , set: set
22   , configurable: true
23   , enumerable: true
24 })
25
26 function get () {
27   return this._progress
28 }
29
30 function set (v) {
31   this._progress = Math.max(0, Math.min(v, 100))
32
33   var w = this.width - this.complete.length - this.incomplete.length
34     , n = w * (this._progress / 100) | 0
35     , i = w - n
36     , com = c(this.complete, n)
37     , inc = c(this.incomplete, i)
38     , delta = this.cursor.newlines - this.delta
39
40   assert.equal(com.length + inc.length, w)
41
42   if (delta > 0) {
43     this.cursor.up(delta)
44     this.delta = this.cursor.newlines
45   }
46
47   this.cursor
48     .horizontalAbsolute(0)
49     .eraseLine(2)
50     .fg.white()
51     .write(this.open)
52     .fg.grey()
53     .bold()
54     .write(com)
55     .resetBold()
56     .write(inc)
57     .fg.white()
58     .write(this.close)
59     .fg.reset()
60     .write('\n')
61 }
62
63 function c (char, length) {
64   return Array.apply(null, Array(length)).map(function () {
65     return char
66   }).join('')
67 }
68
69
70
71
72 // Usage
73 var width = parseInt(process.argv[2], 10) || process.stdout.getWindowSize()[0] / 2
74   , p = new Progress(process.stdout, width)
75
76 ;(function tick () {
77   p.progress += Math.random() * 5
78   p.cursor
79     .eraseLine(2)
80     .write('Progress: ')
81     .bold().write(p.progress.toFixed(2))
82     .write('%')
83     .resetBold()
84     .write('\n')
85   if (p.progress < 100)
86     setTimeout(tick, 100)
87 })()