| 2 | import * as p from '@clack/prompts'; |
| 3 | |
| 4 | async function main() { |
| 5 | p.intro('Advanced Spinner Cancellation Demo'); |
| 6 | |
| 7 | // First demonstrate a visible spinner with no user input needed |
| 8 | p.note('First, we will show a basic spinner (press CTRL+C to cancel)', 'Demo Part 1'); |
| 9 | |
| 10 | const demoSpinner = p.spinner({ |
| 11 | indicator: 'dots', |
| 12 | onCancel: () => { |
| 13 | p.note('Initial spinner was cancelled with CTRL+C', 'Demo Cancelled'); |
| 14 | }, |
| 15 | }); |
| 16 | |
| 17 | demoSpinner.start('Loading demo resources'); |
| 18 | |
| 19 | // Update spinner message a few times to show activity |
| 20 | for (let i = 0; i < 5; i++) { |
| 21 | if (demoSpinner.isCancelled) break; |
| 22 | await sleep(1000); |
| 23 | demoSpinner.message(`Loading demo resources (${i + 1}/5)`); |
| 24 | } |
| 25 | |
| 26 | if (!demoSpinner.isCancelled) { |
| 27 | demoSpinner.stop('Demo resources loaded successfully'); |
| 28 | } |
| 29 | |
| 30 | // Only continue with the rest of the demo if the initial spinner wasn't cancelled |
| 31 | if (!demoSpinner.isCancelled) { |
| 32 | // Stage 1: Get user input with multiselect |
| 33 | p.note("Now let's select some languages to process", 'Demo Part 2'); |
| 34 | |
| 35 | const languages = await p.multiselect({ |
| 36 | message: 'Select programming languages to process:', |
| 37 | options: [ |
| 38 | { value: 'typescript', label: 'TypeScript' }, |
| 39 | { value: 'javascript', label: 'JavaScript' }, |
| 40 | { value: 'python', label: 'Python' }, |
| 41 | { value: 'rust', label: 'Rust' }, |
| 42 | { value: 'go', label: 'Go' }, |
| 43 | ], |
| 44 | required: true, |
| 45 | }); |
| 46 | |
| 47 | // Handle cancellation of the multiselect |
| 48 | if (p.isCancel(languages)) { |
| 49 | p.cancel('Operation cancelled during language selection.'); |
| 50 | process.exit(0); |
| 51 | } |
| 52 | |
| 53 | // Stage 2: Show a spinner that can be cancelled |
| 54 | const processSpinner = p.spinner({ |
| 55 | indicator: 'dots', |
| 56 | onCancel: () => { |
| 57 | p.note( |
| 58 | 'You cancelled during processing. Any completed work will be saved.', |
| 59 | 'Processing Cancelled' |
| 60 | ); |
| 61 | }, |