Last active
November 6, 2022 03:42
-
-
Save milcktoast/4c00d071a29db1b3242136e475e50549 to your computer and use it in GitHub Desktop.
Draw progress bar in Node JS
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
// 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