Skip to content

feat: added p.spinnerGroup API #158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions examples/basic/spinnerGroup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as p from '@clack/prompts';

p.intro('spinner groups start...');

const s = p.spinner();
s.start('example start');
await new Promise((resolve) => setTimeout(resolve, 500));
s.stop('example stopped');

await p.spinnerGroup('Outer group', [
[
'First sub-task',
async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
},
],
[
'Second sub-task',
async () => {
if (process.env.THROW_ERROR) {
throw new Error(process.env.THROW_ERROR);
}
await new Promise((resolve) => setTimeout(resolve, 1000));
},
],
[
'Third sub-task',
async () => {
await new Promise((resolve) => setTimeout(resolve, 1000));
},
],
]);

p.outro('spinner group stop...');
79 changes: 71 additions & 8 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import readline from 'node:readline';
import { stripVTControlCharacters as strip } from 'node:util';
import {
ConfirmPrompt,
Expand Down Expand Up @@ -641,10 +642,14 @@ export const log = {
},
};

function getIsCI() {
return process.env.CI === 'true';
}

export const spinner = () => {
const frames = unicode ? ['◒', '◐', '◓', '◑'] : ['•', 'o', 'O', '0'];
const delay = unicode ? 80 : 120;
const isCI = process.env.CI === 'true';
const isCI = getIsCI();

let unblock: () => void;
let loop: NodeJS.Timeout;
Expand Down Expand Up @@ -717,14 +722,8 @@ export const spinner = () => {
isSpinnerActive = false;
clearInterval(loop);
clearPrevMessage();
const step =
code === 0
? color.green(S_STEP_SUBMIT)
: code === 1
? color.red(S_STEP_CANCEL)
: color.red(S_STEP_ERROR);
_message = parseMessage(msg ?? _message);
process.stdout.write(`${step} ${_message}\n`);
process.stdout.write(`${stepForCode(code)} ${_message}\n`);
clearHooks();
unblock();
};
Expand All @@ -740,6 +739,70 @@ export const spinner = () => {
};
};

function stepForCode(code: number) {
return code === 0
? color.green(S_STEP_SUBMIT)
: code === 1
? color.red(S_STEP_CANCEL)
: color.red(S_STEP_ERROR);
}

export type SpinnerGroup = [message: string, run: () => Promise<void>];

export const spinnerGroup = async (outerMessage: string, groups: SpinnerGroup[]) => {
process.stdout.write(`${color.gray(S_BAR)}\n ${S_BAR_START} ${outerMessage}\n`);

const lines = getIsCI()
? {
clearLine: () => {},
clearScreenDown: () => {},
moveCursor: () => {},
}
: readline;

const s = spinner();
let caught: [group: SpinnerGroup, error: unknown] | undefined;

for (const [message, run] of groups) {
const line = `${color.gray(S_BAR)} ${message}`;

lines.clearLine(process.stdout, -1);
lines.moveCursor(process.stdout, -999, -1);

s.start(line);
await run().catch((error) => {
caught = [[message, run], error];
});
s.stop(line);

if (caught) {
break;
}
}

if (caught) {
lines.moveCursor(process.stdout, -999, -1);
process.stdout.write(
[
color.gray(S_BAR),
'',
stepForCode(1),
caught[0][0],
color.gray('>'),
(caught[1] as any).message || caught[1],
].join(' ')
);
} else {
lines.moveCursor(process.stdout, -999, -groups.length - 1);
lines.clearScreenDown(process.stdout);
process.stdout.write(`${stepForCode(0)} ${outerMessage}`);
}

process.stdout.write('\n');

return caught;
};

export type PromptGroupAwaitedReturn<T> = {
[P in keyof T]: Exclude<Awaited<T[P]>, symbol>;
};
Expand Down
Loading