/
checkbox.ts
234 lines (210 loc) 路 6.04 KB
/
checkbox.ts
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import type { KeyCode } from "../keycode/mod.ts";
import { brightBlue, dim, green, red, yellow } from "./deps.ts";
import { Figures } from "./figures.ts";
import {
GenericList,
GenericListKeys,
GenericListOption,
GenericListOptions,
GenericListOptionSettings,
GenericListSettings,
} from "./_generic_list.ts";
import { GenericPrompt } from "./_generic_prompt.ts";
/** Checkbox key options. */
export interface CheckboxKeys extends GenericListKeys {
check?: string[];
}
/** Checkbox option options. */
export interface CheckboxOption extends GenericListOption {
checked?: boolean;
icon?: boolean;
}
/** Checkbox option settings. */
export interface CheckboxOptionSettings extends GenericListOptionSettings {
checked: boolean;
icon: boolean;
}
/** Checkbox options type. */
export type CheckboxValueOptions = (string | CheckboxOption)[];
/** Checkbox option settings type. */
export type CheckboxValueSettings = CheckboxOptionSettings[];
/** Checkbox prompt options. */
export interface CheckboxOptions
extends GenericListOptions<string[], string[]> {
options: CheckboxValueOptions;
check?: string;
uncheck?: string;
minOptions?: number;
maxOptions?: number;
keys?: CheckboxKeys;
}
/** Checkbox prompt settings. */
interface CheckboxSettings extends GenericListSettings<string[], string[]> {
options: CheckboxValueSettings;
check: string;
uncheck: string;
minOptions: number;
maxOptions: number;
keys?: CheckboxKeys;
}
/** Checkbox prompt representation. */
export class Checkbox
extends GenericList<string[], string[], CheckboxSettings> {
/**
* Inject prompt value. Can be used for unit tests or pre selections.
* @param value Array of input values.
*/
public static inject(value: string[]): void {
GenericPrompt.inject(value);
}
/** Execute the prompt and show cursor on end. */
public static prompt(options: CheckboxOptions): Promise<string[]> {
return new this({
pointer: brightBlue(Figures.POINTER_SMALL),
prefix: yellow("? "),
indent: " ",
listPointer: brightBlue(Figures.POINTER),
maxRows: 10,
searchLabel: brightBlue(Figures.SEARCH),
minOptions: 0,
maxOptions: Infinity,
check: green(Figures.TICK),
uncheck: red(Figures.CROSS),
...options,
keys: {
check: ["space"],
...(options.keys ?? {}),
},
options: Checkbox.mapOptions(options),
}).prompt();
}
/**
* Create list separator.
* @param label Separator label.
*/
public static separator(label?: string): CheckboxOption {
return {
...super.separator(label),
icon: false,
};
}
/**
* Map string option values to options and set option defaults.
* @param options Checkbox options.
*/
protected static mapOptions(options: CheckboxOptions): CheckboxValueSettings {
return options.options
.map((item: string | CheckboxOption) =>
typeof item === "string" ? { value: item } : item
)
.map((item) => ({
...this.mapOption(item),
checked: typeof item.checked === "undefined" && options.default &&
options.default.indexOf(item.value) !== -1
? true
: !!item.checked,
icon: typeof item.icon === "undefined" ? true : item.icon,
}));
}
/**
* Render checkbox option.
* @param item Checkbox option settings.
* @param isSelected Set to true if option is selected.
*/
protected getListItem(
item: CheckboxOptionSettings,
isSelected?: boolean,
): string {
let line = this.settings.indent;
// pointer
line += isSelected ? this.settings.listPointer + " " : " ";
// icon
if (item.icon) {
let check = item.checked
? this.settings.check + " "
: this.settings.uncheck + " ";
if (item.disabled) {
check = dim(check);
}
line += check;
} else {
line += " ";
}
// value
line += `${
isSelected && !item.disabled
? this.highlight(item.name, (val) => val)
: this.highlight(item.name)
}`;
return line;
}
/** Get value of checked options. */
protected getValue(): string[] {
return this.settings.options
.filter((item) => item.checked)
.map((item) => item.value);
}
/**
* Handle user input event.
* @param event Key event.
*/
protected async handleEvent(event: KeyCode): Promise<void> {
switch (true) {
case this.isKey(this.settings.keys, "check", event):
this.checkValue();
break;
default:
await super.handleEvent(event);
}
}
/** Check selected option. */
protected checkValue(): void {
const item = this.options[this.listIndex];
if (item.disabled) {
this.setErrorMessage("This option is disabled and cannot be changed.");
} else {
item.checked = !item.checked;
}
}
/**
* Validate input value.
* @param value User input value.
* @return True on success, false or error message on error.
*/
protected validate(value: string[]): boolean | string {
const isValidValue = Array.isArray(value) &&
value.every((val) =>
typeof val === "string" &&
val.length > 0 &&
this.settings.options.findIndex((option: CheckboxOptionSettings) =>
option.value === val
) !== -1
);
if (!isValidValue) {
return false;
}
if (value.length < this.settings.minOptions) {
return `The minimum number of options is ${this.settings.minOptions} but got ${value.length}.`;
}
if (value.length > this.settings.maxOptions) {
return `The maximum number of options is ${this.settings.maxOptions} but got ${value.length}.`;
}
return true;
}
/**
* Map input value to output value.
* @param value Input value.
* @return Output value.
*/
protected transform(value: string[]): string[] {
return value.map((val) => val.trim());
}
/**
* Format output value.
* @param value Output value.
*/
protected format(value: string[]): string {
return value.map((val) => this.getOptionByValue(val)?.name ?? val)
.join(", ");
}
}