5b7dca2eeac26a805beada2689a88c5a0645d58bbef8001892148b12fe21b44231b42ef4eccae912c5c7719ccc30bac573956423641c95034000b9c61b8160 5.0 KB

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