@prozilla-os/dev-tools 1.1.17 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,42 @@
1
+ import { createPrompt as I, useState as p, usePrefix as K, useKeypress as S, isEnterKey as L, isUpKey as P, isDownKey as b } from "@inquirer/core";
2
+ import { Ansi as a, ANSI as A } from "@prozilla-os/shared";
3
+ const j = async (c) => {
4
+ const m = ({ key: t }) => a.dim(`(${a.bold(t)})`), {
5
+ renderSelected: x = (t) => a.yellow(`❯ ${m(t)} ${t.name}`),
6
+ renderUnselected: $ = (t) => ` ${m(t)} ${t.name}`
7
+ } = c;
8
+ return await I((t, f) => {
9
+ const { choices: n, default: i, hideCursor: g = !0, alignShortcuts: y = !1 } = t, [C, u] = p("pending"), [o, l] = p(i === null ? -1 : i === void 0 ? 0 : n.findIndex((e) => e.value === i)), h = K({});
10
+ if (y) {
11
+ let e = 0;
12
+ for (const s of n)
13
+ e = Math.max(e, s.name.length);
14
+ for (const s of n)
15
+ s.name = s.name + " ".repeat(e - s.name.length);
16
+ }
17
+ S((e, s) => {
18
+ if (L(e)) {
19
+ const r = n[o];
20
+ r && (u("done"), f(r.value));
21
+ } else if (P(e))
22
+ l((o + n.length - 1) % n.length);
23
+ else if (b(e))
24
+ l((o + 1) % n.length);
25
+ else {
26
+ const r = n.findIndex((w) => w.key.toLowerCase() === e.name.toLowerCase());
27
+ r !== -1 && (l(r), u("done"), f(n[r]?.value ?? ""));
28
+ }
29
+ });
30
+ let d = a.bold(t.message);
31
+ if (C === "done")
32
+ return `${h} ${d} ${a.cyan(n[o]?.name ?? "")}`;
33
+ g && (d += A.cursor.hide);
34
+ const v = n.map((e, s) => s === o ? x(e, o) : $(e, s)).join(`
35
+ `);
36
+ return [`${h} ${d}`, v];
37
+ })(c);
38
+ };
39
+ export {
40
+ j as i
41
+ };
42
+ //# sourceMappingURL=interactiveListPrompt-CsphCZPx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"interactiveListPrompt-CsphCZPx.js","sources":["../../src/features/prompts/interactiveListPrompt.ts"],"sourcesContent":["import { createPrompt, useState, useKeypress, usePrefix, isEnterKey, isUpKey, isDownKey } from \"@inquirer/core\";\nimport { ANSI, Ansi } from \"@prozilla-os/shared\";\n\n/**\n * Represents a choice in an {@link interactiveListPrompt}.\n */\nexport interface InteractiveListChoice {\n\t/** The name to display in the list. */\n\tname: string;\n\t/** The value to return if this is submitted. */\n\tvalue: string;\n\t/** The keyboard shortcut that submits this choice. */\n\tkey: string;\n}\n\n/**\n * Configuration for {@link interactiveListPrompt}.\n */\nexport interface InteractiveListConfig {\n\t/** The message to display at the top of the list. */\n\tmessage: string;\n\t/** The list of choices. */\n\tchoices: Array<InteractiveListChoice>;\n\t/** The default value to select. Defaults to the first choice. A value of `null` represents no selection. */\n\tdefault?: string | null;\n\t/** Function that renders the selected choice. */\n\trenderSelected?: (choice: InteractiveListChoice, index: number) => string;\n\t/** Functions that renders the unselected choices. */\n\trenderUnselected?: (choice: InteractiveListChoice, index: number) => string;\n\t/** \n\t * Whether to hide the cursor during selection.\n\t * @default true\n\t */\n\thideCursor?: boolean;\n\t/**\n\t * Whether to horizontally align the shortcuts in the list of choices.\n\t * @default false\n\t */\n\talignShortcuts?: boolean;\n}\n\n/**\n * Creates an interactive list of choices that supports selection via arrow keys + enter or via pressing a key associated with a choice.\n * \n * This is based on {@link https://github.com/pgibler/inquirer-interactive-list-prompt pgibler/inquirer-interactive-list-prompt}.\n * @example\n * ```ts\n * const answer = await interactiveListPrompt({\n * message: \"Select an option:\",\n * choices: [\n * { name: \"Option A\", value: \"A\", key: \"a\" },\n * { name: \"Option B\", value: \"B\", key: \"b\" },\n * { name: \"Option C\", value: \"C\", key: \"c\" },\n * ],\n * });\n * ```\n */\nexport const interactiveListPrompt = async (config: InteractiveListConfig) => {\n\tconst renderKey = ({ key }: InteractiveListChoice) => Ansi.dim(`(${Ansi.bold(key)})`);\n\tconst {\n\t\trenderSelected = (choice: InteractiveListChoice) => Ansi.yellow(`❯ ${renderKey(choice)} ${choice.name}`),\n\t\trenderUnselected = (choice: InteractiveListChoice) => ` ${renderKey(choice)} ${choice.name}`,\n\t} = config;\n\n\treturn await createPrompt<string, InteractiveListConfig>((config, done) => {\n\t\tconst { choices, default: defaultValue, hideCursor = true, alignShortcuts = false } = config;\n\t\tconst [status, setStatus] = useState<\"pending\" | \"done\">(\"pending\");\n\t\tconst [index, setIndex] = useState(defaultValue === null ? -1 : defaultValue === undefined ? 0 : choices.findIndex((choice) => choice.value === defaultValue));\n\t\tconst prefix = usePrefix({});\n\n\t\tif (alignShortcuts) {\n\t\t\tlet maxChoiceNameLength = 0;\n\t\t\tfor (const choice of choices) {\n\t\t\t\tmaxChoiceNameLength = Math.max(maxChoiceNameLength, choice.name.length);\n\t\t\t}\n\t\t\tfor (const choice of choices) {\n\t\t\t\tchoice.name = choice.name + \" \".repeat(maxChoiceNameLength - choice.name.length);\n\t\t\t}\n\t\t}\n\n\t\tuseKeypress((key, _rl) => {\n\t\t\tif (isEnterKey(key)) {\n\t\t\t\tconst selectedChoice = choices[index];\n\t\t\t\t// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n\t\t\t\tif (selectedChoice) {\n\t\t\t\t\tsetStatus(\"done\");\n\t\t\t\t\tdone(selectedChoice.value);\n\t\t\t\t}\n\t\t\t} else if (isUpKey(key)) {\n\t\t\t\tsetIndex((index + choices.length - 1) % choices.length);\n\t\t\t} else if (isDownKey(key)) {\n\t\t\t\tsetIndex((index + 1) % choices.length);\n\t\t\t} else {\n\t\t\t\tconst foundIndex = choices.findIndex((choice) => choice.key.toLowerCase() === key.name.toLowerCase());\n\t\t\t\tif (foundIndex !== -1) {\n\t\t\t\t\tsetIndex(foundIndex);\n\t\t\t\t\tsetStatus(\"done\");\n\t\t\t\t\tdone(choices[foundIndex]?.value ?? \"\");\n\t\t\t\t}\n\t\t\t}\n\t\t});\n\n\t\tlet message = Ansi.bold(config.message);\n\n\t\tif (status === \"done\")\n\t\t\treturn `${prefix} ${message} ${Ansi.cyan(choices[index]?.name ?? \"\")}`;\n\n\t\tif (hideCursor)\n\t\t\tmessage += ANSI.cursor.hide;\n\n\t\tconst renderedChoices = choices\n\t\t\t.map((choice, i) => i === index ? renderSelected(choice, index) : renderUnselected(choice, i))\n\t\t\t.join(\"\\n\");\n\n\t\treturn [`${prefix} ${message}`, renderedChoices];\n\t})(config);\n};"],"names":["interactiveListPrompt","config","renderKey","key","Ansi","renderSelected","choice","renderUnselected","createPrompt","done","choices","defaultValue","hideCursor","alignShortcuts","status","setStatus","useState","index","setIndex","prefix","usePrefix","maxChoiceNameLength","useKeypress","_rl","isEnterKey","selectedChoice","isUpKey","isDownKey","foundIndex","message","ANSI","renderedChoices","i"],"mappings":";;AAyDO,MAAMA,IAAwB,OAAOC,MAAkC;AAC7E,QAAMC,IAAY,CAAC,EAAE,KAAAC,EAAA,MAAiCC,EAAK,IAAI,IAAIA,EAAK,KAAKD,CAAG,CAAC,GAAG,GAC9E;AAAA,IACL,gBAAAE,IAAiB,CAACC,MAAkCF,EAAK,OAAO,KAAKF,EAAUI,CAAM,CAAC,IAAIA,EAAO,IAAI,EAAE;AAAA,IACvG,kBAAAC,IAAmB,CAACD,MAAkC,KAAKJ,EAAUI,CAAM,CAAC,IAAIA,EAAO,IAAI;AAAA,EAAA,IACxFL;AAEJ,SAAO,MAAMO,EAA4C,CAACP,GAAQQ,MAAS;AAC1E,UAAM,EAAE,SAAAC,GAAS,SAASC,GAAc,YAAAC,IAAa,IAAM,gBAAAC,IAAiB,OAAUZ,GAChF,CAACa,GAAQC,CAAS,IAAIC,EAA6B,SAAS,GAC5D,CAACC,GAAOC,CAAQ,IAAIF,EAASL,MAAiB,OAAO,KAAKA,MAAiB,SAAY,IAAID,EAAQ,UAAU,CAACJ,MAAWA,EAAO,UAAUK,CAAY,CAAC,GACvJQ,IAASC,EAAU,EAAE;AAE3B,QAAIP,GAAgB;AACnB,UAA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