index.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 checkboxTheme = {
  12. icon: {
  13. checked: yoctocolors_cjs_1.default.green(figures_1.default.circleFilled),
  14. unchecked: figures_1.default.circle,
  15. cursor: figures_1.default.pointer,
  16. },
  17. style: {
  18. disabledChoice: (text) => yoctocolors_cjs_1.default.dim(`- ${text}`),
  19. renderSelectedChoices: (selectedChoices) => selectedChoices.map((choice) => choice.short).join(', '),
  20. description: (text) => yoctocolors_cjs_1.default.cyan(text),
  21. },
  22. helpMode: 'auto',
  23. };
  24. function isSelectable(item) {
  25. return !core_1.Separator.isSeparator(item) && !item.disabled;
  26. }
  27. function isChecked(item) {
  28. return isSelectable(item) && Boolean(item.checked);
  29. }
  30. function toggle(item) {
  31. return isSelectable(item) ? { ...item, checked: !item.checked } : item;
  32. }
  33. function check(checked) {
  34. return function (item) {
  35. return isSelectable(item) ? { ...item, checked } : item;
  36. };
  37. }
  38. function normalizeChoices(choices) {
  39. return choices.map((choice) => {
  40. if (core_1.Separator.isSeparator(choice))
  41. return choice;
  42. if (typeof choice === 'string') {
  43. return {
  44. value: choice,
  45. name: choice,
  46. short: choice,
  47. disabled: false,
  48. checked: false,
  49. };
  50. }
  51. const name = choice.name ?? String(choice.value);
  52. const normalizedChoice = {
  53. value: choice.value,
  54. name,
  55. short: choice.short ?? name,
  56. disabled: choice.disabled ?? false,
  57. checked: choice.checked ?? false,
  58. };
  59. if (choice.description) {
  60. normalizedChoice.description = choice.description;
  61. }
  62. return normalizedChoice;
  63. });
  64. }
  65. exports.default = (0, core_1.createPrompt)((config, done) => {
  66. const { instructions, pageSize = 7, loop = true, required, validate = () => true, } = config;
  67. const shortcuts = { all: 'a', invert: 'i', ...config.shortcuts };
  68. const theme = (0, core_1.makeTheme)(checkboxTheme, config.theme);
  69. const firstRender = (0, core_1.useRef)(true);
  70. const [status, setStatus] = (0, core_1.useState)('idle');
  71. const prefix = (0, core_1.usePrefix)({ status, theme });
  72. const [items, setItems] = (0, core_1.useState)(normalizeChoices(config.choices));
  73. const bounds = (0, core_1.useMemo)(() => {
  74. const first = items.findIndex(isSelectable);
  75. const last = items.findLastIndex(isSelectable);
  76. if (first === -1) {
  77. throw new core_1.ValidationError('[checkbox prompt] No selectable choices. All choices are disabled.');
  78. }
  79. return { first, last };
  80. }, [items]);
  81. const [active, setActive] = (0, core_1.useState)(bounds.first);
  82. const [showHelpTip, setShowHelpTip] = (0, core_1.useState)(true);
  83. const [errorMsg, setError] = (0, core_1.useState)();
  84. (0, core_1.useKeypress)(async (key) => {
  85. if ((0, core_1.isEnterKey)(key)) {
  86. const selection = items.filter(isChecked);
  87. const isValid = await validate([...selection]);
  88. if (required && !items.some(isChecked)) {
  89. setError('At least one choice must be selected');
  90. }
  91. else if (isValid === true) {
  92. setStatus('done');
  93. done(selection.map((choice) => choice.value));
  94. }
  95. else {
  96. setError(isValid || 'You must select a valid value');
  97. }
  98. }
  99. else if ((0, core_1.isUpKey)(key) || (0, core_1.isDownKey)(key)) {
  100. if (loop ||
  101. ((0, core_1.isUpKey)(key) && active !== bounds.first) ||
  102. ((0, core_1.isDownKey)(key) && active !== bounds.last)) {
  103. const offset = (0, core_1.isUpKey)(key) ? -1 : 1;
  104. let next = active;
  105. do {
  106. next = (next + offset + items.length) % items.length;
  107. } while (!isSelectable(items[next]));
  108. setActive(next);
  109. }
  110. }
  111. else if ((0, core_1.isSpaceKey)(key)) {
  112. setError(undefined);
  113. setShowHelpTip(false);
  114. setItems(items.map((choice, i) => (i === active ? toggle(choice) : choice)));
  115. }
  116. else if (key.name === shortcuts.all) {
  117. const selectAll = items.some((choice) => isSelectable(choice) && !choice.checked);
  118. setItems(items.map(check(selectAll)));
  119. }
  120. else if (key.name === shortcuts.invert) {
  121. setItems(items.map(toggle));
  122. }
  123. else if ((0, core_1.isNumberKey)(key)) {
  124. // Adjust index to start at 1
  125. const position = Number(key.name) - 1;
  126. const item = items[position];
  127. if (item != null && isSelectable(item)) {
  128. setActive(position);
  129. setItems(items.map((choice, i) => (i === position ? toggle(choice) : choice)));
  130. }
  131. }
  132. });
  133. const message = theme.style.message(config.message, status);
  134. let description;
  135. const page = (0, core_1.usePagination)({
  136. items,
  137. active,
  138. renderItem({ item, isActive }) {
  139. if (core_1.Separator.isSeparator(item)) {
  140. return ` ${item.separator}`;
  141. }
  142. if (item.disabled) {
  143. const disabledLabel = typeof item.disabled === 'string' ? item.disabled : '(disabled)';
  144. return theme.style.disabledChoice(`${item.name} ${disabledLabel}`);
  145. }
  146. if (isActive) {
  147. description = item.description;
  148. }
  149. const checkbox = item.checked ? theme.icon.checked : theme.icon.unchecked;
  150. const color = isActive ? theme.style.highlight : (x) => x;
  151. const cursor = isActive ? theme.icon.cursor : ' ';
  152. return color(`${cursor}${checkbox} ${item.name}`);
  153. },
  154. pageSize,
  155. loop,
  156. });
  157. if (status === 'done') {
  158. const selection = items.filter(isChecked);
  159. const answer = theme.style.answer(theme.style.renderSelectedChoices(selection, items));
  160. return `${prefix} ${message} ${answer}`;
  161. }
  162. let helpTipTop = '';
  163. let helpTipBottom = '';
  164. if (theme.helpMode === 'always' ||
  165. (theme.helpMode === 'auto' &&
  166. showHelpTip &&
  167. (instructions === undefined || instructions))) {
  168. if (typeof instructions === 'string') {
  169. helpTipTop = instructions;
  170. }
  171. else {
  172. const keys = [
  173. `${theme.style.key('space')} to select`,
  174. shortcuts.all ? `${theme.style.key(shortcuts.all)} to toggle all` : '',
  175. shortcuts.invert
  176. ? `${theme.style.key(shortcuts.invert)} to invert selection`
  177. : '',
  178. `and ${theme.style.key('enter')} to proceed`,
  179. ];
  180. helpTipTop = ` (Press ${keys.filter((key) => key !== '').join(', ')})`;
  181. }
  182. if (items.length > pageSize &&
  183. (theme.helpMode === 'always' ||
  184. // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
  185. (theme.helpMode === 'auto' && firstRender.current))) {
  186. helpTipBottom = `\n${theme.style.help('(Use arrow keys to reveal more choices)')}`;
  187. firstRender.current = false;
  188. }
  189. }
  190. const choiceDescription = description
  191. ? `\n${theme.style.description(description)}`
  192. : ``;
  193. let error = '';
  194. if (errorMsg) {
  195. error = `\n${theme.style.error(errorMsg)}`;
  196. }
  197. return `${prefix} ${message}${helpTipTop}\n${page}${helpTipBottom}${choiceDescription}${error}${ansi_escapes_1.default.cursorHide}`;
  198. });
  199. var core_2 = require("@inquirer/core");
  200. Object.defineProperty(exports, "Separator", { enumerable: true, get: function () { return core_2.Separator; } });