-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathinquirer-table-prompt.ts
More file actions
233 lines (205 loc) · 6.77 KB
/
Copy pathinquirer-table-prompt.ts
File metadata and controls
233 lines (205 loc) · 6.77 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
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
/**
* Table prompt for inquirer v12.
* Standalone implementation (no inquirer/lib) compatible with
* inquirer 12 legacy adapter: constructor(question, rl, answers) + run() returns Promise.
*/
import * as readline from 'readline';
import { getChalk } from './chalk';
import figures from 'figures';
import cliCursor from 'cli-cursor';
import Table from 'cli-table';
interface ChoiceLike {
name?: string;
value?: string;
}
interface TableQuestion {
message?: string;
name?: string;
columns?: ChoiceLike[];
rows?: ChoiceLike[];
selectAll?: boolean;
pageSize?: number;
}
type ReadLine = readline.Interface & { input: NodeJS.ReadableStream; output: NodeJS.WritableStream };
function pluckName(c: ChoiceLike): string {
return c.name ?? String(c.value ?? '');
}
function getValue(c: ChoiceLike): string {
return c.value ?? c.name ?? '';
}
class TablePrompt {
private question: TableQuestion;
private rl: ReadLine;
private selectAll: boolean;
private columns: ChoiceLike[];
private rows: ChoiceLike[];
private pointer: number;
private horizontalPointer: number;
private values: (string | undefined)[];
private pageSize: number;
private spaceKeyPressed: boolean;
private status: 'idle' | 'answered';
private done: ((value: (string | undefined)[]) => void) | null;
private lastHeight: number;
constructor(question: TableQuestion, rl: ReadLine, _answers: Record<string, unknown>) {
this.question = question;
this.rl = rl;
this.selectAll = Boolean(question.selectAll);
this.columns = Array.isArray(question.columns) ? question.columns : [];
this.rows = this.selectAll
? [{ name: 'Select All', value: 'selectAll' }, ...(question.rows || [])]
: Array.isArray(question.rows) ? question.rows : [];
this.pointer = 0;
this.horizontalPointer = 0;
this.values = this.columns.map(() => undefined);
this.pageSize = Number(question.pageSize) || 5;
this.spaceKeyPressed = false;
this.status = 'idle';
this.done = null;
this.lastHeight = 0;
}
run(): Promise<(string | undefined)[]> {
return new Promise((resolve) => {
this.done = (value) => {
this.status = 'answered';
cliCursor.show();
resolve(value);
};
const onKeypress = (_str: string, key: { name: string; ctrl?: boolean }) => {
if (this.status === 'answered') return;
if (key.ctrl && key.name === 'c') return;
switch (key.name) {
case 'up':
this.onUpKey();
break;
case 'down':
this.onDownKey();
break;
case 'left':
this.onLeftKey();
break;
case 'right':
this.onRightKey();
break;
case 'space':
this.onSpaceKey();
break;
case 'enter':
case 'return':
this.onSubmit();
break;
default:
return;
}
this.render();
};
(this.rl.input as NodeJS.EventEmitter).on('keypress', onKeypress);
cliCursor.hide();
this.render();
});
}
private getCurrentValue(): (string | undefined)[] {
const out: (string | undefined)[] = [];
for (let i = 0; i < this.rows.length; i++) {
out.push(this.values[i]);
}
return out;
}
private onSubmit(): void {
if (!this.done) return;
const raw = this.getCurrentValue();
if (this.selectAll && raw.length > 0) {
this.done(raw.slice(1));
} else {
this.done(raw);
}
}
private onUpKey(): void {
this.pointer = this.pointer > 0 ? this.pointer - 1 : this.pointer;
}
private onDownKey(): void {
const len = this.rows.length;
this.pointer = this.pointer < len - 1 ? this.pointer + 1 : this.pointer;
}
private onLeftKey(): void {
const len = this.columns.length;
this.horizontalPointer = this.horizontalPointer > 0 ? this.horizontalPointer - 1 : len - 1;
}
private onRightKey(): void {
const len = this.columns.length;
this.horizontalPointer = this.horizontalPointer < len - 1 ? this.horizontalPointer + 1 : 0;
}
private selectAllValues(value: string): void {
this.values = this.rows.map(() => value);
}
private onSpaceKey(): void {
const col = this.columns[this.horizontalPointer];
const row = this.rows[this.pointer];
if (!col) return;
const value = getValue(col);
const rowValue = row ? getValue(row) : '';
if (rowValue === 'selectAll') {
this.selectAllValues(value);
} else {
this.values[this.pointer] = value;
}
this.spaceKeyPressed = true;
}
private paginate(): [number, number] {
const mid = Math.floor(this.pageSize / 2);
const len = this.rows.length;
let first = Math.max(0, this.pointer - mid);
let last = Math.min(first + this.pageSize - 1, len - 1);
const offset = this.pageSize - 1 - (last - first);
first = Math.max(0, first - offset);
return [first, last];
}
private getMessage(): string {
let msg = this.question.message || 'Select';
if (!this.spaceKeyPressed) {
msg +=
' (Press ' +
getChalk().cyan.bold('<space>') +
' to select, ' +
getChalk().cyan.bold('<Up/Down>') +
' rows, ' +
getChalk().cyan.bold('<Left/Right>') +
' columns, ' +
getChalk().cyan.bold('<Enter>') +
' to confirm)';
}
return msg;
}
private render(): void {
const [firstIndex, lastIndex] = this.paginate();
const table = new Table({
head: [getChalk().reset.dim(`${firstIndex + 1}-${lastIndex + 1} of ${this.rows.length}`)].concat(
this.columns.map((c) => getChalk().reset.bold(pluckName(c))),
),
});
for (let rowIndex = firstIndex; rowIndex <= lastIndex; rowIndex++) {
const row = this.rows[rowIndex];
if (!row) continue;
const columnValues: string[] = [];
for (let colIndex = 0; colIndex < this.columns.length; colIndex++) {
const isSelected =
this.status !== 'answered' && this.pointer === rowIndex && this.horizontalPointer === colIndex;
const cellValue =
getValue(this.columns[colIndex]) === this.values[rowIndex] ? figures.radioOn : figures.radioOff;
columnValues.push(`${isSelected ? '[' : ' '} ${cellValue} ${isSelected ? ']' : ' '}`);
}
const chalkModifier =
this.status !== 'answered' && this.pointer === rowIndex ? getChalk().reset.bold.cyan : getChalk().reset;
table.push({ [chalkModifier(pluckName(row))]: columnValues });
}
const message = this.getMessage() + '\n\n' + table.toString();
const lines = message.split('\n').length;
const out = this.rl.output as NodeJS.WritableStream;
if (this.lastHeight > 0) {
out.write('\u001b[' + this.lastHeight + 'A\u001b[0J');
}
out.write(message);
this.lastHeight = lines;
}
}
export = TablePrompt;