|
| 1 | +import { setTimeout as sleep } from 'node:timers/promises'; |
| 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 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + processSpinner.start('Starting to process selected languages...'); |
| 65 | + |
| 66 | + // Process each language with individual progress updates |
| 67 | + let completedCount = 0; |
| 68 | + const totalLanguages = languages.length; |
| 69 | + |
| 70 | + for (const language of languages) { |
| 71 | + // Skip the rest if cancelled |
| 72 | + if (processSpinner.isCancelled) break; |
| 73 | + |
| 74 | + // Update spinner message with current language |
| 75 | + processSpinner.message(`Processing ${language} (${completedCount + 1}/${totalLanguages})`); |
| 76 | + |
| 77 | + try { |
| 78 | + // Simulate work - longer pause to give time to test CTRL+C |
| 79 | + await sleep(2000); |
| 80 | + completedCount++; |
| 81 | + } catch (error) { |
| 82 | + // Handle errors but continue if not cancelled |
| 83 | + if (!processSpinner.isCancelled) { |
| 84 | + p.note(`Error processing ${language}: ${error.message}`, 'Error'); |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + // Stage 3: Handle completion based on cancellation status |
| 90 | + if (!processSpinner.isCancelled) { |
| 91 | + processSpinner.stop(`Processed ${completedCount}/${totalLanguages} languages successfully`); |
| 92 | + |
| 93 | + // Stage 4: Additional user input based on processing results |
| 94 | + if (completedCount > 0) { |
| 95 | + const action = await p.select({ |
| 96 | + message: 'What would you like to do with the processed data?', |
| 97 | + options: [ |
| 98 | + { value: 'save', label: 'Save results', hint: 'Write to disk' }, |
| 99 | + { value: 'share', label: 'Share results', hint: 'Upload to server' }, |
| 100 | + { value: 'analyze', label: 'Further analysis', hint: 'Generate reports' }, |
| 101 | + ], |
| 102 | + }); |
| 103 | + |
| 104 | + if (p.isCancel(action)) { |
| 105 | + p.cancel('Operation cancelled at final stage.'); |
| 106 | + process.exit(0); |
| 107 | + } |
| 108 | + |
| 109 | + // Stage 5: Final action with a timer spinner |
| 110 | + p.note('Now demonstrating a timer-style spinner', 'Final Stage'); |
| 111 | + |
| 112 | + const finalSpinner = p.spinner({ |
| 113 | + indicator: 'timer', // Use timer indicator for variety |
| 114 | + onCancel: () => { |
| 115 | + p.note( |
| 116 | + 'Final operation was cancelled, but processing results are still valid.', |
| 117 | + 'Final Stage Cancelled' |
| 118 | + ); |
| 119 | + }, |
| 120 | + }); |
| 121 | + |
| 122 | + finalSpinner.start(`Performing ${action} operation...`); |
| 123 | + |
| 124 | + try { |
| 125 | + // Simulate final action with incremental updates |
| 126 | + for (let i = 0; i < 3; i++) { |
| 127 | + if (finalSpinner.isCancelled) break; |
| 128 | + await sleep(1500); |
| 129 | + finalSpinner.message(`Performing ${action} operation... Step ${i + 1}/3`); |
| 130 | + } |
| 131 | + |
| 132 | + if (!finalSpinner.isCancelled) { |
| 133 | + finalSpinner.stop(`${action} operation completed successfully`); |
| 134 | + } |
| 135 | + } catch (error) { |
| 136 | + if (!finalSpinner.isCancelled) { |
| 137 | + finalSpinner.stop(`Error during ${action}: ${error.message}`); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + p.outro('Advanced demo completed. Thanks for trying out the spinner cancellation features!'); |
| 145 | +} |
| 146 | + |
| 147 | +main().catch((error) => { |
| 148 | + console.error('Unexpected error:', error); |
| 149 | + process.exit(1); |
| 150 | +}); |
0 commit comments