PatternPrompt.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. function _ansiEscapes() {
  7. const data = _interopRequireDefault(require('ansi-escapes'));
  8. _ansiEscapes = function () {
  9. return data;
  10. };
  11. return data;
  12. }
  13. function _chalk() {
  14. const data = _interopRequireDefault(require('chalk'));
  15. _chalk = function () {
  16. return data;
  17. };
  18. return data;
  19. }
  20. function _jestUtil() {
  21. const data = require('jest-util');
  22. _jestUtil = function () {
  23. return data;
  24. };
  25. return data;
  26. }
  27. function _interopRequireDefault(obj) {
  28. return obj && obj.__esModule ? obj : {default: obj};
  29. }
  30. /**
  31. * Copyright (c) Meta Platforms, Inc. and affiliates.
  32. *
  33. * This source code is licensed under the MIT license found in the
  34. * LICENSE file in the root directory of this source tree.
  35. */
  36. const {CLEAR} = _jestUtil().specialChars;
  37. const usage = entity =>
  38. `\n${_chalk().default.bold('Pattern Mode Usage')}\n` +
  39. ` ${_chalk().default.dim('\u203A Press')} Esc ${_chalk().default.dim(
  40. 'to exit pattern mode.'
  41. )}\n` +
  42. ` ${_chalk().default.dim('\u203A Press')} Enter ` +
  43. `${_chalk().default.dim(`to filter by a ${entity} regex pattern.`)}\n` +
  44. '\n';
  45. const usageRows = usage('').split('\n').length;
  46. class PatternPrompt {
  47. _currentUsageRows;
  48. constructor(_pipe, _prompt, _entityName = '') {
  49. this._pipe = _pipe;
  50. this._prompt = _prompt;
  51. this._entityName = _entityName;
  52. this._currentUsageRows = usageRows;
  53. }
  54. run(onSuccess, onCancel, options) {
  55. this._pipe.write(_ansiEscapes().default.cursorHide);
  56. this._pipe.write(CLEAR);
  57. if (options && options.header) {
  58. this._pipe.write(`${options.header}\n`);
  59. this._currentUsageRows = usageRows + options.header.split('\n').length;
  60. } else {
  61. this._currentUsageRows = usageRows;
  62. }
  63. this._pipe.write(usage(this._entityName));
  64. this._pipe.write(_ansiEscapes().default.cursorShow);
  65. this._prompt.enter(this._onChange.bind(this), onSuccess, onCancel);
  66. }
  67. _onChange(_pattern, _options) {
  68. this._pipe.write(_ansiEscapes().default.eraseLine);
  69. this._pipe.write(_ansiEscapes().default.cursorLeft);
  70. }
  71. }
  72. exports.default = PatternPrompt;