|
1 | 1 | import { styleText } from 'node:util'; |
2 | | -import { GroupMultiSelectPrompt, settings } from '@clack/core'; |
| 2 | +import { GroupMultiSelectPrompt, settings, wrapTextWithPrefix } from '@clack/core'; |
3 | 3 | import { |
4 | 4 | type CommonOptions, |
5 | 5 | S_BAR, |
@@ -41,44 +41,87 @@ export const groupMultiselect = <Value>(opts: GroupMultiSelectOptions<Value>) => |
41 | 41 | const isItem = typeof option.group === 'string'; |
42 | 42 | const next = isItem && (options[options.indexOf(option) + 1] ?? { group: true }); |
43 | 43 | const isLast = isItem && next && next.group === true; |
44 | | - const prefix = isItem ? (selectableGroups ? `${isLast ? S_BAR_END : S_BAR} ` : ' ') : ''; |
| 44 | + let prefix = ''; |
| 45 | + let prefixEnd = ''; |
| 46 | + if (isItem) { |
| 47 | + if (selectableGroups) { |
| 48 | + prefix = isLast ? `${S_BAR_END} ` : `${S_BAR} `; |
| 49 | + prefixEnd = isLast ? ` ` : `${S_BAR} `; |
| 50 | + } else { |
| 51 | + prefix = ' '; |
| 52 | + } |
| 53 | + } |
45 | 54 | let spacingPrefix = ''; |
46 | 55 | if (groupSpacing > 0 && !isItem) { |
47 | 56 | spacingPrefix = '\n'.repeat(groupSpacing); |
48 | 57 | } |
49 | 58 |
|
50 | 59 | if (state === 'active') { |
51 | | - return `${spacingPrefix}${styleText('dim', prefix)}${styleText('cyan', S_CHECKBOX_ACTIVE)} ${label}${ |
52 | | - option.hint ? ` ${styleText('dim', `(${option.hint})`)}` : '' |
53 | | - }`; |
| 60 | + return wrapTextWithPrefix( |
| 61 | + opts.output, |
| 62 | + `${label}${option.hint ? ` ${styleText('dim', `(${option.hint})`)}` : ''}`, |
| 63 | + `${spacingPrefix}${styleText('dim', prefix)} `, |
| 64 | + `${spacingPrefix}${styleText('dim', prefix)}${styleText('cyan', S_CHECKBOX_ACTIVE)} `, |
| 65 | + `${spacingPrefix}${styleText('dim', prefixEnd)} ` |
| 66 | + ); |
54 | 67 | } |
55 | 68 | if (state === 'group-active') { |
56 | | - return `${spacingPrefix}${prefix}${styleText('cyan', S_CHECKBOX_ACTIVE)} ${styleText('dim', label)}`; |
| 69 | + return wrapTextWithPrefix( |
| 70 | + opts.output, |
| 71 | + label, |
| 72 | + `${spacingPrefix}${prefix} `, |
| 73 | + `${spacingPrefix}${prefix}${styleText('cyan', S_CHECKBOX_ACTIVE)} `, |
| 74 | + `${spacingPrefix}${prefixEnd} `, |
| 75 | + (str) => styleText('dim', str) |
| 76 | + ); |
57 | 77 | } |
58 | 78 | if (state === 'group-active-selected') { |
59 | | - return `${spacingPrefix}${prefix}${styleText('green', S_CHECKBOX_SELECTED)} ${styleText('dim', label)}`; |
| 79 | + return wrapTextWithPrefix( |
| 80 | + opts.output, |
| 81 | + label, |
| 82 | + `${spacingPrefix}${prefix} `, |
| 83 | + `${spacingPrefix}${prefix}${styleText('green', S_CHECKBOX_SELECTED)} `, |
| 84 | + `${spacingPrefix}${prefixEnd} `, |
| 85 | + (str) => styleText('dim', str) |
| 86 | + ); |
60 | 87 | } |
61 | 88 | if (state === 'selected') { |
62 | 89 | const selectedCheckbox = |
63 | 90 | isItem || selectableGroups ? styleText('green', S_CHECKBOX_SELECTED) : ''; |
64 | | - return `${spacingPrefix}${styleText('dim', prefix)}${selectedCheckbox} ${styleText('dim', label)}${ |
65 | | - option.hint ? ` ${styleText('dim', `(${option.hint})`)}` : '' |
66 | | - }`; |
| 91 | + return wrapTextWithPrefix( |
| 92 | + opts.output, |
| 93 | + `${label}${option.hint ? ` (${option.hint})` : ''}`, |
| 94 | + `${spacingPrefix}${styleText('dim', prefix)} `, |
| 95 | + `${spacingPrefix}${styleText('dim', prefix)}${selectedCheckbox} `, |
| 96 | + `${spacingPrefix}${styleText('dim', prefixEnd)} `, |
| 97 | + (str) => styleText('dim', str) |
| 98 | + ); |
67 | 99 | } |
68 | 100 | if (state === 'cancelled') { |
69 | 101 | return `${styleText(['strikethrough', 'dim'], label)}`; |
70 | 102 | } |
71 | 103 | if (state === 'active-selected') { |
72 | | - return `${spacingPrefix}${styleText('dim', prefix)}${styleText('green', S_CHECKBOX_SELECTED)} ${label}${ |
73 | | - option.hint ? ` ${styleText('dim', `(${option.hint})`)}` : '' |
74 | | - }`; |
| 104 | + return wrapTextWithPrefix( |
| 105 | + opts.output, |
| 106 | + `${label}${option.hint ? ` ${styleText('dim', `(${option.hint})`)}` : ''}`, |
| 107 | + `${spacingPrefix}${styleText('dim', prefix)} `, |
| 108 | + `${spacingPrefix}${styleText('dim', prefix)}${styleText('green', S_CHECKBOX_SELECTED)} `, |
| 109 | + `${spacingPrefix}${styleText('dim', prefixEnd)} ` |
| 110 | + ); |
75 | 111 | } |
76 | 112 | if (state === 'submitted') { |
77 | 113 | return `${styleText('dim', label)}`; |
78 | 114 | } |
79 | 115 | const unselectedCheckbox = |
80 | 116 | isItem || selectableGroups ? styleText('dim', S_CHECKBOX_INACTIVE) : ''; |
81 | | - return `${spacingPrefix}${styleText('dim', prefix)}${unselectedCheckbox} ${styleText('dim', label)}`; |
| 117 | + return wrapTextWithPrefix( |
| 118 | + opts.output, |
| 119 | + label, |
| 120 | + `${spacingPrefix}${styleText('dim', prefix)} `, |
| 121 | + `${spacingPrefix}${styleText('dim', prefix)}${unselectedCheckbox} `, |
| 122 | + `${spacingPrefix}${styleText('dim', prefixEnd)} `, |
| 123 | + (str) => styleText('dim', str) |
| 124 | + ); |
82 | 125 | }; |
83 | 126 | const required = opts.required ?? true; |
84 | 127 |
|
|
0 commit comments