Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

multiselect.js 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. 'use strict';
  2. const color = require('kleur');
  3. const _require = require('sisteransi'),
  4. cursor = _require.cursor;
  5. const Prompt = require('./prompt');
  6. const _require2 = require('../util'),
  7. clear = _require2.clear,
  8. figures = _require2.figures,
  9. style = _require2.style,
  10. wrap = _require2.wrap,
  11. entriesToDisplay = _require2.entriesToDisplay;
  12. /**
  13. * MultiselectPrompt Base Element
  14. * @param {Object} opts Options
  15. * @param {String} opts.message Message
  16. * @param {Array} opts.choices Array of choice objects
  17. * @param {String} [opts.hint] Hint to display
  18. * @param {String} [opts.warn] Hint shown for disabled choices
  19. * @param {Number} [opts.max] Max choices
  20. * @param {Number} [opts.cursor=0] Cursor start position
  21. * @param {Number} [opts.optionsPerPage=10] Max options to display at once
  22. * @param {Stream} [opts.stdin] The Readable stream to listen to
  23. * @param {Stream} [opts.stdout] The Writable stream to write readline data to
  24. */
  25. class MultiselectPrompt extends Prompt {
  26. constructor(opts = {}) {
  27. super(opts);
  28. this.msg = opts.message;
  29. this.cursor = opts.cursor || 0;
  30. this.scrollIndex = opts.cursor || 0;
  31. this.hint = opts.hint || '';
  32. this.warn = opts.warn || '- This option is disabled -';
  33. this.minSelected = opts.min;
  34. this.showMinError = false;
  35. this.maxChoices = opts.max;
  36. this.instructions = opts.instructions;
  37. this.optionsPerPage = opts.optionsPerPage || 10;
  38. this.value = opts.choices.map((ch, idx) => {
  39. if (typeof ch === 'string') ch = {
  40. title: ch,
  41. value: idx
  42. };
  43. return {
  44. title: ch && (ch.title || ch.value || ch),
  45. description: ch && ch.description,
  46. value: ch && (ch.value === undefined ? idx : ch.value),
  47. selected: ch && ch.selected,
  48. disabled: ch && ch.disabled
  49. };
  50. });
  51. this.clear = clear('', this.out.columns);
  52. if (!opts.overrideRender) {
  53. this.render();
  54. }
  55. }
  56. reset() {
  57. this.value.map(v => !v.selected);
  58. this.cursor = 0;
  59. this.fire();
  60. this.render();
  61. }
  62. selected() {
  63. return this.value.filter(v => v.selected);
  64. }
  65. exit() {
  66. this.abort();
  67. }
  68. abort() {
  69. this.done = this.aborted = true;
  70. this.fire();
  71. this.render();
  72. this.out.write('\n');
  73. this.close();
  74. }
  75. submit() {
  76. const selected = this.value.filter(e => e.selected);
  77. if (this.minSelected && selected.length < this.minSelected) {
  78. this.showMinError = true;
  79. this.render();
  80. } else {
  81. this.done = true;
  82. this.aborted = false;
  83. this.fire();
  84. this.render();
  85. this.out.write('\n');
  86. this.close();
  87. }
  88. }
  89. first() {
  90. this.cursor = 0;
  91. this.render();
  92. }
  93. last() {
  94. this.cursor = this.value.length - 1;
  95. this.render();
  96. }
  97. next() {
  98. this.cursor = (this.cursor + 1) % this.value.length;
  99. this.render();
  100. }
  101. up() {
  102. if (this.cursor === 0) {
  103. this.cursor = this.value.length - 1;
  104. } else {
  105. this.cursor--;
  106. }
  107. this.render();
  108. }
  109. down() {
  110. if (this.cursor === this.value.length - 1) {
  111. this.cursor = 0;
  112. } else {
  113. this.cursor++;
  114. }
  115. this.render();
  116. }
  117. left() {
  118. this.value[this.cursor].selected = false;
  119. this.render();
  120. }
  121. right() {
  122. if (this.value.filter(e => e.selected).length >= this.maxChoices) return this.bell();
  123. this.value[this.cursor].selected = true;
  124. this.render();
  125. }
  126. handleSpaceToggle() {
  127. const v = this.value[this.cursor];
  128. if (v.selected) {
  129. v.selected = false;
  130. this.render();
  131. } else if (v.disabled || this.value.filter(e => e.selected).length >= this.maxChoices) {
  132. return this.bell();
  133. } else {
  134. v.selected = true;
  135. this.render();
  136. }
  137. }
  138. toggleAll() {
  139. if (this.maxChoices !== undefined || this.value[this.cursor].disabled) {
  140. return this.bell();
  141. }
  142. const newSelected = !this.value[this.cursor].selected;
  143. this.value.filter(v => !v.disabled).forEach(v => v.selected = newSelected);
  144. this.render();
  145. }
  146. _(c, key) {
  147. if (c === ' ') {
  148. this.handleSpaceToggle();
  149. } else if (c === 'a') {
  150. this.toggleAll();
  151. } else {
  152. return this.bell();
  153. }
  154. }
  155. renderInstructions() {
  156. if (this.instructions === undefined || this.instructions) {
  157. if (typeof this.instructions === 'string') {
  158. return this.instructions;
  159. }
  160. return '\nInstructions:\n' + ` ${figures.arrowUp}/${figures.arrowDown}: Highlight option\n` + ` ${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection\n` + (this.maxChoices === undefined ? ` a: Toggle all\n` : '') + ` enter/return: Complete answer`;
  161. }
  162. return '';
  163. }
  164. renderOption(cursor, v, i, arrowIndicator) {
  165. const prefix = (v.selected ? color.green(figures.radioOn) : figures.radioOff) + ' ' + arrowIndicator + ' ';
  166. let title, desc;
  167. if (v.disabled) {
  168. title = cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);
  169. } else {
  170. title = cursor === i ? color.cyan().underline(v.title) : v.title;
  171. if (cursor === i && v.description) {
  172. desc = ` - ${v.description}`;
  173. if (prefix.length + title.length + desc.length >= this.out.columns || v.description.split(/\r?\n/).length > 1) {
  174. desc = '\n' + wrap(v.description, {
  175. margin: prefix.length,
  176. width: this.out.columns
  177. });
  178. }
  179. }
  180. }
  181. return prefix + title + color.gray(desc || '');
  182. } // shared with autocompleteMultiselect
  183. paginateOptions(options) {
  184. if (options.length === 0) {
  185. return color.red('No matches for this query.');
  186. }
  187. let _entriesToDisplay = entriesToDisplay(this.cursor, options.length, this.optionsPerPage),
  188. startIndex = _entriesToDisplay.startIndex,
  189. endIndex = _entriesToDisplay.endIndex;
  190. let prefix,
  191. styledOptions = [];
  192. for (let i = startIndex; i < endIndex; i++) {
  193. if (i === startIndex && startIndex > 0) {
  194. prefix = figures.arrowUp;
  195. } else if (i === endIndex - 1 && endIndex < options.length) {
  196. prefix = figures.arrowDown;
  197. } else {
  198. prefix = ' ';
  199. }
  200. styledOptions.push(this.renderOption(this.cursor, options[i], i, prefix));
  201. }
  202. return '\n' + styledOptions.join('\n');
  203. } // shared with autocomleteMultiselect
  204. renderOptions(options) {
  205. if (!this.done) {
  206. return this.paginateOptions(options);
  207. }
  208. return '';
  209. }
  210. renderDoneOrInstructions() {
  211. if (this.done) {
  212. return this.value.filter(e => e.selected).map(v => v.title).join(', ');
  213. }
  214. const output = [color.gray(this.hint), this.renderInstructions()];
  215. if (this.value[this.cursor].disabled) {
  216. output.push(color.yellow(this.warn));
  217. }
  218. return output.join(' ');
  219. }
  220. render() {
  221. if (this.closed) return;
  222. if (this.firstRender) this.out.write(cursor.hide);
  223. super.render(); // print prompt
  224. let prompt = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(false), this.renderDoneOrInstructions()].join(' ');
  225. if (this.showMinError) {
  226. prompt += color.red(`You must select a minimum of ${this.minSelected} choices.`);
  227. this.showMinError = false;
  228. }
  229. prompt += this.renderOptions(this.value);
  230. this.out.write(this.clear + prompt);
  231. this.clear = clear(prompt, this.out.columns);
  232. }
  233. }
  234. module.exports = MultiselectPrompt;