Skip to content

Instantly share code, notes, and snippets.

@guysherman
Last active September 12, 2019 23:15
Show Gist options
  • Save guysherman/34c3025ee5a929e7d723f7b5e2336b0a to your computer and use it in GitHub Desktop.
Save guysherman/34c3025ee5a929e7d723f7b5e2336b0a to your computer and use it in GitHub Desktop.
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