Skip to content

Commit

Permalink
Add ability to start spinner again after finishing (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
PondWader authored Nov 4, 2024
1 parent b1173af commit 6231501
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"author": "PondWader",
"description": "A lightweight, no dependency, pluggable CLI spinner library.",
"scripts": {
"build": "tsup ./src/index.ts --format cjs,esm --dts --minify",
"build": "tsup ./src/index.ts --format cjs,esm --dts",
"format": "prettier --write src",
"test": "tsc && c8 --exclude-after-remap node --test",
"lint": "npm run lint:format && eslint src",
Expand Down
24 changes: 11 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export class Spinner {

start(tickMs = constants.DEFAULT_TICK_MS) {
if (this.running) throw new Error('Spinner is already running.');
if (this.component.finished) this.component.finished = false;

this.interval = setInterval(this.tick.bind(this), tickMs);
this.running = true;
Expand All @@ -62,19 +63,17 @@ export class Spinner {
}

private onProcessExit = (signal?: string | number) => {
if (this.running) {
this.stop();
// SIGTERM is 15, SIGINT is 2
let signalCode;
if (signal === 'SIGTERM') {
signalCode = 15 + 128;
} else if (signal === 'SIGINT') {
signalCode = 2 + 128;
} else {
signalCode = Number(signal);
}
process.exit(signalCode);
this.stop();
// SIGTERM is 15, SIGINT is 2
let signalCode;
if (signal === 'SIGTERM') {
signalCode = 15 + 128;
} else if (signal === 'SIGINT') {
signalCode = 2 + 128;
} else {
signalCode = Number(signal);
}
process.exit(signalCode);
};

private addListeners() {
Expand Down Expand Up @@ -111,7 +110,6 @@ export class Spinner {
setText(text: string, render = true) {
this.text = text;
if (this.running) {
// Clear width cache
if (render) this.refresh();
}
}
Expand Down
21 changes: 20 additions & 1 deletion src/test/spinner-test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as assert from 'node:assert/strict';
import {test} from 'node:test';
import {Spinner, Symbols, renderer} from '../index.js';
import {createFinishingRenderedLine, createRenderedLine, createRenderedOutput, interceptStdout, TickMeasuredSpinner} from './utils.js';
import {createFinishingRenderedLine, createRenderedLine, createRenderedOutput, interceptStdout, suppressStdout, TickMeasuredSpinner} from './utils.js';
import process from 'node:process';
import * as constants from '../constants.js';

Expand Down Expand Up @@ -110,6 +110,25 @@ test('end methods', async (t) => {
process.exit = originalExit;
}
});

await t.test('restart finished spinner with new component', async () => {
const unsuppressStdout = suppressStdout();

const spinner = new Spinner();
try {
spinner.start();
spinner.succeed();
assert.ok(spinner['component'].finished, 'component is finished');
spinner.start();
assert.ok(!spinner['component'].finished, 'component is not finished');
spinner.stop();
} catch (err) {
spinner.stop();
throw err;
} finally {
unsuppressStdout();
}
});
});

async function testSpinner(frames?: string[], text?: string, symbolFormatter?: (v: string) => string) {
Expand Down

0 comments on commit 6231501

Please sign in to comment.