| 66 | } |
| 67 | |
| 68 | function createTtyPrompter(): Prompter { |
| 69 | return { |
| 70 | async selectOne<T>(opts: { |
| 71 | message: string; |
| 72 | options: SelectOption<T>[]; |
| 73 | initialIndex?: number; |
| 74 | }): Promise<T> { |
| 75 | if (opts.options.length === 0) { |
| 76 | throw new Error('No options available for selection.'); |
| 77 | } |
| 78 | |
| 79 | const initialIndex = clampIndex(opts.initialIndex ?? 0, opts.options.length); |
| 80 | |
| 81 | const promptOptions = opts.options.map((option) => ({ |
| 82 | value: option.value, |
| 83 | label: option.label, |
| 84 | ...(option.description ? { hint: option.description } : {}), |
| 85 | })) as unknown as clack.Option<T>[]; |
| 86 | |
| 87 | const result = await clack.select<T>({ |
| 88 | message: opts.message, |
| 89 | options: promptOptions, |
| 90 | initialValue: opts.options[initialIndex].value, |
| 91 | }); |
| 92 | |
| 93 | handleCancel(result); |
| 94 | return result as T; |
| 95 | }, |
| 96 | |
| 97 | async selectMany<T>(opts: { |
| 98 | message: string; |
| 99 | options: SelectOption<T>[]; |
| 100 | initialSelectedKeys?: ReadonlySet<string>; |
| 101 | getKey: (value: T) => string; |
| 102 | minSelected?: number; |
| 103 | }): Promise<T[]> { |
| 104 | if (opts.options.length === 0) { |
| 105 | return []; |
| 106 | } |
| 107 | |
| 108 | const initialKeys = opts.initialSelectedKeys ?? new Set<string>(); |
| 109 | const initialValues = opts.options |
| 110 | .filter((option) => initialKeys.has(opts.getKey(option.value))) |
| 111 | .map((option) => option.value); |
| 112 | |
| 113 | const promptOptions = opts.options.map((option) => ({ |
| 114 | value: option.value, |
| 115 | label: option.label, |
| 116 | ...(option.description ? { hint: option.description } : {}), |
| 117 | })) as unknown as clack.Option<T>[]; |
| 118 | |
| 119 | const result = await clack.multiselect<T>({ |
| 120 | message: opts.message, |
| 121 | options: promptOptions, |
| 122 | initialValues, |
| 123 | required: (opts.minSelected ?? 0) > 0, |
| 124 | }); |
| 125 | |