Actualizacion maquina principal
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / inquirer / lib / prompts / rawlist.js
1 'use strict';
2 /**
3  * `rawlist` type prompt
4  */
5
6 var _ = {
7   extend: require('lodash/extend'),
8   isNumber: require('lodash/isNumber'),
9   findIndex: require('lodash/findIndex'),
10 };
11 var chalk = require('chalk');
12 var { map, takeUntil } = require('rxjs/operators');
13 var Base = require('./base');
14 var Separator = require('../objects/separator');
15 var observe = require('../utils/events');
16 var Paginator = require('../utils/paginator');
17 var incrementListIndex = require('../utils/incrementListIndex');
18
19 class RawListPrompt extends Base {
20   constructor(questions, rl, answers) {
21     super(questions, rl, answers);
22
23     if (!this.opt.choices) {
24       this.throwParamError('choices');
25     }
26
27     this.opt.validChoices = this.opt.choices.filter(Separator.exclude);
28
29     this.selected = 0;
30     this.rawDefault = 0;
31
32     _.extend(this.opt, {
33       validate: function (val) {
34         return val != null;
35       },
36     });
37
38     var def = this.opt.default;
39     if (_.isNumber(def) && def >= 0 && def < this.opt.choices.realLength) {
40       this.selected = def;
41       this.rawDefault = def;
42     } else if (!_.isNumber(def) && def != null) {
43       let index = _.findIndex(this.opt.choices.realChoices, ({ value }) => value === def);
44       let safeIndex = Math.max(index, 0);
45       this.selected = safeIndex;
46       this.rawDefault = safeIndex;
47     }
48
49     // Make sure no default is set (so it won't be printed)
50     this.opt.default = null;
51
52     this.paginator = new Paginator();
53   }
54
55   /**
56    * Start the Inquiry session
57    * @param  {Function} cb      Callback when prompt is done
58    * @return {this}
59    */
60
61   _run(cb) {
62     this.done = cb;
63
64     // Once user confirm (enter key)
65     var events = observe(this.rl);
66     var submit = events.line.pipe(map(this.getCurrentValue.bind(this)));
67
68     var validation = this.handleSubmitEvents(submit);
69     validation.success.forEach(this.onEnd.bind(this));
70     validation.error.forEach(this.onError.bind(this));
71
72     events.normalizedUpKey.pipe(takeUntil(events.line)).forEach(this.onUpKey.bind(this));
73     events.normalizedDownKey
74       .pipe(takeUntil(events.line))
75       .forEach(this.onDownKey.bind(this));
76     events.keypress
77       .pipe(takeUntil(validation.success))
78       .forEach(this.onKeypress.bind(this));
79     // Init the prompt
80     this.render();
81
82     return this;
83   }
84
85   /**
86    * Render the prompt to screen
87    * @return {RawListPrompt} self
88    */
89
90   render(error) {
91     // Render question
92     var message = this.getQuestion();
93     var bottomContent = '';
94
95     if (this.status === 'answered') {
96       message += chalk.cyan(this.answer);
97     } else {
98       var choicesStr = renderChoices(this.opt.choices, this.selected);
99       message +=
100         '\n' + this.paginator.paginate(choicesStr, this.selected, this.opt.pageSize);
101       message += '\n  Answer: ';
102     }
103     message += this.rl.line;
104
105     if (error) {
106       bottomContent = '\n' + chalk.red('>> ') + error;
107     }
108
109     this.screen.render(message, bottomContent);
110   }
111
112   /**
113    * When user press `enter` key
114    */
115
116   getCurrentValue(index) {
117     if (index == null) {
118       index = this.rawDefault;
119     } else if (index === '') {
120       index = this.selected;
121     } else {
122       index -= 1;
123     }
124
125     var choice = this.opt.choices.getChoice(index);
126     return choice ? choice.value : null;
127   }
128
129   onEnd(state) {
130     this.status = 'answered';
131     this.answer = state.value;
132
133     // Re-render prompt
134     this.render();
135
136     this.screen.done();
137     this.done(state.value);
138   }
139
140   onError() {
141     this.render('Please enter a valid index');
142   }
143
144   /**
145    * When user press a key
146    */
147
148   onKeypress() {
149     var index = this.rl.line.length ? Number(this.rl.line) - 1 : 0;
150
151     if (this.opt.choices.getChoice(index)) {
152       this.selected = index;
153     } else {
154       this.selected = undefined;
155     }
156     this.render();
157   }
158
159   /**
160    * When user press up key
161    */
162
163   onUpKey() {
164     this.onArrowKey('up');
165   }
166
167   /**
168    * When user press down key
169    */
170
171   onDownKey() {
172     this.onArrowKey('down');
173   }
174
175   /**
176    * When user press up or down key
177    * @param {String} type Arrow type: up or down
178    */
179
180   onArrowKey(type) {
181     this.selected = incrementListIndex(this.selected, type, this.opt);
182     this.rl.line = String(this.selected + 1);
183   }
184 }
185
186 /**
187  * Function for rendering list choices
188  * @param  {Number} pointer Position of the pointer
189  * @return {String}         Rendered content
190  */
191
192 function renderChoices(choices, pointer) {
193   var output = '';
194   var separatorOffset = 0;
195
196   choices.forEach(function (choice, i) {
197     output += '\n  ';
198
199     if (choice.type === 'separator') {
200       separatorOffset++;
201       output += ' ' + choice;
202       return;
203     }
204
205     var index = i - separatorOffset;
206     var display = index + 1 + ') ' + choice.name;
207     if (index === pointer) {
208       display = chalk.cyan(display);
209     }
210
211     output += display;
212   });
213
214   return output;
215 }
216
217 module.exports = RawListPrompt;