ProgressBar.js 1.0 KB

12345678910111213141516171819
  1. /**
  2. * Create a text progress bar
  3. * @param {Number} value - The value to fill the bar
  4. * @param {Number} maxValue - The max value of the bar
  5. * @param {Number} size - The bar size (in letters)
  6. * @return {{Bar: string, percentageText: string}} - The bar
  7. */
  8. module.exports = (value, maxValue, size) => {
  9. const percentage = value / maxValue; // Calculate the percentage of the bar
  10. const progress = Math.round(size * percentage); // Calculate the number of square caracters to fill the progress side.
  11. const emptyProgress = size - progress; // Calculate the number of dash caracters to fill the empty progress side.
  12. const progressText = "▇".repeat(progress); // Repeat is creating a string with progress * caracters in it
  13. const emptyProgressText = "—".repeat(emptyProgress); // Repeat is creating a string with empty progress * caracters in it
  14. const percentageText = Math.round(percentage * 100) + "%"; // Displaying the percentage of the bar
  15. const Bar = progressText + emptyProgressText; // Creating the bar
  16. return { Bar, percentageText };
  17. };