npmtest-pm2 (v0.0.3)

Code coverage report for node-npmtest-pm2/node_modules/pm2/lib/API/Spinner.js

Statements: 29.41% (10 / 34)      Branches: 0% (0 / 4)      Functions: 0% (0 / 11)      Lines: 29.41% (10 / 34)      Ignored: none     

All files » node-npmtest-pm2/node_modules/pm2/lib/API/ » Spinner.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79          1                         1         1       1                           1       1                     1       1           1         1              
/**
 * Spinner
 * Handle TTY and non-TTY based terminals
 */
 
var defaultSpinnerString = [
  "⠋",
  "⠙",
  "⠹",
  "⠸",
  "⠼",
  "⠴",
  "⠦",
  "⠧",
  "⠇",
  "⠏"
].join('');
 
var InteractiveSpinner = function(textToShow){
  this.text = textToShow || '';
  this.setSpinnerString(defaultSpinnerString); // use default spinner string
};
 
InteractiveSpinner.setDefaultSpinnerString = function(value) {
  defaultSpinnerString = value;
};
 
InteractiveSpinner.prototype.start = function() {
  var current = 0;
  var self = this;
  this.id = setInterval(function() {
    try {
      process.stdout.clearLine();
      process.stdout.cursorTo(0);
      process.stdout.write(self.chars[current] + ' ' + self.text);
      current = ++current % self.chars.length;
    } catch(e) { // ignore error if term is not tty, just display nothing
    }
  }, 80);
};
 
InteractiveSpinner.prototype.setSpinnerString = function(str) {
  this.chars = str.split("");
};
 
InteractiveSpinner.prototype.stop = function() {
  try {
    process.stdout.clearLine();
    process.stdout.cursorTo(0);
  } catch(e) {}
  clearInterval(this.id);
};
 
/**
 * Display dots if non TTY terminal
 */
var StaticSpinner = function(text) {
  console.log(text);
}
 
StaticSpinner.prototype.start = function() {
  this.interval = setInterval(function() {
    process.stdout.write('.');
  }, 500);
};
 
StaticSpinner.prototype.stop = function() {
  clearInterval(this.interval);
  console.log();
};
 
module.exports = function(text) {
  if (process.stdout.isTTY)
    return new InteractiveSpinner(text);
  else
    return new StaticSpinner(text);
};