-
-
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.
Adds a class called `ProgressWriter` used for creating progress bars in the server terminal output. Made updates to use the new class to display a progress bar for when JSON files are being validated and loaded in the database.
- Loading branch information
1 parent
7caec6e
commit 8e0f57a
Showing
2 changed files
with
41 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,35 @@ | ||
export class ProgressWriter { | ||
count = 0; | ||
total: number; | ||
done = false; | ||
|
||
constructor(total: number) { | ||
this.total = total; | ||
} | ||
|
||
public increment(): void { | ||
if (this.done) { | ||
return; | ||
} | ||
|
||
this.count++; | ||
|
||
const progress = Math.floor((this.count / this.total) * 100); | ||
|
||
// reduce bar fill max to 50 characters to save space | ||
const progressHalved = Math.floor(progress / 4); | ||
|
||
const barFill = "=".repeat(progressHalved); | ||
const barEmptySpace = " ".repeat(Math.floor(25 - progressHalved)); | ||
|
||
const progressBar = ` -> ${this.count} / ${this.total} [${barFill}${barEmptySpace}] ${progress}%`; | ||
|
||
process.stdout.write(progressBar); | ||
process.stdout.cursorTo(0); | ||
|
||
if (progress === 100) { | ||
process.stdout.write("\n"); | ||
this.done = true; | ||
} | ||
} | ||
} |