index.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. exports.Separator = void 0;
  7. const core_1 = require("@inquirer/core");
  8. const yoctocolors_cjs_1 = __importDefault(require("yoctocolors-cjs"));
  9. const figures_1 = __importDefault(require("@inquirer/figures"));
  10. const ansi_escapes_1 = __importDefault(require("ansi-escapes"));
  11. const selectTheme = {
  12. icon: { cursor: figures_1.default.pointer },
  13. style: {
  14. disabled: (text) => yoctocolors_cjs_1.default.dim(`- ${text}`),
  15. description: (text) => yoctocolors_cjs_1.default.cyan(text),
  16. },
  17. helpMode: 'auto',
  18. indexMode: 'hidden',
  19. };
  20. function isSelectable(item) {
  21. return !core_1.Separator.isSeparator(item) && !item.disabled;
  22. }
  23. function normalizeChoices(choices) {
  24. return choices.map((choice) => {
  25. if (core_1.Separator.isSeparator(choice))
  26. return choice;
  27. if (typeof choice === 'string') {
  28. return {
  29. value: choice,
  30. name: choice,
  31. short: choice,
  32. disabled: false,
  33. };
  34. }
  35. const name = choice.name ?? String(choice.value);
  36. const normalizedChoice = {
  37. value: choice.value,
  38. name,
  39. short: choice.short ?? name,
  40. disabled: choice.disabled ?? false,
  41. };
  42. if (choice.description) {
  43. normalizedChoice.description = choice.description;
  44. }
  45. return normalizedChoice;
  46. });
  47. }
  48. exports.default = (0, core_1.createPrompt)((config, done) => {
  49. const { loop = true, pageSize = 7 } = config;
  50. const firstRender = (0, core_1.useRef)(true);
  51. const theme = (0, core_1.makeTheme)(selectTheme, config.theme);
  52. const [status, setStatus] = (0, core_1.useState)('idle');
  53. const prefix = (0, core_1.usePrefix)({ status, theme });
  54. const searchTimeoutRef = (0, core_1.useRef)();
  55. const items = (0, core_1.useMemo)(() => normalizeChoices(config.choices), [config.choices]);
  56. const bounds = (0, core_1.useMemo)(() => {
  57. const first = items.findIndex(isSelectable);
  58. const last = items.findLastIndex(isSelectable);
  59. if (first === -1) {
  60. throw new core_1.ValidationError('[select prompt] No selectable choices. All choices are disabled.');
  61. }
  62. return { first, last };
  63. }, [items]);
  64. const defaultItemIndex = (0, core_1.useMemo)(() => {
  65. if (!('default' in config))
  66. return -1;
  67. return items.findIndex((item) => isSelectable(item) && item.value === config.default);
  68. }, [config.default, items]);
  69. const [active, setActive] = (0, core_1.useState)(defaultItemIndex === -1 ? bounds.first : defaultItemIndex);
  70. // Safe to assume the cursor position always point to a Choice.
  71. const selectedChoice = items[active];
  72. (0, core_1.useKeypress)((key, rl) => {
  73. clearTimeout(searchTimeoutRef.current);
  74. if ((0, core_1.isEnterKey)(key)) {
  75. setStatus('done');
  76. done(selectedChoice.value);
  77. }
  78. else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) {
  79. rl.clearLine(0);
  80. if (loop ||
  81. ((0, core_1.isUpKey)(key) && active !== bounds.first) ||
  82. ((0, core_1.isDownKey)(key) && active !== bounds.last)) {
  83. const offset = (0, core_1.isUpKey)(key) ? -1 : 1;
  84. let next = active;
  85. do {
  86. next = (next + offset + items.length) % items.length;
  87. } while (!isSelectable(items[next]));
  88. setActive(next);
  89. }
  90. }
  91. else if ((0, core_1.isNumberKey)(key) && !Number.isNaN(Number(rl.line))) {
  92. const position = Number(rl.line) - 1;
  93. const item = items[position];
  94. if (item != null && isSelectable(item)) {
  95. setActive(position);
  96. }
  97. searchTimeoutRef.current = setTimeout(() => {
  98. rl.clearLine(0);
  99. }, 700);
  100. }
  101. else if ((0, core_1.isBackspaceKey)(key)) {
  102. rl.clearLine(0);
  103. }
  104. else {
  105. // Default to search
  106. const searchTerm = rl.line.toLowerCase();
  107. const matchIndex = items.findIndex((item) => {
  108. if (core_1.Separator.isSeparator(item) || !isSelectable(item))
  109. return false;
  110. return item.name.toLowerCase().startsWith(searchTerm);
  111. });
  112. if (matchIndex !== -1) {
  113. setActive(matchIndex);
  114. }
  115. searchTimeoutRef.current = setTimeout(() => {
  116. rl.clearLine(0);
  117. }, 700);
  118. }
  119. });
  120. (0, core_1.useEffect)(() => () => {
  121. clearTimeout(searchTimeoutRef.current);
  122. }, []);
  123. const message = theme.style.message(config.message, status);
  124. let helpTipTop = '';
  125. let helpTipBottom = '';
  126. if (theme.helpMode === 'always' ||
  127. (theme.helpMode === 'auto' && firstRender.current)) {
  128. firstRender.current = false;
  129. if (items.length > pageSize) {
  130. helpTipBottom = `\n${theme.style.help('(Use arrow keys to reveal more choices)')}`;
  131. }
  132. else {
  133. helpTipTop = theme.style.help('(Use arrow keys)');
  134. }
  135. }
  136. const page = (0, core_1.usePagination)({
  137. items,
  138. active,
  139. renderItem({ item, isActive, index }) {
  140. if (core_1.Separator.isSeparator(item)) {
  141. return ` ${item.separator}`;
  142. }
  143. const indexLabel = theme.indexMode === 'number' ? `${index + 1}. ` : '';
  144. if (item.disabled) {
  145. const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
  146. return theme.style.disabled(`${indexLabel}${item.name} ${disabledLabel}`);
  147. }
  148. const color = isActive ? theme.style.highlight : (x) => x;
  149. const cursor = isActive ? theme.icon.cursor : ` `;
  150. return color(`${cursor} ${indexLabel}${item.name}`);
  151. },
  152. pageSize,
  153. loop,
  154. });
  155. if (status === 'done') {
  156. return `${prefix} ${message} ${theme.style.answer(selectedChoice.short)}`;
  157. }
  158. const choiceDescription = selectedChoice.description
  159. ? `\n${theme.style.description(selectedChoice.description)}`
  160. : ``;
  161. return `${[prefix, message, helpTipTop].filter(Boolean).join(' ')}\n${page}${helpTipBottom}${choiceDescription}${ansi_escapes_1.default.cursorHide}`;
  162. });
  163. var core_2 = require("@inquirer/core");
  164. Object.defineProperty(exports, "Separator", { enumerable: true, get: function () { return core_2.Separator; } });