source: frontend/node_modules/prompts/lib/elements/select.js

Last change on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago

Fix frontend appearance

  • Property mode set to 100644
File size: 4.7 KB
RevLine 
[9af201e]1'use strict';
2
3const color = require('kleur');
4const Prompt = require('./prompt');
5const { style, clear, figures, wrap, entriesToDisplay } = require('../util');
6const { cursor } = require('sisteransi');
7
8/**
9 * SelectPrompt Base Element
10 * @param {Object} opts Options
11 * @param {String} opts.message Message
12 * @param {Array} opts.choices Array of choice objects
13 * @param {String} [opts.hint] Hint to display
14 * @param {Number} [opts.initial] Index of default value
15 * @param {Stream} [opts.stdin] The Readable stream to listen to
16 * @param {Stream} [opts.stdout] The Writable stream to write readline data to
17 * @param {Number} [opts.optionsPerPage=10] Max options to display at once
18 */
19class SelectPrompt extends Prompt {
20 constructor(opts={}) {
21 super(opts);
22 this.msg = opts.message;
23 this.hint = opts.hint || '- Use arrow-keys. Return to submit.';
24 this.warn = opts.warn || '- This option is disabled';
25 this.cursor = opts.initial || 0;
26 this.choices = opts.choices.map((ch, idx) => {
27 if (typeof ch === 'string')
28 ch = {title: ch, value: idx};
29 return {
30 title: ch && (ch.title || ch.value || ch),
31 value: ch && (ch.value === undefined ? idx : ch.value),
32 description: ch && ch.description,
33 selected: ch && ch.selected,
34 disabled: ch && ch.disabled
35 };
36 });
37 this.optionsPerPage = opts.optionsPerPage || 10;
38 this.value = (this.choices[this.cursor] || {}).value;
39 this.clear = clear('', this.out.columns);
40 this.render();
41 }
42
43 moveCursor(n) {
44 this.cursor = n;
45 this.value = this.choices[n].value;
46 this.fire();
47 }
48
49 reset() {
50 this.moveCursor(0);
51 this.fire();
52 this.render();
53 }
54
55 exit() {
56 this.abort();
57 }
58
59 abort() {
60 this.done = this.aborted = true;
61 this.fire();
62 this.render();
63 this.out.write('\n');
64 this.close();
65 }
66
67 submit() {
68 if (!this.selection.disabled) {
69 this.done = true;
70 this.aborted = false;
71 this.fire();
72 this.render();
73 this.out.write('\n');
74 this.close();
75 } else
76 this.bell();
77 }
78
79 first() {
80 this.moveCursor(0);
81 this.render();
82 }
83
84 last() {
85 this.moveCursor(this.choices.length - 1);
86 this.render();
87 }
88
89 up() {
90 if (this.cursor === 0) {
91 this.moveCursor(this.choices.length - 1);
92 } else {
93 this.moveCursor(this.cursor - 1);
94 }
95 this.render();
96 }
97
98 down() {
99 if (this.cursor === this.choices.length - 1) {
100 this.moveCursor(0);
101 } else {
102 this.moveCursor(this.cursor + 1);
103 }
104 this.render();
105 }
106
107 next() {
108 this.moveCursor((this.cursor + 1) % this.choices.length);
109 this.render();
110 }
111
112 _(c, key) {
113 if (c === ' ') return this.submit();
114 }
115
116 get selection() {
117 return this.choices[this.cursor];
118 }
119
120 render() {
121 if (this.closed) return;
122 if (this.firstRender) this.out.write(cursor.hide);
123 else this.out.write(clear(this.outputText, this.out.columns));
124 super.render();
125
126 let { startIndex, endIndex } = entriesToDisplay(this.cursor, this.choices.length, this.optionsPerPage);
127
128 // Print prompt
129 this.outputText = [
130 style.symbol(this.done, this.aborted),
131 color.bold(this.msg),
132 style.delimiter(false),
133 this.done ? this.selection.title : this.selection.disabled
134 ? color.yellow(this.warn) : color.gray(this.hint)
135 ].join(' ');
136
137 // Print choices
138 if (!this.done) {
139 this.outputText += '\n';
140 for (let i = startIndex; i < endIndex; i++) {
141 let title, prefix, desc = '', v = this.choices[i];
142
143 // Determine whether to display "more choices" indicators
144 if (i === startIndex && startIndex > 0) {
145 prefix = figures.arrowUp;
146 } else if (i === endIndex - 1 && endIndex < this.choices.length) {
147 prefix = figures.arrowDown;
148 } else {
149 prefix = ' ';
150 }
151
152 if (v.disabled) {
153 title = this.cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);
154 prefix = (this.cursor === i ? color.bold().gray(figures.pointer) + ' ' : ' ') + prefix;
155 } else {
156 title = this.cursor === i ? color.cyan().underline(v.title) : v.title;
157 prefix = (this.cursor === i ? color.cyan(figures.pointer) + ' ' : ' ') + prefix;
158 if (v.description && this.cursor === i) {
159 desc = ` - ${v.description}`;
160 if (prefix.length + title.length + desc.length >= this.out.columns
161 || v.description.split(/\r?\n/).length > 1) {
162 desc = '\n' + wrap(v.description, { margin: 3, width: this.out.columns });
163 }
164 }
165 }
166
167 this.outputText += `${prefix} ${title}${color.gray(desc)}\n`;
168 }
169 }
170
171 this.out.write(this.outputText);
172 }
173}
174
175module.exports = SelectPrompt;
Note: See TracBrowser for help on using the repository browser.