You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.js 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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) {
  73. this._requestRedraw()
  74. }
  75. }
  76. Gauge.prototype._computeTheme = function (theme) {
  77. if (!theme) {
  78. theme = {}
  79. }
  80. if (typeof theme === 'string') {
  81. theme = this._themes.getTheme(theme)
  82. } else if (
  83. Object.keys(theme).length === 0 || theme.hasUnicode != null || theme.hasColor != null
  84. ) {
  85. var useUnicode = theme.hasUnicode == null ? hasUnicode() : theme.hasUnicode
  86. var useColor = theme.hasColor == null ? hasColor : theme.hasColor
  87. theme = this._themes.getDefault({
  88. hasUnicode: useUnicode,
  89. hasColor: useColor,
  90. platform: theme.platform,
  91. })
  92. }
  93. return theme
  94. }
  95. Gauge.prototype.setThemeset = function (themes) {
  96. this._themes = themes
  97. this.setTheme(this._theme)
  98. }
  99. Gauge.prototype.setTheme = function (theme) {
  100. this._gauge.setTheme(this._computeTheme(theme))
  101. if (this._showing) {
  102. this._requestRedraw()
  103. }
  104. this._theme = theme
  105. }
  106. Gauge.prototype._requestRedraw = function () {
  107. this._needsRedraw = true
  108. if (!this._fixedFramerate) {
  109. this._doRedraw()
  110. }
  111. }
  112. Gauge.prototype.getWidth = function () {
  113. return ((this._tty && this._tty.columns) || 80) - 1
  114. }
  115. Gauge.prototype.setWriteTo = function (writeTo, tty) {
  116. var enabled = !this._disabled
  117. if (enabled) {
  118. this.disable()
  119. }
  120. this._writeTo = writeTo
  121. this._tty = tty ||
  122. (writeTo === process.stderr && process.stdout.isTTY && process.stdout) ||
  123. (writeTo.isTTY && writeTo) ||
  124. this._tty
  125. if (this._gauge) {
  126. this._gauge.setWidth(this.getWidth())
  127. }
  128. if (enabled) {
  129. this.enable()
  130. }
  131. }
  132. Gauge.prototype.enable = function () {
  133. if (!this._disabled) {
  134. return
  135. }
  136. this._disabled = false
  137. if (this._tty) {
  138. this._enableEvents()
  139. }
  140. if (this._showing) {
  141. this.show()
  142. }
  143. }
  144. Gauge.prototype.disable = function () {
  145. if (this._disabled) {
  146. return
  147. }
  148. if (this._showing) {
  149. this._lastUpdateAt = null
  150. this._showing = false
  151. this._doRedraw()
  152. this._showing = true
  153. }
  154. this._disabled = true
  155. if (this._tty) {
  156. this._disableEvents()
  157. }
  158. }
  159. Gauge.prototype._enableEvents = function () {
  160. if (this._cleanupOnExit) {
  161. this._removeOnExit = onExit(callWith(this, this.disable))
  162. }
  163. this._tty.on('resize', this._$$handleSizeChange)
  164. if (this._fixedFramerate) {
  165. this.redrawTracker = setInterval(this._$$doRedraw, this._updateInterval)
  166. if (this.redrawTracker.unref) {
  167. this.redrawTracker.unref()
  168. }
  169. }
  170. }
  171. Gauge.prototype._disableEvents = function () {
  172. this._tty.removeListener('resize', this._$$handleSizeChange)
  173. if (this._fixedFramerate) {
  174. clearInterval(this.redrawTracker)
  175. }
  176. if (this._removeOnExit) {
  177. this._removeOnExit()
  178. }
  179. }
  180. Gauge.prototype.hide = function (cb) {
  181. if (this._disabled) {
  182. return cb && process.nextTick(cb)
  183. }
  184. if (!this._showing) {
  185. return cb && process.nextTick(cb)
  186. }
  187. this._showing = false
  188. this._doRedraw()
  189. cb && setImmediate(cb)
  190. }
  191. Gauge.prototype.show = function (section, completed) {
  192. this._showing = true
  193. if (typeof section === 'string') {
  194. this._status.section = section
  195. } else if (typeof section === 'object') {
  196. var sectionKeys = Object.keys(section)
  197. for (var ii = 0; ii < sectionKeys.length; ++ii) {
  198. var key = sectionKeys[ii]
  199. this._status[key] = section[key]
  200. }
  201. }
  202. if (completed != null) {
  203. this._status.completed = completed
  204. }
  205. if (this._disabled) {
  206. return
  207. }
  208. this._requestRedraw()
  209. }
  210. Gauge.prototype.pulse = function (subsection) {
  211. this._status.subsection = subsection || ''
  212. this._status.spun++
  213. if (this._disabled) {
  214. return
  215. }
  216. if (!this._showing) {
  217. return
  218. }
  219. this._requestRedraw()
  220. }
  221. Gauge.prototype._handleSizeChange = function () {
  222. this._gauge.setWidth(this._tty.columns - 1)
  223. this._requestRedraw()
  224. }
  225. Gauge.prototype._doRedraw = function () {
  226. if (this._disabled || this._paused) {
  227. return
  228. }
  229. if (!this._fixedFramerate) {
  230. var now = Date.now()
  231. if (this._lastUpdateAt && now - this._lastUpdateAt < this._updateInterval) {
  232. return
  233. }
  234. this._lastUpdateAt = now
  235. }
  236. if (!this._showing && this._onScreen) {
  237. this._onScreen = false
  238. var result = this._gauge.hide()
  239. if (this._hideCursor) {
  240. result += this._gauge.showCursor()
  241. }
  242. return this._writeTo.write(result)
  243. }
  244. if (!this._showing && !this._onScreen) {
  245. return
  246. }
  247. if (this._showing && !this._onScreen) {
  248. this._onScreen = true
  249. this._needsRedraw = true
  250. if (this._hideCursor) {
  251. this._writeTo.write(this._gauge.hideCursor())
  252. }
  253. }
  254. if (!this._needsRedraw) {
  255. return
  256. }
  257. if (!this._writeTo.write(this._gauge.show(this._status))) {
  258. this._paused = true
  259. this._writeTo.on('drain', callWith(this, function () {
  260. this._paused = false
  261. this._doRedraw()
  262. }))
  263. }
  264. }