@unscientificjszhai/howto 1.0.0-alpha.2 → 1.0.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.
@@ -1,168 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- exports.PlaceholderSelectionBackError = exports.PlaceholderResolutionError = void 0;
13
- exports.resolveCommandPlaceholders = resolveCommandPlaceholders;
14
- exports.promptPlaceholderValues = promptPlaceholderValues;
15
- exports.replaceCommandPlaceholders = replaceCommandPlaceholders;
16
- exports.assertNoUnresolvedPlaceholders = assertNoUnresolvedPlaceholders;
17
- exports.formatPlaceholderPrompt = formatPlaceholderPrompt;
18
- const interactive_1 = require("./interactive");
19
- const key_input_1 = require("./key-input");
20
- class PlaceholderResolutionError extends Error {
21
- constructor(message) {
22
- super(message);
23
- this.name = "PlaceholderResolutionError";
24
- }
25
- }
26
- exports.PlaceholderResolutionError = PlaceholderResolutionError;
27
- class PlaceholderSelectionBackError extends Error {
28
- constructor(message = "Return to command selection") {
29
- super(message);
30
- this.name = "PlaceholderSelectionBackError";
31
- }
32
- }
33
- exports.PlaceholderSelectionBackError = PlaceholderSelectionBackError;
34
- const UNRESOLVED_PLACEHOLDER_PATTERN = /{{[^{}]*}}/;
35
- function resolveCommandPlaceholders(candidate_1) {
36
- return __awaiter(this, arguments, void 0, function* (candidate, streams = {}) {
37
- var _a, _b;
38
- const input = (_a = streams.input) !== null && _a !== void 0 ? _a : process.stdin;
39
- const output = (_b = streams.output) !== null && _b !== void 0 ? _b : process.stdout;
40
- const values = yield promptPlaceholderValues(candidate.placeholders, { input, output });
41
- const command = replaceCommandPlaceholders(candidate.command, values);
42
- assertNoUnresolvedPlaceholders(command);
43
- return command;
44
- });
45
- }
46
- function promptPlaceholderValues(placeholders, streams) {
47
- return __awaiter(this, void 0, void 0, function* () {
48
- if (placeholders.length === 0) {
49
- return new Map();
50
- }
51
- return readPlaceholderValuesWithRawInput(placeholders, streams);
52
- });
53
- }
54
- function replaceCommandPlaceholders(command, values) {
55
- let resolvedCommand = command;
56
- for (const [name, value] of values) {
57
- resolvedCommand = resolvedCommand.split(`{{${name}}}`).join(value);
58
- }
59
- return resolvedCommand;
60
- }
61
- function assertNoUnresolvedPlaceholders(command) {
62
- if (UNRESOLVED_PLACEHOLDER_PATTERN.test(command)) {
63
- throw new PlaceholderResolutionError("final command contains unresolved placeholders");
64
- }
65
- }
66
- function readPlaceholderValuesWithRawInput(placeholders, streams) {
67
- const { input, output } = streams;
68
- return new Promise((resolve, reject) => {
69
- let activeIndex = 0;
70
- let renderedLineCount = 0;
71
- const values = placeholders.map(() => "");
72
- let buffer = "";
73
- let cleanupKeypressSession;
74
- const clearRenderedPrompt = () => {
75
- if (renderedLineCount > 0) {
76
- output.write(`\x1B[${renderedLineCount}A\x1B[J`);
77
- renderedLineCount = 0;
78
- }
79
- };
80
- const cleanup = () => {
81
- cleanupKeypressSession === null || cleanupKeypressSession === void 0 ? void 0 : cleanupKeypressSession();
82
- cleanupKeypressSession = undefined;
83
- clearRenderedPrompt();
84
- };
85
- const finish = () => {
86
- cleanup();
87
- resolve(new Map(placeholders.map((placeholder, index) => [placeholder.name, values[index]])));
88
- };
89
- const cancel = () => {
90
- cleanup();
91
- reject(new interactive_1.InteractionCancelledError("Placeholder input cancelled"));
92
- };
93
- const backToSelection = () => {
94
- cleanup();
95
- reject(new PlaceholderSelectionBackError());
96
- };
97
- const render = () => {
98
- const text = formatPlaceholderPrompt(placeholders, values, activeIndex, buffer);
99
- clearRenderedPrompt();
100
- output.write(`${text}\n`);
101
- renderedLineCount = text.split("\n").length + 1;
102
- };
103
- const onKeypress = (inputText, key = {}) => {
104
- if (key.ctrl === true && key.name === "c") {
105
- cancel();
106
- return;
107
- }
108
- if (key.name === "escape") {
109
- if (activeIndex === 0) {
110
- values[0] = "";
111
- buffer = "";
112
- render();
113
- backToSelection();
114
- return;
115
- }
116
- values[activeIndex] = "";
117
- activeIndex -= 1;
118
- buffer = values[activeIndex];
119
- render();
120
- return;
121
- }
122
- if (key.name === "return" || key.name === "enter") {
123
- values[activeIndex] = buffer;
124
- if (activeIndex === placeholders.length - 1) {
125
- render();
126
- finish();
127
- return;
128
- }
129
- activeIndex += 1;
130
- buffer = values[activeIndex];
131
- render();
132
- return;
133
- }
134
- if (key.name === "backspace") {
135
- buffer = Array.from(buffer).slice(0, -1).join("");
136
- render();
137
- return;
138
- }
139
- if (isPrintableInput(inputText)) {
140
- buffer += inputText;
141
- render();
142
- }
143
- };
144
- cleanupKeypressSession = (0, key_input_1.beginKeypressSession)(input, onKeypress);
145
- render();
146
- });
147
- }
148
- function formatPlaceholderPrompt(placeholders, values, activeIndex, buffer) {
149
- const lines = ["", "? Fill command placeholders", ""];
150
- placeholders.slice(0, activeIndex + 1).forEach((placeholder, index) => {
151
- lines.push(`${placeholder.name}: ${placeholder.description}`);
152
- if (index < activeIndex) {
153
- lines.push(`> ${values[index]}`);
154
- }
155
- else if (index === activeIndex) {
156
- lines.push(`> ${buffer}`);
157
- }
158
- if (index <= activeIndex && index < placeholders.length - 1) {
159
- lines.push("");
160
- }
161
- });
162
- lines.push("");
163
- lines.push("Press Enter for next value, Esc to go back, Ctrl+C to cancel.");
164
- return lines.join("\n");
165
- }
166
- function isPrintableInput(inputText) {
167
- return inputText.length > 0 && !/[\x00-\x1F\x7F]/.test(inputText);
168
- }