"Fossies" - the Fresh Open Source Software Archive

Member "Atom/resources/app/apm/node_modules/gauge/index.js" (8 Mar 2017, 6789 Bytes) of archive /windows/misc/atom-windows.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Javascript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

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