text.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. const color = require('kleur');
  2. const Prompt = require('./prompt');
  3. const { erase, cursor } = require('sisteransi');
  4. const { style, clear, lines, figures } = require('../util');
  5. /**
  6. * TextPrompt Base Element
  7. * @param {Object} opts Options
  8. * @param {String} opts.message Message
  9. * @param {String} [opts.style='default'] Render style
  10. * @param {String} [opts.initial] Default value
  11. * @param {Function} [opts.validate] Validate function
  12. * @param {Stream} [opts.stdin] The Readable stream to listen to
  13. * @param {Stream} [opts.stdout] The Writable stream to write readline data to
  14. * @param {String} [opts.error] The invalid error label
  15. */
  16. class TextPrompt extends Prompt {
  17. constructor(opts={}) {
  18. super(opts);
  19. this.transform = style.render(opts.style);
  20. this.scale = this.transform.scale;
  21. this.msg = opts.message;
  22. this.initial = opts.initial || ``;
  23. this.validator = opts.validate || (() => true);
  24. this.value = ``;
  25. this.errorMsg = opts.error || `Please Enter A Valid Value`;
  26. this.cursor = Number(!!this.initial);
  27. this.cursorOffset = 0;
  28. this.clear = clear(``, this.out.columns);
  29. this.render();
  30. }
  31. set value(v) {
  32. if (!v && this.initial) {
  33. this.placeholder = true;
  34. this.rendered = color.gray(this.transform.render(this.initial));
  35. } else {
  36. this.placeholder = false;
  37. this.rendered = this.transform.render(v);
  38. }
  39. this._value = v;
  40. this.fire();
  41. }
  42. get value() {
  43. return this._value;
  44. }
  45. reset() {
  46. this.value = ``;
  47. this.cursor = Number(!!this.initial);
  48. this.cursorOffset = 0;
  49. this.fire();
  50. this.render();
  51. }
  52. exit() {
  53. this.abort();
  54. }
  55. abort() {
  56. this.value = this.value || this.initial;
  57. this.done = this.aborted = true;
  58. this.error = false;
  59. this.red = false;
  60. this.fire();
  61. this.render();
  62. this.out.write('\n');
  63. this.close();
  64. }
  65. async validate() {
  66. let valid = await this.validator(this.value);
  67. if (typeof valid === `string`) {
  68. this.errorMsg = valid;
  69. valid = false;
  70. }
  71. this.error = !valid;
  72. }
  73. async submit() {
  74. this.value = this.value || this.initial;
  75. this.cursorOffset = 0;
  76. this.cursor = this.rendered.length;
  77. await this.validate();
  78. if (this.error) {
  79. this.red = true;
  80. this.fire();
  81. this.render();
  82. return;
  83. }
  84. this.done = true;
  85. this.aborted = false;
  86. this.fire();
  87. this.render();
  88. this.out.write('\n');
  89. this.close();
  90. }
  91. next() {
  92. if (!this.placeholder) return this.bell();
  93. this.value = this.initial;
  94. this.cursor = this.rendered.length;
  95. this.fire();
  96. this.render();
  97. }
  98. moveCursor(n) {
  99. if (this.placeholder) return;
  100. this.cursor = this.cursor+n;
  101. this.cursorOffset += n;
  102. }
  103. _(c, key) {
  104. let s1 = this.value.slice(0, this.cursor);
  105. let s2 = this.value.slice(this.cursor);
  106. this.value = `${s1}${c}${s2}`;
  107. this.red = false;
  108. this.cursor = this.placeholder ? 0 : s1.length+1;
  109. this.render();
  110. }
  111. delete() {
  112. if (this.isCursorAtStart()) return this.bell();
  113. let s1 = this.value.slice(0, this.cursor-1);
  114. let s2 = this.value.slice(this.cursor);
  115. this.value = `${s1}${s2}`;
  116. this.red = false;
  117. if (this.isCursorAtStart()) {
  118. this.cursorOffset = 0
  119. } else {
  120. this.cursorOffset++;
  121. this.moveCursor(-1);
  122. }
  123. this.render();
  124. }
  125. deleteForward() {
  126. if(this.cursor*this.scale >= this.rendered.length || this.placeholder) return this.bell();
  127. let s1 = this.value.slice(0, this.cursor);
  128. let s2 = this.value.slice(this.cursor+1);
  129. this.value = `${s1}${s2}`;
  130. this.red = false;
  131. if (this.isCursorAtEnd()) {
  132. this.cursorOffset = 0;
  133. } else {
  134. this.cursorOffset++;
  135. }
  136. this.render();
  137. }
  138. first() {
  139. this.cursor = 0;
  140. this.render();
  141. }
  142. last() {
  143. this.cursor = this.value.length;
  144. this.render();
  145. }
  146. left() {
  147. if (this.cursor <= 0 || this.placeholder) return this.bell();
  148. this.moveCursor(-1);
  149. this.render();
  150. }
  151. right() {
  152. if (this.cursor*this.scale >= this.rendered.length || this.placeholder) return this.bell();
  153. this.moveCursor(1);
  154. this.render();
  155. }
  156. isCursorAtStart() {
  157. return this.cursor === 0 || (this.placeholder && this.cursor === 1);
  158. }
  159. isCursorAtEnd() {
  160. return this.cursor === this.rendered.length || (this.placeholder && this.cursor === this.rendered.length + 1)
  161. }
  162. render() {
  163. if (this.closed) return;
  164. if (!this.firstRender) {
  165. if (this.outputError)
  166. this.out.write(cursor.down(lines(this.outputError, this.out.columns) - 1) + clear(this.outputError, this.out.columns));
  167. this.out.write(clear(this.outputText, this.out.columns));
  168. }
  169. super.render();
  170. this.outputError = '';
  171. this.outputText = [
  172. style.symbol(this.done, this.aborted),
  173. color.bold(this.msg),
  174. style.delimiter(this.done),
  175. this.red ? color.red(this.rendered) : this.rendered
  176. ].join(` `);
  177. if (this.error) {
  178. this.outputError += this.errorMsg.split(`\n`)
  179. .reduce((a, l, i) => a + `\n${i ? ' ' : figures.pointerSmall} ${color.red().italic(l)}`, ``);
  180. }
  181. this.out.write(erase.line + cursor.to(0) + this.outputText + cursor.save + this.outputError + cursor.restore + cursor.move(this.cursorOffset, 0));
  182. }
  183. }
  184. module.exports = TextPrompt;