-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathgroup-multi-select.ts
More file actions
188 lines (184 loc) · 6.45 KB
/
group-multi-select.ts
File metadata and controls
188 lines (184 loc) · 6.45 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { GroupMultiSelectPrompt } from '@clack/core';
import color from 'picocolors';
import {
type CommonOptions,
S_BAR,
S_BAR_END,
S_CHECKBOX_ACTIVE,
S_CHECKBOX_INACTIVE,
S_CHECKBOX_SELECTED,
symbol,
} from './common.js';
import type { Option } from './select.js';
export interface GroupMultiSelectOptions<Value> extends CommonOptions {
message: string;
options: Record<string, Option<Value>[]>;
initialValues?: Value[];
required?: boolean;
cursorAt?: Value;
selectableGroups?: boolean;
groupSpacing?: number;
}
export const groupMultiselect = <Value>(opts: GroupMultiSelectOptions<Value>) => {
const { selectableGroups = true, groupSpacing = 0 } = opts;
const opt = (
option: Option<Value>,
state:
| 'inactive'
| 'active'
| 'selected'
| 'active-selected'
| 'group-active'
| 'group-active-selected'
| 'submitted'
| 'cancelled',
options: Option<Value>[] = []
) => {
const label = option.label ?? String(option.value);
const isItem = typeof (option as any).group === 'string';
const next = isItem && (options[options.indexOf(option) + 1] ?? { group: true });
const isLast = isItem && (next as any).group === true;
const prefix = isItem ? (selectableGroups ? `${isLast ? S_BAR_END : S_BAR} ` : ' ') : '';
let spacingPrefix = '';
if (groupSpacing > 0 && !isItem) {
const spacingPrefixText = `\n${color.cyan(S_BAR)}`;
spacingPrefix = `${spacingPrefixText.repeat(groupSpacing - 1)}${spacingPrefixText} `;
}
if (state === 'active') {
return `${spacingPrefix}${color.dim(prefix)}${color.cyan(S_CHECKBOX_ACTIVE)} ${label}${
option.hint ? ` ${color.dim(`(${option.hint})`)}` : ''
}`;
}
if (state === 'group-active') {
return `${spacingPrefix}${prefix}${color.cyan(S_CHECKBOX_ACTIVE)} ${color.dim(label)}`;
}
if (state === 'group-active-selected') {
return `${spacingPrefix}${prefix}${color.green(S_CHECKBOX_SELECTED)} ${color.dim(label)}`;
}
if (state === 'selected') {
const selectedCheckbox = isItem || selectableGroups ? color.green(S_CHECKBOX_SELECTED) : '';
return `${spacingPrefix}${color.dim(prefix)}${selectedCheckbox} ${color.dim(label)}${
option.hint ? ` ${color.dim(`(${option.hint})`)}` : ''
}`;
}
if (state === 'cancelled') {
return `${color.strikethrough(color.dim(label))}`;
}
if (state === 'active-selected') {
return `${spacingPrefix}${color.dim(prefix)}${color.green(S_CHECKBOX_SELECTED)} ${label}${
option.hint ? ` ${color.dim(`(${option.hint})`)}` : ''
}`;
}
if (state === 'submitted') {
return `${color.dim(label)}`;
}
const unselectedCheckbox = isItem || selectableGroups ? color.dim(S_CHECKBOX_INACTIVE) : '';
return `${spacingPrefix}${color.dim(prefix)}${unselectedCheckbox} ${color.dim(label)}`;
};
const required = opts.required ?? true;
return new GroupMultiSelectPrompt({
options: opts.options,
signal: opts.signal,
input: opts.input,
output: opts.output,
initialValues: opts.initialValues,
required,
cursorAt: opts.cursorAt,
selectableGroups,
validate(selected: Value[] | undefined) {
if (required && (selected === undefined || selected.length === 0))
return `Please select at least one option.\n${color.reset(
color.dim(
`Press ${color.gray(color.bgWhite(color.inverse(' space ')))} to select, ${color.gray(
color.bgWhite(color.inverse(' enter '))
)} to submit`
)
)}`;
},
render() {
const title = `${color.gray(S_BAR)}\n${symbol(this.state)} ${opts.message}\n`;
const value = this.value ?? [];
switch (this.state) {
case 'submit': {
const selectedOptions = this.options
.filter(({ value: optionValue }) => value.includes(optionValue))
.map((option) => opt(option, 'submitted'));
const optionsText =
selectedOptions.length === 0 ? '' : ` ${selectedOptions.join(color.dim(', '))}`;
return `${title}${color.gray(S_BAR)}${optionsText}`;
}
case 'cancel': {
const label = this.options
.filter(({ value: optionValue }) => value.includes(optionValue))
.map((option) => opt(option, 'cancelled'))
.join(color.dim(', '));
return `${title}${color.gray(S_BAR)} ${
label.trim() ? `${label}\n${color.gray(S_BAR)}` : ''
}`;
}
case 'error': {
const footer = this.error
.split('\n')
.map((ln, i) =>
i === 0 ? `${color.yellow(S_BAR_END)} ${color.yellow(ln)}` : ` ${ln}`
)
.join('\n');
return `${title}${color.yellow(S_BAR)} ${this.options
.map((option, i, options) => {
const selected =
value.includes(option.value) ||
(option.group === true && this.isGroupSelected(`${option.value}`));
const active = i === this.cursor;
const groupActive =
!active &&
typeof option.group === 'string' &&
this.options[this.cursor].value === option.group;
if (groupActive) {
return opt(option, selected ? 'group-active-selected' : 'group-active', options);
}
if (active && selected) {
return opt(option, 'active-selected', options);
}
if (selected) {
return opt(option, 'selected', options);
}
return opt(option, active ? 'active' : 'inactive', options);
})
.join(`\n${color.yellow(S_BAR)} `)}\n${footer}\n`;
}
default: {
const optionsText = this.options
.map((option, i, options) => {
const selected =
value.includes(option.value) ||
(option.group === true && this.isGroupSelected(`${option.value}`));
const active = i === this.cursor;
const groupActive =
!active &&
typeof option.group === 'string' &&
this.options[this.cursor].value === option.group;
let optionText = '';
if (groupActive) {
optionText = opt(
option,
selected ? 'group-active-selected' : 'group-active',
options
);
} else if (active && selected) {
optionText = opt(option, 'active-selected', options);
} else if (selected) {
optionText = opt(option, 'selected', options);
} else {
optionText = opt(option, active ? 'active' : 'inactive', options);
}
const prefix = i !== 0 && !optionText.startsWith('\n') ? ' ' : '';
return `${prefix}${optionText}`;
})
.join(`\n${color.cyan(S_BAR)}`);
const optionsPrefix = optionsText.startsWith('\n') ? '' : ' ';
return `${title}${color.cyan(S_BAR)}${optionsPrefix}${optionsText}\n${color.cyan(S_BAR_END)}\n`;
}
}
},
}).prompt() as Promise<Value[] | symbol>;
};