forked from bombshell-dev/clack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspinner-cancel.ts
More file actions
42 lines (33 loc) · 1.11 KB
/
spinner-cancel.ts
File metadata and controls
42 lines (33 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import * as p from '@clack/prompts';
p.intro('Spinner with cancellation detection');
// Example 1: Using onCancel callback
const spin1 = p.spinner({
indicator: 'dots',
onCancel: () => {
p.note('You cancelled the spinner with CTRL-C!', 'Callback detected');
},
});
spin1.start('Press CTRL-C to cancel this spinner (using callback)');
// Sleep for 10 seconds, allowing time for user to press CTRL-C
await sleep(10000).then(() => {
// Only show success message if not cancelled
if (!spin1.isCancelled) {
spin1.stop('Spinner completed without cancellation');
}
});
// Example 2: Checking the isCancelled property
p.note('Starting second example...', 'Example 2');
const spin2 = p.spinner({ indicator: 'timer' });
spin2.start('Press CTRL-C to cancel this spinner (polling isCancelled)');
await sleep(10000).then(() => {
if (spin2.isCancelled) {
p.note('Spinner was cancelled by the user!', 'Property check');
} else {
spin2.stop('Spinner completed without cancellation');
}
});
p.outro('Example completed');
// Helper function
function sleep(ms: number) {
return new Promise((resolve) => setTimeout(resolve, ms));
}