-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
list.js
203 lines (167 loc) · 5.04 KB
/
list.js
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
/**
* `list` type prompt
*/
import chalk from 'chalk';
import figures from '@inquirer/figures';
import cliCursor from 'cli-cursor';
import runAsync from 'run-async';
import { flatMap, map, take, takeUntil } from 'rxjs';
import observe from '../utils/events.js';
import Paginator from '../utils/paginator.js';
import incrementListIndex from '../utils/incrementListIndex.js';
import Base from './base.js';
export default class ListPrompt extends Base {
constructor(questions, rl, answers) {
super(questions, rl, answers);
if (!this.opt.choices) {
this.throwParamError('choices');
}
this.firstRender = true;
this.selected = 0;
const def = this.opt.default;
// If def is a Number, then use as index. Otherwise, check for value.
if (typeof def === 'number' && def >= 0 && def < this.opt.choices.realLength) {
this.selected = def;
} else if (typeof def !== 'number' && def != null) {
const index = this.opt.choices.realChoices.findIndex(({ value }) => value === def);
this.selected = Math.max(index, 0);
}
// Make sure no default is set (so it won't be printed)
this.opt.default = null;
const shouldLoop = this.opt.loop === undefined ? true : this.opt.loop;
this.paginator = new Paginator(this.screen, { isInfinite: shouldLoop });
}
/**
* Start the Inquiry session
* @param {Function} cb Callback when prompt is done
* @return {this}
*/
_run(cb) {
this.done = cb;
const events = observe(this.rl);
events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
events.normalizedDownKey
.pipe(takeUntil(events.line))
.forEach(this.onDownKey.bind(this));
events.numberKey.pipe(takeUntil(events.line)).forEach(this.onNumberKey.bind(this));
events.line
.pipe(
take(1),
map(this.getCurrentValue.bind(this)),
flatMap((value) =>
runAsync(this.opt.filter)(value, this.answers).catch((error) => error),
),
)
.forEach(this.onSubmit.bind(this));
// Init the prompt
cliCursor.hide();
this.render();
return this;
}
/**
* Render the prompt to screen
* @return {ListPrompt} self
*/
render() {
// Render question
let message = this.getQuestion();
if (this.firstRender) {
message += chalk.dim('(Use arrow keys)');
}
// Render choices or answer depending on the state
if (this.status === 'answered') {
message += chalk.cyan(this.opt.choices.getChoice(this.selected).short);
} else {
const choicesStr = listRender(this.opt.choices, this.selected);
const indexPosition = this.opt.choices.indexOf(
this.opt.choices.getChoice(this.selected),
);
const realIndexPosition =
this.opt.choices.reduce((acc, value, i) => {
// Dont count lines past the choice we are looking at
if (i > indexPosition) {
return acc;
}
// Add line if it's a separator
if (value.type === 'separator') {
return acc + 1;
}
let l = value.name;
// Non-strings take up one line
if (typeof l !== 'string') {
return acc + 1;
}
// Calculate lines taken up by string
l = l.split('\n');
return acc + l.length;
}, 0) - 1;
message +=
'\n' + this.paginator.paginate(choicesStr, realIndexPosition, this.opt.pageSize);
}
this.firstRender = false;
this.screen.render(message);
}
/**
* When user press `enter` key
*/
onSubmit(value) {
this.status = 'answered';
// Rerender prompt
this.render();
this.screen.done();
cliCursor.show();
this.done(value);
}
getCurrentValue() {
return this.opt.choices.getChoice(this.selected).value;
}
/**
* When user press a key
*/
onUpKey() {
this.selected = incrementListIndex(this.selected, 'up', this.opt);
this.render();
}
onDownKey() {
this.selected = incrementListIndex(this.selected, 'down', this.opt);
this.render();
}
onNumberKey(input) {
if (input <= this.opt.choices.realLength) {
this.selected = input - 1;
}
this.render();
}
}
/**
* Function for rendering list choices
* @param {Number} pointer Position of the pointer
* @return {String} Rendered content
*/
function listRender(choices, pointer) {
let output = '';
let separatorOffset = 0;
choices.forEach((choice, i) => {
if (choice.type === 'separator') {
separatorOffset++;
output += ' ' + choice + '\n';
return;
}
if (choice.disabled) {
separatorOffset++;
output += ' - ' + choice.name;
output += ` (${
typeof choice.disabled === 'string' ? choice.disabled : 'Disabled'
})`;
output += '\n';
return;
}
const isSelected = i - separatorOffset === pointer;
let line = (isSelected ? figures.pointer + ' ' : ' ') + choice.name;
if (isSelected) {
line = chalk.cyan(line);
}
output += line + ' \n';
});
return output.replaceAll(/\n$/g, '');
}