index.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. if (options.cleanupOnExit == null || options.cleanupOnExit) {
  59. onExit(callWith(this, this.disable))
  60. }
  61. if (options.enabled || (options.enabled == null && this._tty && this._tty.isTTY)) {
  62. this.enable()
  63. } else {
  64. this.disable()
  65. }
  66. }
  67. Gauge.prototype = {}
  68. Gauge.prototype.isEnabled = function () {
  69. return !this._disabled
  70. }
  71. Gauge.prototype.setTemplate = function (template) {
  72. this._gauge.setTemplate(template)
  73. if (this._showing) this._requestRedraw()
  74. }
  75. Gauge.prototype._computeTheme = function (theme) {
  76. if (!theme) theme = {}
  77. if (theme && (Object.keys(theme).length === 0 || theme.hasUnicode != null || theme.hasColor != null)) {
  78. var useUnicode = theme.hasUnicode == null ? hasUnicode() : theme.hasUnicode
  79. var useColor = theme.hasColor == null ? hasColor : theme.hasColor
  80. theme = this._themes.getDefault({hasUnicode: useUnicode, hasColor: useColor, platform: theme.platform})
  81. } else if (typeof theme === 'string') {
  82. theme = this._themes.getTheme(theme)
  83. }
  84. return theme
  85. }
  86. Gauge.prototype.setThemeset = function (themes) {
  87. this._themes = themes
  88. this.setTheme(this._theme)
  89. }
  90. Gauge.prototype.setTheme = function (theme) {
  91. this._gauge.setTheme(this._computeTheme(theme))
  92. if (this._showing) this._requestRedraw()
  93. this._theme = theme
  94. }
  95. Gauge.prototype._requestRedraw = function () {
  96. this._needsRedraw = true
  97. if (!this._fixedFramerate) this._doRedraw()
  98. }
  99. Gauge.prototype.getWidth = function () {
  100. return ((this._tty && this._tty.columns) || 80) - 1
  101. }
  102. Gauge.prototype.setWriteTo = function (writeTo, tty) {
  103. var enabled = !this._disabled
  104. if (enabled) this.disable()
  105. this._writeTo = writeTo
  106. this._tty = tty ||
  107. (writeTo === process.stderr && process.stdout.isTTY && process.stdout) ||
  108. (writeTo.isTTY && writeTo) ||
  109. this._tty
  110. if (this._gauge) this._gauge.setWidth(this.getWidth())
  111. if (enabled) this.enable()
  112. }
  113. Gauge.prototype.enable = function () {
  114. if (!this._disabled) return
  115. this._disabled = false
  116. if (this._tty) this._enableEvents()
  117. if (this._showing) this.show()
  118. }
  119. Gauge.prototype.disable = function () {
  120. if (this._disabled) return
  121. if (this._showing) {
  122. this._lastUpdateAt = null
  123. this._showing = false
  124. this._doRedraw()
  125. this._showing = true
  126. }
  127. this._disabled = true
  128. if (this._tty) this._disableEvents()
  129. }
  130. Gauge.prototype._enableEvents = function () {
  131. this._tty.on('resize', this._$$handleSizeChange)
  132. if (this._fixedFramerate) {
  133. this.redrawTracker = setInterval(this._$$doRedraw, this._updateInterval)
  134. if (this.redrawTracker.unref) this.redrawTracker.unref()
  135. }
  136. }
  137. Gauge.prototype._disableEvents = function () {
  138. this._tty.removeListener('resize', this._$$handleSizeChange)
  139. if (this._fixedFramerate) clearInterval(this.redrawTracker)
  140. }
  141. Gauge.prototype.hide = function (cb) {
  142. if (this._disabled) return cb && process.nextTick(cb)
  143. if (!this._showing) return cb && process.nextTick(cb)
  144. this._showing = false
  145. this._doRedraw()
  146. cb && setImmediate(cb)
  147. }
  148. Gauge.prototype.show = function (section, completed) {
  149. this._showing = true
  150. if (typeof section === 'string') {
  151. this._status.section = section
  152. } else if (typeof section === 'object') {
  153. var sectionKeys = Object.keys(section)
  154. for (var ii = 0; ii < sectionKeys.length; ++ii) {
  155. var key = sectionKeys[ii]
  156. this._status[key] = section[key]
  157. }
  158. }
  159. if (completed != null) this._status.completed = completed
  160. if (this._disabled) return
  161. this._requestRedraw()
  162. }
  163. Gauge.prototype.pulse = function (subsection) {
  164. this._status.subsection = subsection || ''
  165. this._status.spun ++
  166. if (this._disabled) return
  167. if (!this._showing) return
  168. this._requestRedraw()
  169. }
  170. Gauge.prototype._handleSizeChange = function () {
  171. this._gauge.setWidth(this._tty.columns - 1)
  172. this._requestRedraw()
  173. }
  174. Gauge.prototype._doRedraw = function () {
  175. if (this._disabled || this._paused) return
  176. if (!this._fixedFramerate) {
  177. var now = Date.now()
  178. if (this._lastUpdateAt && now - this._lastUpdateAt < this._updateInterval) return
  179. this._lastUpdateAt = now
  180. }
  181. if (!this._showing && this._onScreen) {
  182. this._onScreen = false
  183. var result = this._gauge.hide()
  184. if (this._hideCursor) {
  185. result += this._gauge.showCursor()
  186. }
  187. return this._writeTo.write(result)
  188. }
  189. if (!this._showing && !this._onScreen) return
  190. if (this._showing && !this._onScreen) {
  191. this._onScreen = true
  192. this._needsRedraw = true
  193. if (this._hideCursor) {
  194. this._writeTo.write(this._gauge.hideCursor())
  195. }
  196. }
  197. if (!this._needsRedraw) return
  198. if (!this._writeTo.write(this._gauge.show(this._status))) {
  199. this._paused = true
  200. this._writeTo.on('drain', callWith(this, function () {
  201. this._paused = false
  202. this._doRedraw()
  203. }))
  204. }
  205. }