Created
November 14, 2018 10:49
-
-
Save quantumsheep/b7bb182bc09fb143d493ebae90833529 to your computer and use it in GitHub Desktop.
Progress bar generator function
This file contains hidden or 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
/** | |
* | |
* @param {number} percents / 100 | |
* @param {number} length | |
* @param {object} phases | |
* @param {string} phases.filled | |
* @param {string} phases.empty | |
* @param {string} phases.partial | |
*/ | |
function progress_bar(percents, length = 10, phases = { filled: '█', empty: '░', partial: '▓' }) { | |
let bar = phases.empty.repeat(Math.floor(length)); | |
const completed = Math.floor(percents) / 100 * length; | |
let i = 0; | |
for (; i + 1 <= completed; i++) { | |
bar = bar.slice(0, i) + phases.filled + bar.slice(i + 1); | |
} | |
if (phases.partial && (completed - Math.floor(completed)) >= 0.5 && completed < length) { | |
bar = bar.slice(0, i) + phases.partial + bar.slice(i + 1); | |
} | |
return bar; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment