nathanpeck / clui

Command Line UI toolkit for Node.js
MIT License
1.66k stars 40 forks source link

Spinner with manual ticking #20

Open Bizarrus opened 6 years ago

Bizarrus commented 6 years ago

I wan't to tick manually the spinner.

For sample

var countdown  = new CLI.Spinner('Running...  ', ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷']);
countdown.start();

let's run automatic. But i want to "move" the spinner programatically like

var countdown  = new CLI.Spinner('Running...  ', ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷']);

setInterval(function() {
    countdown.tick();
}, 1000); // each second, the spinner moves...
danjaywing commented 6 years ago

Well the symbols are in the order they are to be animated, so why not just output and replace each symbol every tick?

So something like

var spinnerSymbols = ['⣾','⣽','⣻','⢿','⡿','⣟','⣯','⣷'];
var i = 0;
setInterval(function() {
  process.stdout.clearLine();  // clear line
  process.stdout.cursorTo(0); // move to the start of the line
  process.stdout.write(spinnerSymbols[i]); // write the symbol

  // Increase the counter until it reaches the end of the array
  if(i >= (spinnerSymbols.length - 1)) {
    i = 0;
  } else {
    i++;
  }
}, 1000); // each second, the spinner moves...