Last active
December 13, 2017 05:07
-
-
Save sebassdc/19d86c4c72c67c0dca33526bafad265e to your computer and use it in GitHub Desktop.
[Progresive Bar] This is a port to javascript from https://gist.github.com/rougier/c0d31f5cbdaac27b876c#file-progress_bar-py #Javascript
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
//@ts-check | |
const progress = ({ | |
value, | |
length=40, | |
title = " ", | |
vmin=0.0, | |
vmax=1.0, | |
progressive = false | |
}) => { | |
// Block progression is 1/8 | |
const blocks = ["", "▏","▎","▍","▌","▋","▊","▉","█"] | |
const lsep = "▏", rsep = "▕" | |
// Normalize value | |
const normalized_value = (Math.min(Math.max(value, vmin), vmax)-vmin)/Number(vmax-vmin) | |
const v = normalized_value * length | |
const x = Math.floor(v) // integer part | |
const y = v - x // fractional part | |
const i = Math.round(y*8) | |
const bar = Array(x).fill("█").join("") + blocks[i] | |
const remaining = Array(length - bar.length).fill(" ").join("") | |
return `${lsep}${bar}${!progressive ? remaining : ""}${rsep} ${(Math.round(normalized_value * 100 * 100) / 100)}%` | |
} | |
let prevStr = "" | |
for (let i = 0; i < 1000; i++) { | |
prevStr = Array(prevStr.length).fill('\b').join('') + progress({value: i, vmin: 0, vmax: 999}) | |
process.stderr.write(prevStr) | |
} | |
prevStr = "" | |
for (let i = 0; i < 1000; i++) { | |
prevStr = Array(prevStr.length).fill('\b').join('') + progress({value: i, vmin: 0, vmax: 999, progressive: true}) | |
process.stderr.write(prevStr) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment