Last active
January 3, 2019 16:04
-
-
Save GUIEEN/a905b59ce26f8ef0192dc01fb5d6dc88 to your computer and use it in GitHub Desktop.
Animate loading message in console.log
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
const twirlTimer = function () { | |
let P = ['\\', '|', '/', '-'] | |
let x = 0 | |
return setInterval(function () { | |
process.stdout.write('\r' + P[x++]) | |
x &= 3 | |
}, 250) | |
} | |
const animateLoading = function (fullLength: number, current: number = 0) { | |
current = (current / fullLength) * 40 | |
fullLength = 40 | |
process.stdout.write('\r') | |
for (let i = 0; i < current; i++) { | |
process.stdout.write('▷') | |
} | |
for (let i = 0; i < fullLength - current; i++) { | |
process.stdout.write('*') | |
} | |
} | |
class LoadingConsole { | |
constructor ( | |
private loadingChrs = ['\\', '|', '/', '-'], | |
private timer = 0, | |
private interval = null | |
) {} | |
static run (loadingChrs, timer) { | |
return setInterval(function () { | |
process.stdout.write('\r' + loadingChrs[timer++]) | |
timer &= 3 | |
}, 250) | |
} | |
public start () { | |
this.interval = LoadingConsole.run(this.loadingChrs, this.timer) | |
} | |
public end () { | |
clearInterval(this.interval) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment