Skip to content

Commit

Permalink
fix: disable showing/hiding cursor while showing a progress bar and r…
Browse files Browse the repository at this point in the history
…emove the custom SIGINT handler.

Because emitting code is behaving similarly to an loop, handling SIGINT causes the handler to fire too late.
See nodejs/node#9050
  • Loading branch information
stefan-lacatus committed Apr 16, 2024
1 parent 9cdad7f commit 4b25939
Showing 1 changed file with 2 additions and 18 deletions.
20 changes: 2 additions & 18 deletions src/Utilities/ProgressBar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,11 @@ export class ProgressBar {
*/
private loggingBuffer: string[] = [];

/**
* A callback that is invoked on SIGINT and SIGTERM while the progress bar is running.
* Reenables the cursor and exists the process.
*/
private _sigintHandler = () => {
process.stdout.write('\u001B[?25h');
process.exit(1);
};

/**
* Draws the progress bar. Until invoking `stop`, you should not write to stdout.
*/
start() {
this._render();

// Reenable the cursor if the process stops
process.once('SIGINT', this._sigintHandler);
process.once('SIGTERM', this._sigintHandler);
}

/**
Expand Down Expand Up @@ -129,7 +116,7 @@ export class ProgressBar {
}

// Close the bar and draw the message, then hide the cursor
process.stdout.write(`${EndSymbol} | ${(this._progress * 100) | 0}%\n\x1b[2m${this._message}\x1b[0m\u001B[?25l`);
process.stdout.write(`${EndSymbol} | ${(this._progress * 100) | 0}%\n\x1b[2m${this._message}\x1b[0m`);
}

/**
Expand All @@ -151,10 +138,7 @@ export class ProgressBar {
* Stops the progress bar, moves the cursor to the next line and shows it.
*/
stop() {
process.stdout.write('\n\u001B[?25h');

process.off('SIGINT', this.destroy);
process.off('SIGTERM', this.destroy);
process.stdout.write('\n');
}

/**
Expand Down

0 comments on commit 4b25939

Please sign in to comment.