|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- 'use strict';
-
- Object.defineProperty(exports, '__esModule', {
- value: true
- });
- exports.default = void 0;
-
- function _constants() {
- const data = require('../constants');
-
- _constants = function () {
- return data;
- };
-
- return data;
- }
-
- function _defineProperty(obj, key, value) {
- if (key in obj) {
- Object.defineProperty(obj, key, {
- value: value,
- enumerable: true,
- configurable: true,
- writable: true
- });
- } else {
- obj[key] = value;
- }
- return obj;
- }
-
- class Prompt {
- constructor() {
- _defineProperty(this, '_entering', void 0);
-
- _defineProperty(this, '_value', void 0);
-
- _defineProperty(this, '_onChange', void 0);
-
- _defineProperty(this, '_onSuccess', void 0);
-
- _defineProperty(this, '_onCancel', void 0);
-
- _defineProperty(this, '_offset', void 0);
-
- _defineProperty(this, '_promptLength', void 0);
-
- _defineProperty(this, '_selection', void 0);
-
- _defineProperty(this, '_onResize', () => {
- this._onChange();
- });
-
- // Copied from `enter` to satisfy TS
- this._entering = true;
- this._value = '';
- this._selection = null;
- this._offset = -1;
- this._promptLength = 0;
-
- this._onChange = () => {};
-
- this._onSuccess = () => {};
-
- this._onCancel = () => {};
- }
-
- enter(onChange, onSuccess, onCancel) {
- this._entering = true;
- this._value = '';
- this._onSuccess = onSuccess;
- this._onCancel = onCancel;
- this._selection = null;
- this._offset = -1;
- this._promptLength = 0;
-
- this._onChange = () =>
- onChange(this._value, {
- max: 10,
- offset: this._offset
- });
-
- this._onChange();
-
- process.stdout.on('resize', this._onResize);
- }
-
- setPromptLength(length) {
- this._promptLength = length;
- }
-
- setPromptSelection(selected) {
- this._selection = selected;
- }
-
- put(key) {
- switch (key) {
- case _constants().KEYS.ENTER:
- this._entering = false;
-
- this._onSuccess(this._selection || this._value);
-
- this.abort();
- break;
-
- case _constants().KEYS.ESCAPE:
- this._entering = false;
-
- this._onCancel(this._value);
-
- this.abort();
- break;
-
- case _constants().KEYS.ARROW_DOWN:
- this._offset = Math.min(this._offset + 1, this._promptLength - 1);
-
- this._onChange();
-
- break;
-
- case _constants().KEYS.ARROW_UP:
- this._offset = Math.max(this._offset - 1, -1);
-
- this._onChange();
-
- break;
-
- case _constants().KEYS.ARROW_LEFT:
- case _constants().KEYS.ARROW_RIGHT:
- break;
-
- case _constants().KEYS.CONTROL_U:
- this._value = '';
- this._offset = -1;
- this._selection = null;
-
- this._onChange();
-
- break;
-
- default:
- this._value =
- key === _constants().KEYS.BACKSPACE
- ? this._value.slice(0, -1)
- : this._value + key;
- this._offset = -1;
- this._selection = null;
-
- this._onChange();
-
- break;
- }
- }
-
- abort() {
- this._entering = false;
- this._value = '';
- process.stdout.removeListener('resize', this._onResize);
- }
-
- isEntering() {
- return this._entering;
- }
- }
-
- exports.default = Prompt;
|