2 var Plumbing = require('./plumbing.js')
3 var hasUnicode = require('has-unicode')
4 var hasColor = require('./has-color.js')
5 var onExit = require('signal-exit')
6 var defaultThemes = require('./themes')
7 var setInterval = require('./set-interval.js')
8 var process = require('./process.js')
9 var setImmediate = require('./set-immediate')
11 module.exports = Gauge
13 function callWith (obj, method) {
15 return method.call(obj)
19 function Gauge (arg1, arg2) {
21 if (arg1 && arg1.write) {
24 } else if (arg2 && arg2.write) {
28 writeTo = process.stderr
29 options = arg1 || arg2 || {}
37 this._paused = false // are we paused for back pressure?
38 this._disabled = true // are all progress bar updates disabled?
39 this._showing = false // do we WANT the progress bar on screen
40 this._onScreen = false // IS the progress bar on screen
41 this._needsRedraw = false // should we print something at next tick?
42 this._hideCursor = options.hideCursor == null ? true : options.hideCursor
43 this._fixedFramerate = options.fixedFramerate == null
44 ? !(/^v0\.8\./.test(process.version))
45 : options.fixedFramerate
46 this._lastUpdateAt = null
47 this._updateInterval = options.updateInterval == null ? 50 : options.updateInterval
49 this._themes = options.themes || defaultThemes
50 this._theme = options.theme
51 var theme = this._computeTheme(options.theme)
52 var template = options.template || [
53 {type: 'progressbar', length: 20},
54 {type: 'activityIndicator', kerning: 1, length: 1},
55 {type: 'section', kerning: 1, default: ''},
56 {type: 'subsection', kerning: 1, default: ''}
58 this.setWriteTo(writeTo, options.tty)
59 var PlumbingClass = options.Plumbing || Plumbing
60 this._gauge = new PlumbingClass(theme, template, this.getWidth())
62 this._$$doRedraw = callWith(this, this._doRedraw)
63 this._$$handleSizeChange = callWith(this, this._handleSizeChange)
65 this._cleanupOnExit = options.cleanupOnExit == null || options.cleanupOnExit
66 this._removeOnExit = null
68 if (options.enabled || (options.enabled == null && this._tty && this._tty.isTTY)) {
76 Gauge.prototype.isEnabled = function () {
77 return !this._disabled
80 Gauge.prototype.setTemplate = function (template) {
81 this._gauge.setTemplate(template)
82 if (this._showing) this._requestRedraw()
85 Gauge.prototype._computeTheme = function (theme) {
86 if (!theme) theme = {}
87 if (typeof theme === 'string') {
88 theme = this._themes.getTheme(theme)
89 } else if (theme && (Object.keys(theme).length === 0 || theme.hasUnicode != null || theme.hasColor != null)) {
90 var useUnicode = theme.hasUnicode == null ? hasUnicode() : theme.hasUnicode
91 var useColor = theme.hasColor == null ? hasColor : theme.hasColor
92 theme = this._themes.getDefault({hasUnicode: useUnicode, hasColor: useColor, platform: theme.platform})
97 Gauge.prototype.setThemeset = function (themes) {
99 this.setTheme(this._theme)
102 Gauge.prototype.setTheme = function (theme) {
103 this._gauge.setTheme(this._computeTheme(theme))
104 if (this._showing) this._requestRedraw()
108 Gauge.prototype._requestRedraw = function () {
109 this._needsRedraw = true
110 if (!this._fixedFramerate) this._doRedraw()
113 Gauge.prototype.getWidth = function () {
114 return ((this._tty && this._tty.columns) || 80) - 1
117 Gauge.prototype.setWriteTo = function (writeTo, tty) {
118 var enabled = !this._disabled
119 if (enabled) this.disable()
120 this._writeTo = writeTo
122 (writeTo === process.stderr && process.stdout.isTTY && process.stdout) ||
123 (writeTo.isTTY && writeTo) ||
125 if (this._gauge) this._gauge.setWidth(this.getWidth())
126 if (enabled) this.enable()
129 Gauge.prototype.enable = function () {
130 if (!this._disabled) return
131 this._disabled = false
132 if (this._tty) this._enableEvents()
133 if (this._showing) this.show()
136 Gauge.prototype.disable = function () {
137 if (this._disabled) return
139 this._lastUpdateAt = null
140 this._showing = false
144 this._disabled = true
145 if (this._tty) this._disableEvents()
148 Gauge.prototype._enableEvents = function () {
149 if (this._cleanupOnExit) {
150 this._removeOnExit = onExit(callWith(this, this.disable))
152 this._tty.on('resize', this._$$handleSizeChange)
153 if (this._fixedFramerate) {
154 this.redrawTracker = setInterval(this._$$doRedraw, this._updateInterval)
155 if (this.redrawTracker.unref) this.redrawTracker.unref()
159 Gauge.prototype._disableEvents = function () {
160 this._tty.removeListener('resize', this._$$handleSizeChange)
161 if (this._fixedFramerate) clearInterval(this.redrawTracker)
162 if (this._removeOnExit) this._removeOnExit()
165 Gauge.prototype.hide = function (cb) {
166 if (this._disabled) return cb && process.nextTick(cb)
167 if (!this._showing) return cb && process.nextTick(cb)
168 this._showing = false
170 cb && setImmediate(cb)
173 Gauge.prototype.show = function (section, completed) {
175 if (typeof section === 'string') {
176 this._status.section = section
177 } else if (typeof section === 'object') {
178 var sectionKeys = Object.keys(section)
179 for (var ii = 0; ii < sectionKeys.length; ++ii) {
180 var key = sectionKeys[ii]
181 this._status[key] = section[key]
184 if (completed != null) this._status.completed = completed
185 if (this._disabled) return
186 this._requestRedraw()
189 Gauge.prototype.pulse = function (subsection) {
190 this._status.subsection = subsection || ''
192 if (this._disabled) return
193 if (!this._showing) return
194 this._requestRedraw()
197 Gauge.prototype._handleSizeChange = function () {
198 this._gauge.setWidth(this._tty.columns - 1)
199 this._requestRedraw()
202 Gauge.prototype._doRedraw = function () {
203 if (this._disabled || this._paused) return
204 if (!this._fixedFramerate) {
206 if (this._lastUpdateAt && now - this._lastUpdateAt < this._updateInterval) return
207 this._lastUpdateAt = now
209 if (!this._showing && this._onScreen) {
210 this._onScreen = false
211 var result = this._gauge.hide()
212 if (this._hideCursor) {
213 result += this._gauge.showCursor()
215 return this._writeTo.write(result)
217 if (!this._showing && !this._onScreen) return
218 if (this._showing && !this._onScreen) {
219 this._onScreen = true
220 this._needsRedraw = true
221 if (this._hideCursor) {
222 this._writeTo.write(this._gauge.hideCursor())
225 if (!this._needsRedraw) return
226 if (!this._writeTo.write(this._gauge.show(this._status))) {
228 this._writeTo.on('drain', callWith(this, function () {