Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. /* eslint-disable no-return-assign */
  2. const chalk = require('chalk');
  3. const ora = require('ora');
  4. const R = require('ramda');
  5. const prettyMs = require('pretty-ms');
  6.  
  7. /**
  8. * Creates progress bar with appendable statuses.
  9. * @param {String} [title=''] Top title of the bar.
  10. * @param {String} [fillChar=chalk.cyan('-')] Filling character.
  11. * @param {String} [bgChar=' '] Background character
  12. * @param {String} [startingChar=chalk.gray('[')] [description]
  13. * @param {String} [endingChar=chalk.gray(']')] [description]
  14. * @param {Number} [total=10] Aka length of the bar.
  15. * @returns {Object} With tick and append functions.
  16. */
  17. const createProgressBar = ({
  18. title = '',
  19. fillChar = chalk.cyan('-'),
  20. bgChar = ' ',
  21. startingChar = chalk.gray('['),
  22. endingChar = chalk.gray(']'),
  23. total = 10,
  24. }) => {
  25. const MAX_SCREEN_LENGTH = 10;
  26. const barLength = total > MAX_SCREEN_LENGTH ? MAX_SCREEN_LENGTH : total;
  27. const getCompleteness = progress => Math.round((barLength * progress) / total);
  28. let currentProgress = 0;
  29.  
  30. const fillProgress = () => fillChar.repeat(getCompleteness(currentProgress));
  31. const fillSpace = () => bgChar.repeat(barLength - getCompleteness(currentProgress));
  32.  
  33. const spinner = ora();
  34. // mutation of spinner text causes it to update
  35. const updateProgress = text => (spinner.text = text);
  36.  
  37. const appendedLines = [];
  38. const executionTimes = [];
  39. let timeOfLastTick;
  40.  
  41. return {
  42. tick() {
  43. let prependedHeader = '';
  44. // const normalizedProgress = getNormalizedProgress(currentProgress);
  45.  
  46. // start the spinner on first tick
  47. if (currentProgress === 0) {
  48. spinner.start();
  49. } else {
  50. const [, nanoseconds] = process.hrtime(timeOfLastTick);
  51. const timeBetweenTwoTicksInMs = nanoseconds / 1e6;
  52. executionTimes.push(timeBetweenTwoTicksInMs);
  53.  
  54. const mean = R.mean(executionTimes);
  55. const roundMeanStr = `${Math.round(mean)}ms`;
  56. const estTimeLeftStr = prettyMs(Math.round((mean * (total - currentProgress)) / 1000));
  57.  
  58. prependedHeader = chalk.grey(
  59. `Avg exec time: ${chalk.white(roundMeanStr)}, est. time left: ` +
  60. `${chalk.white(estTimeLeftStr)}`,
  61. );
  62. }
  63.  
  64. currentProgress += 1;
  65.  
  66. if (currentProgress >= total) {
  67. if (spinner.isSpinning) {
  68. spinner.succeed(`${title} πŸŽ‰`);
  69. }
  70.  
  71. return;
  72. }
  73.  
  74. const currentProgressFrame =
  75. `${title}\n` +
  76. `${currentProgress}/${total} ${startingChar}` +
  77. `${fillProgress()}${fillSpace()}${endingChar}\n` +
  78. `${prependedHeader ? `\n${prependedHeader}\n` : ''}` +
  79. `\n${appendedLines.join('\n')}`;
  80.  
  81. timeOfLastTick = process.hrtime();
  82. updateProgress(currentProgressFrame);
  83. },
  84.  
  85. append(text) {
  86. appendedLines.push(text);
  87. },
  88. };
  89. };
  90.  
  91. module.exports = createProgressBar;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement