-
-
Notifications
You must be signed in to change notification settings - Fork 10.6k
/
Copy pathprompts-multi-select.ts
194 lines (172 loc) · 4.57 KB
/
prompts-multi-select.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
/**
* Adapted from https://github.com/withastro/cli-kit
* @license MIT License Copyright (c) 2022 Nate Moore
*/
import { cursor, erase } from "sisteransi";
import { Prompt, type PromptOptions } from "./prompts-prompt-base";
import { type SelectChoice } from "./prompts-select";
import { color, strip, clear, type ActionKey } from "./utils";
export interface MultiSelectPromptOptions<
Choices extends Readonly<Readonly<SelectChoice>[]>
> extends PromptOptions {
hint?: string;
message: string;
label: string;
initial?: Choices[number]["value"];
validate?: (v: any) => boolean;
error?: string;
choices: Choices;
}
export class MultiSelectPrompt<
Choices extends Readonly<Readonly<SelectChoice>[]>
> extends Prompt {
choices: Readonly<Array<Choices[number] & { selected: boolean }>>;
label: string;
msg: string;
hint?: string;
value: Array<Choices[number]["value"]>;
initialValue: Choices[number]["value"];
done: boolean | undefined;
cursor: number;
name = "MultiSelectPrompt" as const;
// set by render which is called in constructor
outputText!: string;
constructor(opts: MultiSelectPromptOptions<Choices>) {
if (
!opts.choices ||
!Array.isArray(opts.choices) ||
opts.choices.length < 1
) {
throw new Error("MultiSelectPrompt must contain choices");
}
super(opts);
this.label = opts.label;
this.msg = opts.message;
this.hint = opts.hint;
this.value = [];
this.choices =
opts.choices.map((choice) => ({ ...choice, selected: false })) || [];
this.initialValue = opts.initial || this.choices[0].value;
this.cursor = this.choices.findIndex((c) => c.value === this.initialValue);
this.render();
}
get type() {
return "multiselect" as const;
}
exit() {
this.abort();
}
abort() {
this.done = this.aborted = true;
this.cursor = this.choices.findIndex((c) => c.value === this.initialValue);
this.fire();
this.render();
this.out.write("\n");
this.close();
}
submit() {
return this.toggle();
}
finish() {
// eslint-disable-next-line no-self-assign
this.value = this.value;
this.done = true;
this.aborted = false;
this.fire();
this.render();
this.out.write("\n");
this.close();
}
moveCursor(n: number) {
this.cursor = n;
this.fire();
}
toggle() {
let choice = this.choices[this.cursor];
if (!choice) return;
choice.selected = !choice.selected;
this.render();
}
_(c: string, key: ActionKey) {
if (c === " ") {
return this.toggle();
}
if (c.toLowerCase() === "c") {
return this.finish();
}
return;
}
reset() {
this.moveCursor(0);
this.fire();
this.render();
}
first() {
this.moveCursor(0);
this.render();
}
last() {
this.moveCursor(this.choices.length - 1);
this.render();
}
up() {
if (this.cursor === 0) {
this.moveCursor(this.choices.length - 1);
} else {
this.moveCursor(this.cursor - 1);
}
this.render();
}
down() {
if (this.cursor === this.choices.length - 1) {
this.moveCursor(0);
} else {
this.moveCursor(this.cursor + 1);
}
this.render();
}
render() {
if (this.closed) return;
if (this.firstRender) {
this.out.write(cursor.hide);
} else {
this.out.write(clear(this.outputText, this.out.columns));
}
super.render();
let outputText = ["\n", this.label, " ", this.msg, "\n"];
let prefix = " ".repeat(strip(this.label).length);
if (this.done) {
outputText.push(
this.choices
.map((choice) =>
choice.selected ? `${prefix} ${color.dim(`${choice.label}`)}\n` : ""
)
.join("")
.trimEnd()
);
} else {
outputText.push(
this.choices
.map((choice, i) =>
i === this.cursor
? `${prefix.slice(0, -2)}${color.cyanBright("▶")} ${
choice.selected ? color.green("■") : color.whiteBright("□")
} ${color.underline(choice.label)} ${
choice.hint ? color.dim(choice.hint) : ""
}`
: color[choice.selected ? "reset" : "dim"](
`${prefix} ${choice.selected ? color.green("■") : "□"} ${
choice.label
} `
)
)
.join("\n")
);
outputText.push(
`\n\n${prefix} Press ${color.inverse(" C ")} to continue`
);
}
this.outputText = outputText.join("");
this.out.write(erase.line + cursor.to(0) + this.outputText);
}
}