index.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. 'use strict'
  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')
  10. module.exports = Gauge
  11. function callWith (obj, method) {
  12. return function () {
  13. return method.call(obj)
  14. }
  15. }
  16. function Gauge (arg1, arg2) {
  17. var options, writeTo
  18. if (arg1 && arg1.write) {
  19. writeTo = arg1
  20. options = arg2 || {}
  21. } else if (arg2 && arg2.write) {
  22. writeTo = arg2
  23. options = arg1 || {}
  24. } else {
  25. writeTo = process.stderr
  26. options = arg1 || arg2 || {}
  27. }
  28. this._status = {
  29. spun: 0,
  30. section: '',
  31. subsection: ''
  32. }
  33. this._paused = false // are we paused for back pressure?
  34. this._disabled = true // are all progress bar updates disabled?
  35. this._showing = false // do we WANT the progress bar on screen
  36. this._onScreen = false // IS the progress bar on screen
  37. this._needsRedraw = false // should we print something at next tick?
  38. this._hideCursor = options.hideCursor == null ? true : options.hideCursor
  39. this._fixedFramerate = options.fixedFramerate == null
  40. ? !(/^v0\.8\./.test(process.version))
  41. : options.fixedFramerate
  42. this._lastUpdateAt = null
  43. this._updateInterval = options.updateInterval == null ? 50 : options.updateInterval
  44. this._themes = options.themes || defaultThemes
  45. this._theme = options.theme
  46. var theme = this._computeTheme(options.theme)
  47. var template = options.template || [
  48. {type: 'progressbar', length: 20},
  49. {type: 'activityIndicator', kerning: 1, length: 1},
  50. {type: 'section', kerning: 1, default: ''},
  51. {type: 'subsection', kerning: 1, default: ''}
  52. ]
  53. this.setWriteTo(writeTo, options.tty)
  54. var PlumbingClass = options.Plumbing || Plumbing
  55. this._gauge = new PlumbingClass(theme, template, this.getWidth())
  56. this._$$doRedraw = callWith(this, this._doRedraw)
  57. this._$$handleSizeChange = callWith(this, this._handleSizeChange)
  58. this._cleanupOnExit = options.cleanupOnExit == null || options.cleanupOnExit
  59. this._removeOnExit = null
  60. if (options.enabled || (options.enabled == null && this._tty && this._tty.isTTY)) {
  61. this.enable()
  62. } else {
  63. this.disable()
  64. }
  65. }
  66. Gauge.prototype = {}
  67. Gauge.prototype.isEnabled = function () {
  68. return !this._disabled
  69. }
  70. Gauge.prototype.setTemplate = function (template) {
  71. this._gauge.setTemplate(template)
  72. if (this._showing) this._requestRedraw()
  73. }
  74. Gauge.prototype._computeTheme = function (theme) {
  75. if (!theme) theme = {}
  76. if (typeof theme === 'string') {
  77. theme = this._themes.getTheme(theme)
  78. } else if (theme && (Object.keys(theme).length === 0 || theme.hasUnicode != null || theme.hasColor != null)) {
  79. var useUnicode = theme.hasUnicode == null ? hasUnicode() : theme.hasUnicode
  80. var useColor = theme.hasColor == null ? hasColor : theme.hasColor
  81. theme = this._themes.getDefault({hasUnicode: useUnicode, hasColor: useColor, platform: theme.platform})
  82. }
  83. return theme
  84. }
  85. Gauge.prototype.setThemeset = function (themes) {
  86. this._themes = themes
  87. this.setTheme(this._theme)
  88. }
  89. Gauge.prototype.setTheme = function (theme) {
  90. this._gauge.setTheme(this._computeTheme(theme))
  91. if (this._showing) this._requestRedraw()
  92. this._theme = theme
  93. }
  94. Gauge.prototype._requestRedraw = function () {
  95. this._needsRedraw = true
  96. if (!this._fixedFramerate) this._doRedraw()
  97. }
  98. Gauge.prototype.getWidth = function () {
  99. return ((this._tty && this._tty.columns) || 80) - 1
  100. }
  101. Gauge.prototype.setWriteTo = function (writeTo, tty) {
  102. var enabled = !this._disabled
  103. if (enabled) this.disable()
  104. this._writeTo = writeTo
  105. this._tty = tty ||
  106. (writeTo === process.stderr && process.stdout.isTTY && process.stdout) ||
  107. (writeTo.isTTY && writeTo) ||
  108. this._tty
  109. if (this._gauge) this._gauge.setWidth(this.getWidth())
  110. if (enabled) this.enable()
  111. }
  112. Gauge.prototype.enable = function () {
  113. if (!this._disabled) return
  114. this._disabled = false
  115. if (this._tty) this._enableEvents()
  116. if (this._showing) this.show()
  117. }
  118. Gauge.prototype.disable = function () {
  119. if (this._disabled) return
  120. if (this._showing) {
  121. this._lastUpdateAt = null
  122. this._showing = false
  123. this._doRedraw()
  124. this._showing = true
  125. }
  126. this._disabled = true
  127. if (this._tty) this._disableEvents()
  128. }
  129. Gauge.prototype._enableEvents = function () {
  130. if (this._cleanupOnExit) {
  131. this._removeOnExit = onExit(callWith(this, this.disable))
  132. }
  133. this._tty.on('resize', this._$$handleSizeChange)
  134. if (this._fixedFramerate) {
  135. this.redrawTracker = setInterval(this._$$doRedraw, this._updateInterval)
  136. if (this.redrawTracker.unref) this.redrawTracker.unref()
  137. }
  138. }
  139. Gauge.prototype._disableEvents = function () {
  140. this._tty.removeListener('resize', this._$$handleSizeChange)
  141. if (this._fixedFramerate) clearInterval(this.redrawTracker)
  142. if (this._removeOnExit) this._removeOnExit()
  143. }
  144. Gauge.prototype.hide = function (cb) {
  145. if (this._disabled) return cb && process.nextTick(cb)
  146. if (!this._showing) return cb && process.nextTick(cb)
  147. this._showing = false
  148. this._doRedraw()
  149. cb && setImmediate(cb)
  150. }
  151. Gauge.prototype.show = function (section, completed) {
  152. this._showing = true
  153. if (typeof section === 'string') {
  154. this._status.section = section
  155. } else if (typeof section === 'object') {
  156. var sectionKeys = Object.keys(section)
  157. for (var ii = 0; ii < sectionKeys.length; ++ii) {
  158. var key = sectionKeys[ii]
  159. this._status[key] = section[key]
  160. }
  161. }
  162. if (completed != null) this._status.completed = completed
  163. if (this._disabled) return
  164. this._requestRedraw()
  165. }
  166. Gauge.prototype.pulse = function (subsection) {
  167. this._status.subsection = subsection || ''
  168. this._status.spun ++
  169. if (this._disabled) return
  170. if (!this._showing) return
  171. this._requestRedraw()
  172. }
  173. Gauge.prototype._handleSizeChange = function () {
  174. this._gauge.setWidth(this._tty.columns - 1)
  175. this._requestRedraw()
  176. }
  177. Gauge.prototype._doRedraw = function () {
  178. if (this._disabled || this._paused) return
  179. if (!this._fixedFramerate) {
  180. var now = Date.now()
  181. if (this._lastUpdateAt && now - this._lastUpdateAt < this._updateInterval) return
  182. this._lastUpdateAt = now
  183. }
  184. if (!this._showing && this._onScreen) {
  185. this._onScreen = false
  186. var result = this._gauge.hide()
  187. if (this._hideCursor) {
  188. result += this._gauge.showCursor()
  189. }
  190. return this._writeTo.write(result)
  191. }
  192. if (!this._showing && !this._onScreen) return
  193. if (this._showing && !this._onScreen) {
  194. this._onScreen = true
  195. this._needsRedraw = true
  196. if (this._hideCursor) {
  197. this._writeTo.write(this._gauge.hideCursor())
  198. }
  199. }
  200. if (!this._needsRedraw) return
  201. if (!this._writeTo.write(this._gauge.show(this._status))) {
  202. this._paused = true
  203. this._writeTo.on('drain', callWith(this, function () {
  204. this._paused = false
  205. this._doRedraw()
  206. }))
  207. }
  208. }