diff options
Diffstat (limited to 'node_modules/npmlog/log.js')
-rw-r--r-- | node_modules/npmlog/log.js | 147 |
1 files changed, 97 insertions, 50 deletions
diff --git a/node_modules/npmlog/log.js b/node_modules/npmlog/log.js index 8d781f173..e5b70f1f8 100644 --- a/node_modules/npmlog/log.js +++ b/node_modules/npmlog/log.js @@ -2,49 +2,76 @@ var Progress = require('are-we-there-yet') var Gauge = require('gauge') var EE = require('events').EventEmitter -var log = exports = module.exports = new EE +var log = exports = module.exports = new EE() var util = require('util') -var ansi = require('ansi') -log.cursor = ansi(process.stderr) -log.stream = process.stderr +var setBlocking = require('set-blocking') +var consoleControl = require('console-control-strings') + +setBlocking(true) +var stream = process.stderr +Object.defineProperty(log, 'stream', { + set: function (newStream) { + stream = newStream + if (this.gauge) this.gauge.setWriteTo(stream, stream) + }, + get: function () { + return stream + } +}) + +// by default, decide based on tty-ness. +var colorEnabled +log.useColor = function () { + return colorEnabled != null ? colorEnabled : stream.isTTY +} -// by default, let ansi decide based on tty-ness. -var colorEnabled = undefined log.enableColor = function () { colorEnabled = true - this.cursor.enabled = true + this.gauge.setTheme({hasColor: colorEnabled, hasUnicode: unicodeEnabled}) } log.disableColor = function () { colorEnabled = false - this.cursor.enabled = false + this.gauge.setTheme({hasColor: colorEnabled, hasUnicode: unicodeEnabled}) } // default level log.level = 'info' -log.gauge = new Gauge(log.cursor) +log.gauge = new Gauge(stream, { + theme: {hasColor: log.useColor()}, + template: [ + {type: 'progressbar', length: 20}, + {type: 'activityIndicator', kerning: 1, length: 1}, + {type: 'section', default: ''}, + ':', + {type: 'logline', kerning: 1, default: ''} + ] +}) + log.tracker = new Progress.TrackerGroup() // no progress bars unless asked log.progressEnabled = false -var gaugeTheme = undefined +var unicodeEnabled log.enableUnicode = function () { - gaugeTheme = Gauge.unicode - log.gauge.setTheme(gaugeTheme) + unicodeEnabled = true + this.gauge.setTheme({hasColor: this.useColor(), hasUnicode: unicodeEnabled}) } log.disableUnicode = function () { - gaugeTheme = Gauge.ascii - log.gauge.setTheme(gaugeTheme) + unicodeEnabled = false + this.gauge.setTheme({hasColor: this.useColor(), hasUnicode: unicodeEnabled}) +} + +log.setGaugeThemeset = function (themes) { + this.gauge.setThemeset(themes) } -var gaugeTemplate = undefined log.setGaugeTemplate = function (template) { - gaugeTemplate = template - log.gauge.setTemplate(gaugeTemplate) + this.gauge.setTemplate(template) } log.enableProgress = function () { @@ -95,15 +122,26 @@ 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.clearProgress = function (cb) { + if (!this.progressEnabled) return cb && process.nextTick(cb) + this.gauge.hide(cb) } log.showProgress = function (name, completed) { if (!this.progressEnabled) return - if (completed == null) completed = this.tracker.completed() - this.gauge.show(name, completed) + var values = {} + if (name) values.section = name + var last = log.record[log.record.length - 1] + if (last) { + values.subsection = last.prefix + var disp = log.disp[last.level] || last.level + var logline = this._format(disp, log.style[last.level]) + if (last.prefix) logline += ' ' + this._format(last.prefix, this.prefixStyle) + logline += ' ' + last.message.split(/\r?\n/)[0] + values.logline = logline + } + values.completed = completed || this.tracker.completed() + this.gauge.show(values) }.bind(log) // bind for use in tracker's on-change listener // temporarily stop emitting, but don't drop @@ -137,8 +175,8 @@ log.log = function (lvl, prefix, message) { var a = new Array(arguments.length - 2) var stack = null - for (var i = 2; i < arguments.length; i ++) { - var arg = a[i-2] = arguments[i] + for (var i = 2; i < arguments.length; i++) { + var arg = a[i - 2] = arguments[i] // resolve stack traces to a plain string. if (typeof arg === 'object' && arg && @@ -181,7 +219,6 @@ log.emitLog = function (m) { if (l < this.levels[this.level]) return if (l > 0 && !isFinite(l)) return - var style = log.style[m.level] var disp = log.disp[m.level] || m.level this.clearProgress() m.message.split(/\r?\n/).forEach(function (line) { @@ -198,38 +235,48 @@ log.emitLog = function (m) { 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) +log._format = function (msg, style) { + if (!stream) return + + var output = '' + if (this.useColor()) { + style = style || {} + var settings = [] + if (style.fg) settings.push(style.fg) + if (style.bg) settings.push('bg' + style.bg[0].toUpperCase() + style.bg.slice(1)) + if (style.bold) settings.push('bold') + if (style.underline) settings.push('underline') + if (style.inverse) settings.push('inverse') + if (settings.length) output += consoleControl.color(settings) + if (style.beep) output += consoleControl.beep() } + output += msg + if (this.useColor()) { + output += consoleControl.color('reset') + } + return output +} - style = style || {} - if (style.fg) this.cursor.fg[style.fg]() - if (style.bg) this.cursor.bg[style.bg]() - if (style.bold) this.cursor.bold() - if (style.underline) this.cursor.underline() - if (style.inverse) this.cursor.inverse() - if (style.beep) this.cursor.beep() - this.cursor.write(msg).reset() +log.write = function (msg, style) { + if (!stream) return + + stream.write(this._format(msg, style)) } log.addLevel = function (lvl, n, style, disp) { if (!disp) disp = lvl this.levels[lvl] = n this.style[lvl] = style - if (!this[lvl]) this[lvl] = function () { - var a = new Array(arguments.length + 1) - a[0] = lvl - for (var i = 0; i < arguments.length; i ++) { - a[i + 1] = arguments[i] - } - return this.log.apply(this, a) - }.bind(this) + if (!this[lvl]) { + this[lvl] = function () { + var a = new Array(arguments.length + 1) + a[0] = lvl + for (var i = 0; i < arguments.length; i++) { + a[i + 1] = arguments[i] + } + return this.log.apply(this, a) + }.bind(this) + } this.disp[lvl] = disp } @@ -248,4 +295,4 @@ log.addLevel('error', 5000, { fg: 'red', bg: 'black' }, 'ERR!') log.addLevel('silent', Infinity) // allow 'error' prefix -log.on('error', function(){}) +log.on('error', function () {}) |