progress.js 625 B

12345678910111213141516171819202122232425
  1. 'use strict'
  2. const readline = require('readline')
  3. /**
  4. * Adds an animated progress indicator
  5. *
  6. * @param {string} message The message to write next to the indicator
  7. * @param {number} amountOfDots The amount of dots you want to animate
  8. */
  9. function animateProgress (message, amountOfDots) {
  10. if (typeof amountOfDots !== 'number') {
  11. amountOfDots = 3
  12. }
  13. let i = 0
  14. return setInterval(function () {
  15. readline.cursorTo(process.stdout, 0)
  16. i = (i + 1) % (amountOfDots + 1)
  17. const dots = new Array(i + 1).join('.')
  18. process.stdout.write(message + dots)
  19. }, 500)
  20. }
  21. module.exports = animateProgress