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.

autocompleteMultiselect.js 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. 'use strict';
  2. const color = require('kleur');
  3. const _require = require('sisteransi'),
  4. cursor = _require.cursor;
  5. const MultiselectPrompt = require('./multiselect');
  6. const _require2 = require('../util'),
  7. clear = _require2.clear,
  8. style = _require2.style,
  9. figures = _require2.figures;
  10. /**
  11. * MultiselectPrompt Base Element
  12. * @param {Object} opts Options
  13. * @param {String} opts.message Message
  14. * @param {Array} opts.choices Array of choice objects
  15. * @param {String} [opts.hint] Hint to display
  16. * @param {String} [opts.warn] Hint shown for disabled choices
  17. * @param {Number} [opts.max] Max choices
  18. * @param {Number} [opts.cursor=0] Cursor start position
  19. * @param {Stream} [opts.stdin] The Readable stream to listen to
  20. * @param {Stream} [opts.stdout] The Writable stream to write readline data to
  21. */
  22. class AutocompleteMultiselectPrompt extends MultiselectPrompt {
  23. constructor(opts = {}) {
  24. opts.overrideRender = true;
  25. super(opts);
  26. this.inputValue = '';
  27. this.clear = clear('', this.out.columns);
  28. this.filteredOptions = this.value;
  29. this.render();
  30. }
  31. last() {
  32. this.cursor = this.filteredOptions.length - 1;
  33. this.render();
  34. }
  35. next() {
  36. this.cursor = (this.cursor + 1) % this.filteredOptions.length;
  37. this.render();
  38. }
  39. up() {
  40. if (this.cursor === 0) {
  41. this.cursor = this.filteredOptions.length - 1;
  42. } else {
  43. this.cursor--;
  44. }
  45. this.render();
  46. }
  47. down() {
  48. if (this.cursor === this.filteredOptions.length - 1) {
  49. this.cursor = 0;
  50. } else {
  51. this.cursor++;
  52. }
  53. this.render();
  54. }
  55. left() {
  56. this.filteredOptions[this.cursor].selected = false;
  57. this.render();
  58. }
  59. right() {
  60. if (this.value.filter(e => e.selected).length >= this.maxChoices) return this.bell();
  61. this.filteredOptions[this.cursor].selected = true;
  62. this.render();
  63. }
  64. delete() {
  65. if (this.inputValue.length) {
  66. this.inputValue = this.inputValue.substr(0, this.inputValue.length - 1);
  67. this.updateFilteredOptions();
  68. }
  69. }
  70. updateFilteredOptions() {
  71. const currentHighlight = this.filteredOptions[this.cursor];
  72. this.filteredOptions = this.value.filter(v => {
  73. if (this.inputValue) {
  74. if (typeof v.title === 'string') {
  75. if (v.title.toLowerCase().includes(this.inputValue.toLowerCase())) {
  76. return true;
  77. }
  78. }
  79. if (typeof v.value === 'string') {
  80. if (v.value.toLowerCase().includes(this.inputValue.toLowerCase())) {
  81. return true;
  82. }
  83. }
  84. return false;
  85. }
  86. return true;
  87. });
  88. const newHighlightIndex = this.filteredOptions.findIndex(v => v === currentHighlight);
  89. this.cursor = newHighlightIndex < 0 ? 0 : newHighlightIndex;
  90. this.render();
  91. }
  92. handleSpaceToggle() {
  93. const v = this.filteredOptions[this.cursor];
  94. if (v.selected) {
  95. v.selected = false;
  96. this.render();
  97. } else if (v.disabled || this.value.filter(e => e.selected).length >= this.maxChoices) {
  98. return this.bell();
  99. } else {
  100. v.selected = true;
  101. this.render();
  102. }
  103. }
  104. handleInputChange(c) {
  105. this.inputValue = this.inputValue + c;
  106. this.updateFilteredOptions();
  107. }
  108. _(c, key) {
  109. if (c === ' ') {
  110. this.handleSpaceToggle();
  111. } else {
  112. this.handleInputChange(c);
  113. }
  114. }
  115. renderInstructions() {
  116. if (this.instructions === undefined || this.instructions) {
  117. if (typeof this.instructions === 'string') {
  118. return this.instructions;
  119. }
  120. return `
  121. Instructions:
  122. ${figures.arrowUp}/${figures.arrowDown}: Highlight option
  123. ${figures.arrowLeft}/${figures.arrowRight}/[space]: Toggle selection
  124. [a,b,c]/delete: Filter choices
  125. enter/return: Complete answer
  126. `;
  127. }
  128. return '';
  129. }
  130. renderCurrentInput() {
  131. return `
  132. Filtered results for: ${this.inputValue ? this.inputValue : color.gray('Enter something to filter')}\n`;
  133. }
  134. renderOption(cursor, v, i) {
  135. let title;
  136. if (v.disabled) title = cursor === i ? color.gray().underline(v.title) : color.strikethrough().gray(v.title);else title = cursor === i ? color.cyan().underline(v.title) : v.title;
  137. return (v.selected ? color.green(figures.radioOn) : figures.radioOff) + ' ' + title;
  138. }
  139. renderDoneOrInstructions() {
  140. if (this.done) {
  141. return this.value.filter(e => e.selected).map(v => v.title).join(', ');
  142. }
  143. const output = [color.gray(this.hint), this.renderInstructions(), this.renderCurrentInput()];
  144. if (this.filteredOptions.length && this.filteredOptions[this.cursor].disabled) {
  145. output.push(color.yellow(this.warn));
  146. }
  147. return output.join(' ');
  148. }
  149. render() {
  150. if (this.closed) return;
  151. if (this.firstRender) this.out.write(cursor.hide);
  152. super.render(); // print prompt
  153. let prompt = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(false), this.renderDoneOrInstructions()].join(' ');
  154. if (this.showMinError) {
  155. prompt += color.red(`You must select a minimum of ${this.minSelected} choices.`);
  156. this.showMinError = false;
  157. }
  158. prompt += this.renderOptions(this.filteredOptions);
  159. this.out.write(this.clear + prompt);
  160. this.clear = clear(prompt, this.out.columns);
  161. }
  162. }
  163. module.exports = AutocompleteMultiselectPrompt;