diff options
Diffstat (limited to 'node_modules/npmlog/log.js')
-rw-r--r-- | node_modules/npmlog/log.js | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/node_modules/npmlog/log.js b/node_modules/npmlog/log.js index 38b7c74ac..067f6ff10 100644 --- a/node_modules/npmlog/log.js +++ b/node_modules/npmlog/log.js @@ -1,3 +1,6 @@ +'use strict' +var Progress = require('are-we-there-yet') +var Gauge = require('gauge') var EE = require('events').EventEmitter var log = exports = module.exports = new EE var util = require('util') @@ -20,6 +23,88 @@ log.disableColor = function () { // default level log.level = 'info' +log.gauge = new Gauge(log.cursor) +log.tracker = new Progress.TrackerGroup() + +// no progress bars unless asked +log.progressEnabled = false + +var gaugeTheme = undefined + +log.enableUnicode = function () { + gaugeTheme = gauge.unicode + log.gauge.setTheme(gaugeTheme) +} + +log.disableUnicode = function () { + gaugeTheme = gauge.ascii + log.gauge.setTheme(gaugeTheme) +} + +var gaugeTemplate = undefined +log.setGaugeTemplate = function (template) { + gaugeTemplate = template + log.gauge.setTemplate(gaugeTemplate) +} + +log.enableProgress = function () { + if (this.progressEnabled) return + this.progressEnabled = true + if (this._pause) return + this.tracker.on('change', this.showProgress) + this.gauge.enable() + this.showProgress() +} + +log.disableProgress = function () { + if (!this.progressEnabled) return + this.clearProgress() + this.progressEnabled = false + this.tracker.removeListener('change', this.showProgress) + this.gauge.disable() +} + +var trackerConstructors = ['newGroup', 'newItem', 'newStream'] + +var mixinLog = function (tracker) { + // mixin the public methods from log into the tracker + // (except: conflicts and one's we handle specially) + Object.keys(log).forEach(function (P) { + if (P[0] === '_') return + if (trackerConstructors.filter(function (C) { return C === P }).length) return + if (tracker[P]) return + if (typeof log[P] !== 'function') return + var func = log[P] + tracker[P] = function () { + return func.apply(log, arguments) + } + }) + // if the new tracker is a group, make sure any subtrackers get + // mixed in too + if (tracker instanceof Progress.TrackerGroup) { + trackerConstructors.forEach(function (C) { + var func = tracker[C] + tracker[C] = function () { return mixinLog(func.apply(tracker, arguments)) } + }) + } + return tracker +} + +// Add tracker constructors to the top level log object +trackerConstructors.forEach(function (C) { + log[C] = function () { return mixinLog(this.tracker[C].apply(this.tracker, arguments)) } +}) + +log.clearProgress = function () { + if (!this.progressEnabled) return + this.gauge.hide() +} + +log.showProgress = function (name) { + if (!this.progressEnabled) return + this.gauge.show(name, this.tracker.completed()) +}.bind(log) // bind for use in tracker's on-change listener + // temporarily stop emitting, but don't drop log.pause = function () { this._paused = true @@ -34,6 +119,7 @@ log.resume = function () { b.forEach(function (m) { this.emitLog(m) }, this) + if (this.progressEnabled) this.enableProgress() } log._buffer = [] @@ -88,6 +174,7 @@ log.emitLog = function (m) { this._buffer.push(m) return } + if (this.progressEnabled) this.gauge.pulse(m.prefix) var l = this.levels[m.level] if (l === undefined) return if (l < this.levels[this.level]) return @@ -95,6 +182,7 @@ log.emitLog = function (m) { var style = log.style[m.level] var disp = log.disp[m.level] || m.level + this.clearProgress() m.message.split(/\r?\n/).forEach(function (line) { if (this.heading) { this.write(this.heading, this.headingStyle) @@ -106,12 +194,17 @@ log.emitLog = function (m) { this.write(p, this.prefixStyle) this.write(' ' + line + '\n') }, this) + this.showProgress() } log.write = function (msg, style) { if (!this.cursor) return if (this.stream !== this.cursor.stream) { this.cursor = ansi(this.stream, { enabled: colorEnabled }) + var options = {} + if (gaugeTheme != null) options.theme = gaugeTheme + if (gaugeTemplate != null) options.template = gaugeTemplate + this.gauge = new Gauge(options, this.cursor) } style = style || {} |