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.

select.js 5.0KB

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