Skip to content

Commit

Permalink
feat(@clack/prompts): new method spinner.message(msg: string)
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhihua committed Mar 24, 2023
1 parent 593f93d commit 89371be
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/loud-bugs-move.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@clack/prompts': minor
---

feat(@clack/prompts): new method `spinner.message(msg: string)`
3 changes: 2 additions & 1 deletion examples/basic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"picocolors": "^1.0.0"
},
"scripts": {
"start": "jiti ./index.ts"
"start": "jiti ./index.ts",
"spinner": "jiti ./spinner.ts"
},
"devDependencies": {
"jiti": "^1.17.0"
Expand Down
22 changes: 22 additions & 0 deletions examples/basic/spinner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import * as p from '@clack/prompts';

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

const spin = p.spinner();
const total = 10000;
let progress = 0;
spin.start();

new Promise((resolve) => {
const timer = setInterval(() => {
progress = Math.min(total, progress + 100);
if (progress >= total) {
clearInterval(timer);
resolve(true);
}
spin.message(`Loading packages [${progress}/${total}]`); // <===
}, 100);
}).then(() => {
spin.stop(`Done`);
p.outro('spinner stop...');
});
10 changes: 8 additions & 2 deletions packages/prompts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -607,9 +607,11 @@ const frames = unicode ? ['◒', '◐', '◓', '◑'] : ['•', 'o', 'O', '0'];
export const spinner = () => {
let unblock: () => void;
let loop: NodeJS.Timer;
let message = '';
const delay = unicode ? 80 : 120;
return {
start(message = '') {
start(msg = '') {
this.message(msg);
message = message.replace(/\.?\.?\.$/, '');
unblock = block();
process.stdout.write(`${color.gray(S_BAR)}\n${color.magenta('○')} ${message}\n`);
Expand All @@ -627,7 +629,11 @@ export const spinner = () => {
dot = dot === frames.length ? 0 : dot + 0.125;
}, delay);
},
stop(message = '') {
message(msg = '') {
message = msg ?? message;
},
stop(msg = '') {
this.message(msg);
process.stdout.write(cursor.move(-999, -2));
process.stdout.write(erase.down(2));
clearInterval(loop);
Expand Down

0 comments on commit 89371be

Please sign in to comment.