Last active
September 12, 2019 23:15
-
-
Save guysherman/34c3025ee5a929e7d723f7b5e2336b0a to your computer and use it in GitHub Desktop.
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
const argv = process.execArgv.join(); | |
const isDebug = argv.includes('inspect') || argv.includes('debug'); | |
class ProgressReporter { | |
constructor(prefix) { | |
this.prefix = prefix; | |
this.lastProgress = 0; | |
} | |
printProgress(percent, isFinal) { | |
if (percent === 0) this.lastProgress = 0; | |
if (percent > this.lastProgress) { | |
const output = `${this.prefix}: Progress: ${percent}%`; | |
if (isDebug) { | |
console.log(output); | |
} else { | |
process.stdout.clearLine(); | |
process.stdout.cursorTo(0); | |
process.stdout.write(output); | |
if (isFinal) { | |
process.stdout.write('\n'); | |
} | |
} | |
this.lastProgress = percent; | |
} | |
} | |
} | |
module.exports = (prefix) => { | |
return new ProgressReporter(prefix); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment