]> 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/lib/ansi.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 / lib / ansi.js
1
2 /**
3  * References:
4  *
5  *   - http://en.wikipedia.org/wiki/ANSI_escape_code
6  *   - http://www.termsys.demon.co.uk/vtansi.htm
7  *
8  */
9
10 /**
11  * Module dependencies.
12  */
13
14 var emitNewlineEvents = require('./newlines')
15   , prefix = '\x1b[' // For all escape codes
16   , suffix = 'm'     // Only for color codes
17
18 /**
19  * The ANSI escape sequences.
20  */
21
22 var codes = {
23     up: 'A'
24   , down: 'B'
25   , forward: 'C'
26   , back: 'D'
27   , nextLine: 'E'
28   , previousLine: 'F'
29   , horizontalAbsolute: 'G'
30   , eraseData: 'J'
31   , eraseLine: 'K'
32   , scrollUp: 'S'
33   , scrollDown: 'T'
34   , savePosition: 's'
35   , restorePosition: 'u'
36   , queryPosition: '6n'
37   , hide: '?25l'
38   , show: '?25h'
39 }
40
41 /**
42  * Rendering ANSI codes.
43  */
44
45 var styles = {
46     bold: 1
47   , italic: 3
48   , underline: 4
49   , inverse: 7
50 }
51
52 /**
53  * The negating ANSI code for the rendering modes.
54  */
55
56 var reset = {
57     bold: 22
58   , italic: 23
59   , underline: 24
60   , inverse: 27
61 }
62
63 /**
64  * The standard, styleable ANSI colors.
65  */
66
67 var colors = {
68     white: 37
69   , black: 30
70   , blue: 34
71   , cyan: 36
72   , green: 32
73   , magenta: 35
74   , red: 31
75   , yellow: 33
76   , grey: 90
77   , brightBlack: 90
78   , brightRed: 91
79   , brightGreen: 92
80   , brightYellow: 93
81   , brightBlue: 94
82   , brightMagenta: 95
83   , brightCyan: 96
84   , brightWhite: 97
85 }
86
87
88 /**
89  * Creates a Cursor instance based off the given `writable stream` instance.
90  */
91
92 function ansi (stream, options) {
93   if (stream._ansicursor) {
94     return stream._ansicursor
95   } else {
96     return stream._ansicursor = new Cursor(stream, options)
97   }
98 }
99 module.exports = exports = ansi
100
101 /**
102  * The `Cursor` class.
103  */
104
105 function Cursor (stream, options) {
106   if (!(this instanceof Cursor)) {
107     return new Cursor(stream, options)
108   }
109   if (typeof stream != 'object' || typeof stream.write != 'function') {
110     throw new Error('a valid Stream instance must be passed in')
111   }
112
113   // the stream to use
114   this.stream = stream
115
116   // when 'enabled' is false then all the functions are no-ops except for write()
117   this.enabled = options && options.enabled
118   if (typeof this.enabled === 'undefined') {
119     this.enabled = stream.isTTY
120   }
121   this.enabled = !!this.enabled
122
123   // then `buffering` is true, then `write()` calls are buffered in
124   // memory until `flush()` is invoked
125   this.buffering = !!(options && options.buffering)
126   this._buffer = []
127
128   // controls the foreground and background colors
129   this.fg = this.foreground = new Colorer(this, 0)
130   this.bg = this.background = new Colorer(this, 10)
131
132   // defaults
133   this.Bold = false
134   this.Italic = false
135   this.Underline = false
136   this.Inverse = false
137
138   // keep track of the number of "newlines" that get encountered
139   this.newlines = 0
140   emitNewlineEvents(stream)
141   stream.on('newline', function () {
142     this.newlines++
143   }.bind(this))
144 }
145 exports.Cursor = Cursor
146
147 /**
148  * Helper function that calls `write()` on the underlying Stream.
149  * Returns `this` instead of the write() return value to keep
150  * the chaining going.
151  */
152
153 Cursor.prototype.write = function (data) {
154   if (this.buffering) {
155     this._buffer.push(arguments)
156   } else {
157     this.stream.write.apply(this.stream, arguments)
158   }
159   return this
160 }
161
162 /**
163  * Buffer `write()` calls into memory.
164  *
165  * @api public
166  */
167
168 Cursor.prototype.buffer = function () {
169   this.buffering = true
170   return this
171 }
172
173 /**
174  * Write out the in-memory buffer.
175  *
176  * @api public
177  */
178
179 Cursor.prototype.flush = function () {
180   this.buffering = false
181   var str = this._buffer.map(function (args) {
182     if (args.length != 1) throw new Error('unexpected args length! ' + args.length);
183     return args[0];
184   }).join('');
185   this._buffer.splice(0); // empty
186   this.write(str);
187   return this
188 }
189
190
191 /**
192  * The `Colorer` class manages both the background and foreground colors.
193  */
194
195 function Colorer (cursor, base) {
196   this.current = null
197   this.cursor = cursor
198   this.base = base
199 }
200 exports.Colorer = Colorer
201
202 /**
203  * Write an ANSI color code, ensuring that the same code doesn't get rewritten.
204  */
205
206 Colorer.prototype._setColorCode = function setColorCode (code) {
207   var c = String(code)
208   if (this.current === c) return
209   this.cursor.enabled && this.cursor.write(prefix + c + suffix)
210   this.current = c
211   return this
212 }
213
214
215 /**
216  * Set up the positional ANSI codes.
217  */
218
219 Object.keys(codes).forEach(function (name) {
220   var code = String(codes[name])
221   Cursor.prototype[name] = function () {
222     var c = code
223     if (arguments.length > 0) {
224       c = toArray(arguments).map(Math.round).join(';') + code
225     }
226     this.enabled && this.write(prefix + c)
227     return this
228   }
229 })
230
231 /**
232  * Set up the functions for the rendering ANSI codes.
233  */
234
235 Object.keys(styles).forEach(function (style) {
236   var name = style[0].toUpperCase() + style.substring(1)
237     , c = styles[style]
238     , r = reset[style]
239
240   Cursor.prototype[style] = function () {
241     if (this[name]) return this
242     this.enabled && this.write(prefix + c + suffix)
243     this[name] = true
244     return this
245   }
246
247   Cursor.prototype['reset' + name] = function () {
248     if (!this[name]) return this
249     this.enabled && this.write(prefix + r + suffix)
250     this[name] = false
251     return this
252   }
253 })
254
255 /**
256  * Setup the functions for the standard colors.
257  */
258
259 Object.keys(colors).forEach(function (color) {
260   var code = colors[color]
261
262   Colorer.prototype[color] = function () {
263     this._setColorCode(this.base + code)
264     return this.cursor
265   }
266
267   Cursor.prototype[color] = function () {
268     return this.foreground[color]()
269   }
270 })
271
272 /**
273  * Makes a beep sound!
274  */
275
276 Cursor.prototype.beep = function () {
277   this.enabled && this.write('\x07')
278   return this
279 }
280
281 /**
282  * Moves cursor to specific position
283  */
284
285 Cursor.prototype.goto = function (x, y) {
286   x = x | 0
287   y = y | 0
288   this.enabled && this.write(prefix + y + ';' + x + 'H')
289   return this
290 }
291
292 /**
293  * Resets the color.
294  */
295
296 Colorer.prototype.reset = function () {
297   this._setColorCode(this.base + 39)
298   return this.cursor
299 }
300
301 /**
302  * Resets all ANSI formatting on the stream.
303  */
304
305 Cursor.prototype.reset = function () {
306   this.enabled && this.write(prefix + '0' + suffix)
307   this.Bold = false
308   this.Italic = false
309   this.Underline = false
310   this.Inverse = false
311   this.foreground.current = null
312   this.background.current = null
313   return this
314 }
315
316 /**
317  * Sets the foreground color with the given RGB values.
318  * The closest match out of the 216 colors is picked.
319  */
320
321 Colorer.prototype.rgb = function (r, g, b) {
322   var base = this.base + 38
323     , code = rgb(r, g, b)
324   this._setColorCode(base + ';5;' + code)
325   return this.cursor
326 }
327
328 /**
329  * Same as `cursor.fg.rgb(r, g, b)`.
330  */
331
332 Cursor.prototype.rgb = function (r, g, b) {
333   return this.foreground.rgb(r, g, b)
334 }
335
336 /**
337  * Accepts CSS color codes for use with ANSI escape codes.
338  * For example: `#FF000` would be bright red.
339  */
340
341 Colorer.prototype.hex = function (color) {
342   return this.rgb.apply(this, hex(color))
343 }
344
345 /**
346  * Same as `cursor.fg.hex(color)`.
347  */
348
349 Cursor.prototype.hex = function (color) {
350   return this.foreground.hex(color)
351 }
352
353
354 // UTIL FUNCTIONS //
355
356 /**
357  * Translates a 255 RGB value to a 0-5 ANSI RGV value,
358  * then returns the single ANSI color code to use.
359  */
360
361 function rgb (r, g, b) {
362   var red = r / 255 * 5
363     , green = g / 255 * 5
364     , blue = b / 255 * 5
365   return rgb5(red, green, blue)
366 }
367
368 /**
369  * Turns rgb 0-5 values into a single ANSI color code to use.
370  */
371
372 function rgb5 (r, g, b) {
373   var red = Math.round(r)
374     , green = Math.round(g)
375     , blue = Math.round(b)
376   return 16 + (red*36) + (green*6) + blue
377 }
378
379 /**
380  * Accepts a hex CSS color code string (# is optional) and
381  * translates it into an Array of 3 RGB 0-255 values, which
382  * can then be used with rgb().
383  */
384
385 function hex (color) {
386   var c = color[0] === '#' ? color.substring(1) : color
387     , r = c.substring(0, 2)
388     , g = c.substring(2, 4)
389     , b = c.substring(4, 6)
390   return [parseInt(r, 16), parseInt(g, 16), parseInt(b, 16)]
391 }
392
393 /**
394  * Turns an array-like object into a real array.
395  */
396
397 function toArray (a) {
398   var i = 0
399     , l = a.length
400     , rtn = []
401   for (; i<l; i++) {
402     rtn.push(a[i])
403   }
404   return rtn
405 }