Skip to content

Commit

Permalink
Database Import Progress Bar (#987)
Browse files Browse the repository at this point in the history
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:


![image](https://github.com/user-attachments/assets/c547a675-5331-44f3-a44e-940edaba6fe8)

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
refringe and waffle-lord authored Dec 19, 2024
1 parent 7caec6e commit 8926e28
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 1 deletion.
7 changes: 6 additions & 1 deletion project/src/utils/ImporterUtil.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { JsonUtil } from "@spt/utils/JsonUtil";
import { ProgressWriter } from "@spt/utils/ProgressWriter";
import { VFS } from "@spt/utils/VFS";
import { Queue } from "@spt/utils/collections/queue/Queue";
import { inject, injectable } from "tsyringe";
Expand Down Expand Up @@ -119,9 +120,12 @@ export class ImporterUtil {
directoriesToRead.enqueueAll(this.vfs.getDirs(directory).map((d) => `${directory}/${d}`));
}

const progressWriter = new ProgressWriter(filesToProcess.length);

while (filesToProcess.length !== 0) {
const fileNode = filesToProcess.dequeue();
if (!fileNode) continue;

if (this.vfs.getFileExtension(fileNode.fileName) === "json") {
const filePathAndName = `${fileNode.filePath}${fileNode.fileName}`;
promises.push(
Expand All @@ -135,7 +139,8 @@ export class ImporterUtil {
onObjectDeserialized(filePathAndName, fileDeserialized);
const strippedFilePath = this.vfs.stripExtension(filePathAndName).replace(filepath, "");
this.placeObject(fileDeserialized, strippedFilePath, result, strippablePath);
}),
})
.then(() => progressWriter.increment()),
);
}
}
Expand Down
53 changes: 53 additions & 0 deletions project/src/utils/ProgressWriter.ts
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;
}
}
}

0 comments on commit 8926e28

Please sign in to comment.