text.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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.clear = clear(``, this.out.columns);
  28. this.render();
  29. }
  30. set value(v) {
  31. if (!v && this.initial) {
  32. this.placeholder = true;
  33. this.rendered = color.gray(this.transform.render(this.initial));
  34. } else {
  35. this.placeholder = false;
  36. this.rendered = this.transform.render(v);
  37. }
  38. this._value = v;
  39. this.fire();
  40. }
  41. get value() {
  42. return this._value;
  43. }
  44. reset() {
  45. this.value = ``;
  46. this.cursor = Number(!!this.initial);
  47. this.fire();
  48. this.render();
  49. }
  50. exit() {
  51. this.abort();
  52. }
  53. abort() {
  54. this.value = this.value || this.initial;
  55. this.done = this.aborted = true;
  56. this.error = false;
  57. this.red = false;
  58. this.fire();
  59. this.render();
  60. this.out.write('\n');
  61. this.close();
  62. }
  63. async validate() {
  64. let valid = await this.validator(this.value);
  65. if (typeof valid === `string`) {
  66. this.errorMsg = valid;
  67. valid = false;
  68. }
  69. this.error = !valid;
  70. }
  71. async submit() {
  72. this.value = this.value || this.initial;
  73. await this.validate();
  74. if (this.error) {
  75. this.red = true;
  76. this.fire();
  77. this.render();
  78. return;
  79. }
  80. this.done = true;
  81. this.aborted = false;
  82. this.fire();
  83. this.render();
  84. this.out.write('\n');
  85. this.close();
  86. }
  87. next() {
  88. if (!this.placeholder) return this.bell();
  89. this.value = this.initial;
  90. this.cursor = this.rendered.length;
  91. this.fire();
  92. this.render();
  93. }
  94. moveCursor(n) {
  95. if (this.placeholder) return;
  96. this.cursor = this.cursor+n;
  97. }
  98. _(c, key) {
  99. let s1 = this.value.slice(0, this.cursor);
  100. let s2 = this.value.slice(this.cursor);
  101. this.value = `${s1}${c}${s2}`;
  102. this.red = false;
  103. this.cursor = this.placeholder ? 0 : s1.length+1;
  104. this.render();
  105. }
  106. delete() {
  107. if (this.cursor === 0) return this.bell();
  108. let s1 = this.value.slice(0, this.cursor-1);
  109. let s2 = this.value.slice(this.cursor);
  110. this.value = `${s1}${s2}`;
  111. this.red = false;
  112. this.moveCursor(-1);
  113. this.render();
  114. }
  115. deleteForward() {
  116. if(this.cursor*this.scale >= this.rendered.length || this.placeholder) return this.bell();
  117. let s1 = this.value.slice(0, this.cursor);
  118. let s2 = this.value.slice(this.cursor+1);
  119. this.value = `${s1}${s2}`;
  120. this.red = false;
  121. this.render();
  122. }
  123. first() {
  124. this.cursor = 0;
  125. this.render();
  126. }
  127. last() {
  128. this.cursor = this.value.length;
  129. this.render();
  130. }
  131. left() {
  132. if (this.cursor <= 0 || this.placeholder) return this.bell();
  133. this.moveCursor(-1);
  134. this.render();
  135. }
  136. right() {
  137. if (this.cursor*this.scale >= this.rendered.length || this.placeholder) return this.bell();
  138. this.moveCursor(1);
  139. this.render();
  140. }
  141. render() {
  142. if (this.closed) return;
  143. if (!this.firstRender) {
  144. if (this.outputError)
  145. this.out.write(cursor.down(lines(this.outputError, this.out.columns) - 1) + clear(this.outputError, this.out.columns));
  146. this.out.write(clear(this.outputText, this.out.columns));
  147. }
  148. super.render();
  149. this.outputError = '';
  150. this.outputText = [
  151. style.symbol(this.done, this.aborted),
  152. color.bold(this.msg),
  153. style.delimiter(this.done),
  154. this.red ? color.red(this.rendered) : this.rendered
  155. ].join(` `);
  156. if (this.error) {
  157. this.outputError += this.errorMsg.split(`\n`)
  158. .reduce((a, l, i) => a + `\n${i ? ' ' : figures.pointerSmall} ${color.red().italic(l)}`, ``);
  159. }
  160. this.out.write(erase.line + cursor.to(0) + this.outputText + cursor.save + this.outputError + cursor.restore);
  161. }
  162. }
  163. module.exports = TextPrompt;