text.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. 'use strict';
  2. const color = require('kleur');
  3. const Prompt = require('./prompt');
  4. var _require = require('sisteransi');
  5. const cursor = _require.cursor;
  6. var _require2 = require('../util');
  7. const style = _require2.style,
  8. clear = _require2.clear;
  9. /**
  10. * TextPrompt Base Element
  11. * @param {Object} opts Options
  12. * @param {String} opts.message Message
  13. * @param {String} [opts.style='default'] Render style
  14. * @param {String} [opts.initial] Default value
  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.value = '';
  24. this.cursor = Number(!!this.initial);
  25. this.clear = clear('');
  26. this.render();
  27. }
  28. set value(v) {
  29. if (!v && this.initial) {
  30. this.placeholder = true;
  31. this.rendered = color.gray(this.transform.render(this.initial));
  32. } else {
  33. this.placeholder = false;
  34. this.rendered = this.transform.render(v);
  35. }
  36. this._value = v;
  37. this.fire();
  38. }
  39. get value() {
  40. return this._value;
  41. }
  42. reset() {
  43. this.value = '';
  44. this.fire();
  45. this.render();
  46. }
  47. abort() {
  48. this.value = this.value || this.initial;
  49. this.done = this.aborted = true;
  50. this.fire();
  51. this.render();
  52. this.out.write('\n');
  53. this.close();
  54. }
  55. submit() {
  56. this.value = this.value || this.initial;
  57. this.done = true;
  58. this.aborted = false;
  59. this.fire();
  60. this.render();
  61. this.out.write('\n');
  62. this.close();
  63. }
  64. next() {
  65. if (!this.placeholder) return this.bell();
  66. this.value = this.initial;
  67. this.cursor = this.rendered.length;
  68. this.fire();
  69. this.render();
  70. }
  71. moveCursor(n) {
  72. if (this.placeholder) return;
  73. this.cursor = this.cursor + n;
  74. }
  75. _(c, key) {
  76. let s1 = this.value.slice(0, this.cursor);
  77. let s2 = this.value.slice(this.cursor);
  78. this.moveCursor(1);
  79. this.value = `${s1}${c}${s2}`;
  80. if (this.placeholder) this.cursor = 0;
  81. this.render();
  82. }
  83. delete() {
  84. if (this.value.length === 0) return this.bell();
  85. let s1 = this.value.slice(0, this.cursor - 1);
  86. let s2 = this.value.slice(this.cursor);
  87. this.value = `${s1}${s2}`;
  88. this.moveCursor(-1);
  89. this.render();
  90. }
  91. first() {
  92. this.cursor = 0;
  93. this.render();
  94. }
  95. last() {
  96. this.cursor = this.value.length;
  97. this.render();
  98. }
  99. left() {
  100. if (this.cursor <= 0 || this.placeholder) return this.bell();
  101. this.moveCursor(-1);
  102. this.render();
  103. }
  104. right() {
  105. if (this.cursor * this.scale >= this.rendered.length || this.placeholder) return this.bell();
  106. this.moveCursor(1);
  107. this.render();
  108. }
  109. render() {
  110. const prompt = [style.symbol(this.done, this.aborted), color.bold(this.msg), style.delimiter(this.done), this.rendered].join(' ');
  111. this.out.write(this.clear + prompt);
  112. this.out.write(cursor.move(this.placeholder ? -this.initial.length * this.scale : -this.rendered.length + this.cursor * this.scale));
  113. this.clear = clear(prompt);
  114. }
  115. }
  116. module.exports = TextPrompt;