-
-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Waffle wrote a PR (#986) that shows a progress bar when the database is populating. I pulled it into a local branch and made some adjustments. I've tested this on my end and it's working okay. In the compiled project, and directly in terminals (that *I have installed*) it looks great, but in VSCode's built in terminal you can see escape characters at the beginning of the line:  I can't figure out why, or how to get rid of it, but it doesn't really bother me much. --------- Co-authored-by: waffle-lord <[email protected]>
- Loading branch information
1 parent
7caec6e
commit 8926e28
Showing
2 changed files
with
59 additions
and
1 deletion.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import * as readline from "node:readline"; | ||
|
||
export class ProgressWriter { | ||
private count = 0; | ||
private total?: number; | ||
private done = false; | ||
private barFillChar: string; | ||
private barEmptyChar: string; | ||
private maxBarLength: number; | ||
|
||
constructor(total: number, maxBarLength = 25, barFillChar = "\u25A0", barEmptyChar = " ") { | ||
if (total <= 0) { | ||
throw new Error("Total must be a positive number."); | ||
} | ||
if ((barFillChar && barFillChar.length !== 1) || (barEmptyChar && barEmptyChar.length !== 1)) { | ||
throw new Error("Bar character values must be a single character."); | ||
} | ||
|
||
this.total = total; | ||
this.maxBarLength = maxBarLength; | ||
this.barFillChar = barFillChar; | ||
this.barEmptyChar = barEmptyChar; | ||
} | ||
|
||
/** | ||
* Increment the progress counter and update the progress bar display. | ||
*/ | ||
public increment(): void { | ||
if (this.done) { | ||
return; | ||
} | ||
|
||
this.count++; | ||
|
||
const progress = Math.floor((this.count / this.total) * 100); | ||
const filledChars = Math.floor((progress / 100) * this.maxBarLength); | ||
const emptyChars = this.maxBarLength - filledChars; | ||
|
||
const barFill = this.barFillChar.repeat(filledChars); | ||
const barEmptySpace = this.barEmptyChar.repeat(emptyChars); | ||
|
||
const progressBar = ` -> ${this.count} / ${this.total} [${barFill}${barEmptySpace}] ${progress}%`; | ||
|
||
readline.clearLine(process.stdout, 0); | ||
readline.cursorTo(process.stdout, 0, null); | ||
process.stdout.write(progressBar); | ||
|
||
if (progress === 100) { | ||
process.stdout.write("\n"); | ||
this.done = true; | ||
} | ||
} | ||
} |