Skip to content

Instantly share code, notes, and snippets.

@milcktoast
Last active November 6, 2022 03:42
Show Gist options
  • Save milcktoast/4c00d071a29db1b3242136e475e50549 to your computer and use it in GitHub Desktop.
Save milcktoast/4c00d071a29db1b3242136e475e50549 to your computer and use it in GitHub Desktop.
Draw progress bar in Node JS
// cols (int) – character width
// progress (float) – progress in the range (0-1)
function drawProgress (cols, progress) {
const { stdout } = process
let c0 = Math.round(progress * cols)
let c1 = cols - c0
let b0 = new Array(c0).fill('/').join('')
let b1 = new Array(c1).fill('.').join('')
stdout.clearLine()
stdout.cursorTo(0)
stdout.write(`${b0}${b1}`)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment