PatternPrompt.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. function _defineProperty(obj, key, value) {
  31. if (key in obj) {
  32. Object.defineProperty(obj, key, {
  33. value: value,
  34. enumerable: true,
  35. configurable: true,
  36. writable: true
  37. });
  38. } else {
  39. obj[key] = value;
  40. }
  41. return obj;
  42. }
  43. const {CLEAR} = _jestUtil().specialChars;
  44. const usage = entity =>
  45. `\n${_chalk().default.bold('Pattern Mode Usage')}\n` +
  46. ` ${_chalk().default.dim('\u203A Press')} Esc ${_chalk().default.dim(
  47. 'to exit pattern mode.'
  48. )}\n` +
  49. ` ${_chalk().default.dim('\u203A Press')} Enter ` +
  50. `${_chalk().default.dim(`to filter by a ${entity} regex pattern.`)}\n` +
  51. '\n';
  52. const usageRows = usage('').split('\n').length;
  53. class PatternPrompt {
  54. constructor(pipe, prompt) {
  55. _defineProperty(this, '_pipe', void 0);
  56. _defineProperty(this, '_prompt', void 0);
  57. _defineProperty(this, '_entityName', void 0);
  58. _defineProperty(this, '_currentUsageRows', void 0);
  59. // TODO: Should come in the constructor
  60. this._entityName = '';
  61. this._pipe = pipe;
  62. this._prompt = prompt;
  63. this._currentUsageRows = usageRows;
  64. }
  65. run(onSuccess, onCancel, options) {
  66. this._pipe.write(_ansiEscapes().default.cursorHide);
  67. this._pipe.write(CLEAR);
  68. if (options && options.header) {
  69. this._pipe.write(options.header + '\n');
  70. this._currentUsageRows = usageRows + options.header.split('\n').length;
  71. } else {
  72. this._currentUsageRows = usageRows;
  73. }
  74. this._pipe.write(usage(this._entityName));
  75. this._pipe.write(_ansiEscapes().default.cursorShow);
  76. this._prompt.enter(this._onChange.bind(this), onSuccess, onCancel);
  77. }
  78. _onChange(_pattern, _options) {
  79. this._pipe.write(_ansiEscapes().default.eraseLine);
  80. this._pipe.write(_ansiEscapes().default.cursorLeft);
  81. }
  82. }
  83. exports.default = PatternPrompt;