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.

string.js 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. 'use strict';
  2. const Prompt = require('../prompt');
  3. const placeholder = require('../placeholder');
  4. const { isPrimitive } = require('../utils');
  5. class StringPrompt extends Prompt {
  6. constructor(options) {
  7. super(options);
  8. this.initial = isPrimitive(this.initial) ? String(this.initial) : '';
  9. if (this.initial) this.cursorHide();
  10. this.state.prevCursor = 0;
  11. this.state.clipboard = [];
  12. }
  13. async keypress(input, key = {}) {
  14. let prev = this.state.prevKeypress;
  15. this.state.prevKeypress = key;
  16. if (this.options.multiline === true && key.name === 'return') {
  17. if (!prev || prev.name !== 'return') {
  18. return this.append('\n', key);
  19. }
  20. }
  21. return super.keypress(input, key);
  22. }
  23. moveCursor(n) {
  24. this.cursor += n;
  25. }
  26. reset() {
  27. this.input = this.value = '';
  28. this.cursor = 0;
  29. return this.render();
  30. }
  31. dispatch(ch, key) {
  32. if (!ch || key.ctrl || key.code) return this.alert();
  33. this.append(ch);
  34. }
  35. append(ch) {
  36. let { cursor, input } = this.state;
  37. this.input = `${input}`.slice(0, cursor) + ch + `${input}`.slice(cursor);
  38. this.moveCursor(String(ch).length);
  39. this.render();
  40. }
  41. insert(str) {
  42. this.append(str);
  43. }
  44. delete() {
  45. let { cursor, input } = this.state;
  46. if (cursor <= 0) return this.alert();
  47. this.input = `${input}`.slice(0, cursor - 1) + `${input}`.slice(cursor);
  48. this.moveCursor(-1);
  49. this.render();
  50. }
  51. deleteForward() {
  52. let { cursor, input } = this.state;
  53. if (input[cursor] === void 0) return this.alert();
  54. this.input = `${input}`.slice(0, cursor) + `${input}`.slice(cursor + 1);
  55. this.render();
  56. }
  57. cutForward() {
  58. let pos = this.cursor;
  59. if (this.input.length <= pos) return this.alert();
  60. this.state.clipboard.push(this.input.slice(pos));
  61. this.input = this.input.slice(0, pos);
  62. this.render();
  63. }
  64. cutLeft() {
  65. let pos = this.cursor;
  66. if (pos === 0) return this.alert();
  67. let before = this.input.slice(0, pos);
  68. let after = this.input.slice(pos);
  69. let words = before.split(' ');
  70. this.state.clipboard.push(words.pop());
  71. this.input = words.join(' ');
  72. this.cursor = this.input.length;
  73. this.input += after;
  74. this.render();
  75. }
  76. paste() {
  77. if (!this.state.clipboard.length) return this.alert();
  78. this.insert(this.state.clipboard.pop());
  79. this.render();
  80. }
  81. toggleCursor() {
  82. if (this.state.prevCursor) {
  83. this.cursor = this.state.prevCursor;
  84. this.state.prevCursor = 0;
  85. } else {
  86. this.state.prevCursor = this.cursor;
  87. this.cursor = 0;
  88. }
  89. this.render();
  90. }
  91. first() {
  92. this.cursor = 0;
  93. this.render();
  94. }
  95. last() {
  96. this.cursor = this.input.length - 1;
  97. this.render();
  98. }
  99. next() {
  100. let init = this.initial != null ? String(this.initial) : '';
  101. if (!init || !init.startsWith(this.input)) return this.alert();
  102. this.input = this.initial;
  103. this.cursor = this.initial.length;
  104. this.render();
  105. }
  106. prev() {
  107. if (!this.input) return this.alert();
  108. this.reset();
  109. }
  110. backward() {
  111. return this.left();
  112. }
  113. forward() {
  114. return this.right();
  115. }
  116. right() {
  117. if (this.cursor >= this.input.length) return this.alert();
  118. this.moveCursor(1);
  119. return this.render();
  120. }
  121. left() {
  122. if (this.cursor <= 0) return this.alert();
  123. this.moveCursor(-1);
  124. return this.render();
  125. }
  126. isValue(value) {
  127. return !!value;
  128. }
  129. async format(input = this.value) {
  130. let initial = await this.resolve(this.initial, this.state);
  131. if (!this.state.submitted) {
  132. return placeholder(this, { input, initial, pos: this.cursor });
  133. }
  134. return this.styles.submitted(input || initial);
  135. }
  136. async render() {
  137. let size = this.state.size;
  138. let prefix = await this.prefix();
  139. let separator = await this.separator();
  140. let message = await this.message();
  141. let prompt = [prefix, message, separator].filter(Boolean).join(' ');
  142. this.state.prompt = prompt;
  143. let header = await this.header();
  144. let output = await this.format();
  145. let help = (await this.error()) || (await this.hint());
  146. let footer = await this.footer();
  147. if (help && !output.includes(help)) output += ' ' + help;
  148. prompt += ' ' + output;
  149. this.clear(size);
  150. this.write([header, prompt, footer].filter(Boolean).join('\n'));
  151. this.restore();
  152. }
  153. }
  154. module.exports = StringPrompt;