Prompt.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _constants = require('../constants');
  7. function _defineProperty(obj, key, value) {
  8. if (key in obj) {
  9. Object.defineProperty(obj, key, {
  10. value: value,
  11. enumerable: true,
  12. configurable: true,
  13. writable: true
  14. });
  15. } else {
  16. obj[key] = value;
  17. }
  18. return obj;
  19. }
  20. class Prompt {
  21. constructor() {
  22. _defineProperty(this, '_entering', void 0);
  23. _defineProperty(this, '_value', void 0);
  24. _defineProperty(this, '_onChange', void 0);
  25. _defineProperty(this, '_onSuccess', void 0);
  26. _defineProperty(this, '_onCancel', void 0);
  27. _defineProperty(this, '_offset', void 0);
  28. _defineProperty(this, '_promptLength', void 0);
  29. _defineProperty(this, '_selection', void 0);
  30. _defineProperty(this, '_onResize', () => {
  31. this._onChange();
  32. });
  33. // Copied from `enter` to satisfy TS
  34. this._entering = true;
  35. this._value = '';
  36. this._selection = null;
  37. this._offset = -1;
  38. this._promptLength = 0;
  39. this._onChange = () => {};
  40. this._onSuccess = () => {};
  41. this._onCancel = () => {};
  42. }
  43. enter(onChange, onSuccess, onCancel) {
  44. this._entering = true;
  45. this._value = '';
  46. this._onSuccess = onSuccess;
  47. this._onCancel = onCancel;
  48. this._selection = null;
  49. this._offset = -1;
  50. this._promptLength = 0;
  51. this._onChange = () =>
  52. onChange(this._value, {
  53. max: 10,
  54. offset: this._offset
  55. });
  56. this._onChange();
  57. process.stdout.on('resize', this._onResize);
  58. }
  59. setPromptLength(length) {
  60. this._promptLength = length;
  61. }
  62. setPromptSelection(selected) {
  63. this._selection = selected;
  64. }
  65. put(key) {
  66. switch (key) {
  67. case _constants.KEYS.ENTER:
  68. this._entering = false;
  69. this._onSuccess(this._selection || this._value);
  70. this.abort();
  71. break;
  72. case _constants.KEYS.ESCAPE:
  73. this._entering = false;
  74. this._onCancel(this._value);
  75. this.abort();
  76. break;
  77. case _constants.KEYS.ARROW_DOWN:
  78. this._offset = Math.min(this._offset + 1, this._promptLength - 1);
  79. this._onChange();
  80. break;
  81. case _constants.KEYS.ARROW_UP:
  82. this._offset = Math.max(this._offset - 1, -1);
  83. this._onChange();
  84. break;
  85. case _constants.KEYS.ARROW_LEFT:
  86. case _constants.KEYS.ARROW_RIGHT:
  87. break;
  88. case _constants.KEYS.CONTROL_U:
  89. this._value = '';
  90. this._offset = -1;
  91. this._selection = null;
  92. this._onChange();
  93. break;
  94. default:
  95. this._value =
  96. key === _constants.KEYS.BACKSPACE
  97. ? this._value.slice(0, -1)
  98. : this._value + key;
  99. this._offset = -1;
  100. this._selection = null;
  101. this._onChange();
  102. break;
  103. }
  104. }
  105. abort() {
  106. this._entering = false;
  107. this._value = '';
  108. process.stdout.removeListener('resize', this._onResize);
  109. }
  110. isEntering() {
  111. return this._entering;
  112. }
  113. }
  114. exports.default = Prompt;