Created
October 5, 2024 09:05
-
-
Save miohtama/63e96ff6d7b61584284000ec4989a0af to your computer and use it in GitHub Desktop.
Render a text mode progress bar in node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Render a text mode progress bar. | |
* | |
* For characters see | |
* | |
* - https://en.wikipedia.org/wiki/Geometric_Shapes_(Unicode_block) | |
* - https://en.wikipedia.org/wiki/Block_Elements | |
* | |
* @param {number} width Width in characters | |
* @param {number} progress Current progress as percent 0....1 | |
* @param {string} start Start character | |
* @param {string} end End character | |
* @param {string} fill Fill character | |
* @param {string} empty Empty area character | |
* @param {string} cursor The current progress indicator | |
* @returns {string} Progress bar to be printed to the console | |
*/ | |
function renderProgressBar( | |
width, | |
progress, | |
start, | |
end, | |
fill, | |
empty, | |
cursor, | |
) { | |
const filledWidth = Math.round(width * progress); | |
const emptyWidth = width - filledWidth - 1; | |
return start + | |
fill.repeat(filledWidth) + | |
cursor + | |
empty.repeat(emptyWidth) + | |
end; | |
} | |
// Example usage: | |
console.log(renderProgressBar(60, 0.75, '▐', '▌', '▒', '·', '▷')); | |
// Output: ▐▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▒▷··············▌ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment