sebassdc
11/27/2017 - 9:36 AM

[Progresive Bar] This is a port to javascript from https://gist.github.com/rougier/c0d31f5cbdaac27b876c#file-progress_bar-py #Javascript

[Progresive Bar] This is a port to javascript from https://gist.github.com/rougier/c0d31f5cbdaac27b876c#file-progress_bar-py #Javascript

//@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)
}