Skip to content

Instantly share code, notes, and snippets.

@LunaSquee
Last active October 19, 2016 17:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LunaSquee/16ec2481869133d0f3d1543dbb380888 to your computer and use it in GitHub Desktop.
Save LunaSquee/16ec2481869133d0f3d1543dbb380888 to your computer and use it in GitHub Desktop.
JavaScript CLI-style progress bar with variable width
/**
* Example:
* progress_bar(40, 0.5);
* Returns:
* [####################-------------------] 50%
*
*/
function progress_bar(barsize, percentage) {
let bar = "";
let cnt = Math.floor(barsize * percentage);
for(let i=1;i<barsize;i++) {
if(i<=cnt)
bar += '#';
else
bar += '-';
}
return "["+bar+"] "+Math.floor(percentage * 100)+"%";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment