]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/npmlog/node_modules/gauge/progress-bar.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / npmlog / node_modules / gauge / progress-bar.js
1 "use strict"
2 var hasUnicode = require("has-unicode")
3 var ansi = require("ansi")
4 var align = {
5   center: require("lodash.pad"),
6   left:   require("lodash.padend"),
7   right:  require("lodash.padstart")
8 }
9 var defaultStream = process.stderr
10 function isTTY() {
11   return process.stderr.isTTY
12 }
13 function getWritableTTYColumns() {
14   // Writing to the final column wraps the line
15   // We have to use stdout here, because Node's magic SIGWINCH handler only
16   // updates process.stdout, not process.stderr
17   return process.stdout.columns - 1
18 }
19
20 var ProgressBar = module.exports = function (options, cursor) {
21   if (! options) options = {}
22   if (! cursor && options.write) {
23     cursor = options
24     options = {}
25   }
26   if (! cursor) {
27     cursor = ansi(defaultStream)
28   }
29   this.cursor = cursor
30   this.showing = false
31   this.theme = options.theme || (hasUnicode() ? ProgressBar.unicode : ProgressBar.ascii)
32   this.template = options.template || [
33     {type: "name", separated: true, length: 25},
34     {type: "spinner", separated: true},
35     {type: "startgroup"},
36     {type: "completionbar"},
37     {type: "endgroup"}
38   ]
39   this.updatefreq = options.maxUpdateFrequency == null ? 50 : options.maxUpdateFrequency
40   this.lastName = ""
41   this.lastCompleted = 0
42   this.spun = 0
43   this.last = new Date(0)
44
45   var self = this
46   this._handleSizeChange = function () {
47     if (!self.showing) return
48     self.hide()
49     self.show()
50   }
51 }
52 ProgressBar.prototype = {}
53
54 ProgressBar.unicode = {
55   startgroup: "╢",
56   endgroup: "╟",
57   complete: "█",
58   incomplete: "░",
59   spinner: "▀▐▄▌",
60   subsection: "→"
61 }
62
63 ProgressBar.ascii = {
64   startgroup: "|",
65   endgroup: "|",
66   complete: "#",
67   incomplete: "-",
68   spinner: "-\\|/",
69   subsection: "->"
70 }
71
72 ProgressBar.prototype.setTheme = function(theme) {
73   this.theme = theme
74 }
75
76 ProgressBar.prototype.setTemplate = function(template) {
77   this.template = template
78 }
79
80 ProgressBar.prototype._enableResizeEvents = function() {
81   process.stdout.on('resize', this._handleSizeChange)
82 }
83
84 ProgressBar.prototype._disableResizeEvents = function() {
85   process.stdout.removeListener('resize', this._handleSizeChange)
86 }
87
88 ProgressBar.prototype.disable = function() {
89   this.hide()
90   this.disabled = true
91 }
92
93 ProgressBar.prototype.enable = function() {
94   this.disabled = false
95   this.show()
96 }
97
98 ProgressBar.prototype.hide = function() {
99   if (!isTTY()) return
100   if (this.disabled) return
101   this.cursor.show()
102   if (this.showing) this.cursor.up(1)
103   this.cursor.horizontalAbsolute(0).eraseLine()
104   this.showing = false
105 }
106
107 var repeat = function (str, count) {
108   var out = ""
109   for (var ii=0; ii<count; ++ii) out += str
110   return out
111 }
112
113 ProgressBar.prototype.pulse = function(name) {
114   ++ this.spun
115   if (! this.showing) return
116   if (this.disabled) return
117
118   var baseName = this.lastName
119   name = name
120        ? ( baseName
121          ? baseName + " " + this.theme.subsection + " " + name
122          : null )
123        : baseName
124   this.show(name)
125   this.lastName = baseName
126 }
127
128 ProgressBar.prototype.show = function(name, completed) {
129   name = this.lastName = name || this.lastName
130   completed = this.lastCompleted = completed || this.lastCompleted
131
132   if (!isTTY()) return
133   if (this.disabled) return
134   if (! this.spun && ! completed) return
135   if (this.tryAgain) return
136   var self = this
137
138   if (this.showing && new Date() - this.last < this.updatefreq) {
139     this.tryAgain = setTimeout(function () {
140       self.tryAgain = null
141       if (self.disabled) return
142       if (! self.spun && ! completed) return
143       drawBar()
144     }, this.updatefreq - (new Date() - this.last))
145     return
146   }
147
148   return drawBar()
149
150   function drawBar() {
151     var values = {
152       name: name,
153       spinner: self.spun,
154       completed: completed
155     }
156
157     self.last = new Date()
158
159     var statusline = self.renderTemplate(self.theme, self.template, values)
160
161     if (self.showing) self.cursor.up(1)
162     self.cursor
163         .hide()
164         .horizontalAbsolute(0)
165         .write(statusline.substr(0, getWritableTTYColumns()) + "\n")
166         .show()
167
168     self.showing = true
169   }
170 }
171
172 ProgressBar.prototype.renderTemplate = function (theme, template, values) {
173   values.startgroup = theme.startgroup
174   values.endgroup = theme.endgroup
175   values.spinner = values.spinner
176     ? theme.spinner.substr(values.spinner % theme.spinner.length,1)
177     : ""
178
179   var output = {prebar: "", postbar: ""}
180   var status = "prebar"
181   var self = this
182   template.forEach(function(T) {
183     if (typeof T === "string") {
184       output[status] += T
185       return
186     }
187     if (T.type === "completionbar") {
188       status = "postbar"
189       return
190     }
191     if (!values.hasOwnProperty(T.type)) throw new Error("Unknown template value '"+T.type+"'")
192     var value = self.renderValue(T, values[T.type])
193     if (value === "") return
194     var sofar = output[status].length
195     var lastChar = sofar ? output[status][sofar-1] : null
196     if (T.separated && sofar && lastChar !== " ") {
197       output[status] += " "
198     }
199     output[status] += value
200     if (T.separated) output[status] += " "
201   })
202
203   var bar = ""
204   if (status === "postbar") {
205     var nonBarLen = output.prebar.length + output.postbar.length
206
207     var barLen = getWritableTTYColumns() - nonBarLen
208     var sofar = Math.round(barLen * Math.max(0,Math.min(1,values.completed||0)))
209     var rest = barLen - sofar
210     bar = repeat(theme.complete, sofar)
211         + repeat(theme.incomplete, rest)
212   }
213
214   return output.prebar + bar + output.postbar
215 }
216 ProgressBar.prototype.renderValue = function (template, value) {
217   if (value == null || value === "") return ""
218   var maxLength = template.maxLength || template.length
219   var minLength = template.minLength || template.length
220   var alignWith = align[template.align] || align.left
221 //  if (maxLength) value = value.substr(-1 * maxLength)
222   if (maxLength) value = value.substr(0, maxLength)
223   if (minLength) value = alignWith(value, minLength)
224   return value
225 }