@inquirer/core 1.2.2 → 2.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.
- package/README.md +3 -1
- package/dist/cjs/index.js +67 -49
- package/dist/cjs/lib/screen-manager.js +22 -34
- package/dist/cjs/lib/utils.js +1 -0
- package/dist/cjs/types/index.d.cts +1 -1
- package/dist/esm/index.mjs +66 -39
- package/dist/esm/lib/screen-manager.mjs +22 -11
- package/dist/esm/lib/utils.mjs +1 -0
- package/dist/esm/types/index.d.mts +1 -1
- package/package.json +6 -6
- package/dist/cjs/lib/readline.js +0 -60
- package/dist/cjs/types/lib/readline.d.cts +0 -31
- package/dist/esm/lib/readline.mjs +0 -49
- package/dist/esm/types/lib/readline.d.mts +0 -31
package/README.md
CHANGED
|
@@ -75,7 +75,7 @@ See more examples:
|
|
|
75
75
|
|
|
76
76
|
### `createPrompt(viewFn)`
|
|
77
77
|
|
|
78
|
-
The `createPrompt` function returns an asynchronous function that returns a promise resolving to the valid answer a user submit. This prompt function takes the prompt configuration as its first argument (this is defined by each prompt), and the context options as a second argument.
|
|
78
|
+
The `createPrompt` function returns an asynchronous function that returns a cancelable promise resolving to the valid answer a user submit. This prompt function takes the prompt configuration as its first argument (this is defined by each prompt), and the context options as a second argument.
|
|
79
79
|
|
|
80
80
|
The prompt configuration is unique to each prompt. The context options are:
|
|
81
81
|
|
|
@@ -85,6 +85,8 @@ The prompt configuration is unique to each prompt. The context options are:
|
|
|
85
85
|
| output | `NodeJS.WritableStream` | no | The stdout stream (defaults to `process.stdout`) |
|
|
86
86
|
| clearPromptOnDone | `boolean` | no | If true, we'll clear the screen after the prompt is answered |
|
|
87
87
|
|
|
88
|
+
The cancelable promise exposes a `cancel` method that'll exit the prompt and reject the promise.
|
|
89
|
+
|
|
88
90
|
#### Typescript
|
|
89
91
|
|
|
90
92
|
If using typescript, `createPrompt` takes 2 generic arguments (ex `createPrompt<string, { message: string }>()`)
|
package/dist/cjs/index.js
CHANGED
|
@@ -13,21 +13,13 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
17
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
18
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
19
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
20
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
21
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
22
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
23
|
-
});
|
|
24
|
-
};
|
|
25
16
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
17
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
18
|
};
|
|
28
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
20
|
exports.createPrompt = exports.useRef = exports.useKeypress = exports.useEffect = exports.useState = exports.usePrefix = void 0;
|
|
30
21
|
const node_readline_1 = __importDefault(require("node:readline"));
|
|
22
|
+
const type_1 = require("@inquirer/type");
|
|
31
23
|
const mute_stream_1 = __importDefault(require("mute-stream"));
|
|
32
24
|
const screen_manager_mjs_1 = __importDefault(require('./lib/screen-manager.js'));
|
|
33
25
|
const options_mjs_1 = require('./lib/options.js');
|
|
@@ -42,6 +34,14 @@ const hooksEffect = [];
|
|
|
42
34
|
let index = 0;
|
|
43
35
|
let handleChange = () => { };
|
|
44
36
|
let sessionRl;
|
|
37
|
+
const resetHookState = () => {
|
|
38
|
+
hooks.length = 0;
|
|
39
|
+
hooksCleanup.length = 0;
|
|
40
|
+
hooksEffect.length = 0;
|
|
41
|
+
index = 0;
|
|
42
|
+
handleChange = () => { };
|
|
43
|
+
sessionRl = undefined;
|
|
44
|
+
};
|
|
45
45
|
const cleanupHook = (index) => {
|
|
46
46
|
const cleanFn = hooksCleanup[index];
|
|
47
47
|
if (typeof cleanFn === 'function') {
|
|
@@ -111,68 +111,86 @@ function useRef(val) {
|
|
|
111
111
|
}
|
|
112
112
|
exports.useRef = useRef;
|
|
113
113
|
function createPrompt(view) {
|
|
114
|
-
const prompt = (config, context) =>
|
|
114
|
+
const prompt = (config, context) => {
|
|
115
115
|
var _a, _b;
|
|
116
|
+
// Set our state before starting the prompt.
|
|
117
|
+
resetHookState();
|
|
116
118
|
// Default `input` to stdin
|
|
117
119
|
const input = (_a = context === null || context === void 0 ? void 0 : context.input) !== null && _a !== void 0 ? _a : process.stdin;
|
|
118
120
|
// Add mute capabilities to the output
|
|
119
121
|
const output = new mute_stream_1.default();
|
|
120
122
|
output.pipe((_b = context === null || context === void 0 ? void 0 : context.output) !== null && _b !== void 0 ? _b : process.stdout);
|
|
121
|
-
|
|
123
|
+
sessionRl = node_readline_1.default.createInterface({
|
|
122
124
|
terminal: true,
|
|
123
125
|
input,
|
|
124
126
|
output,
|
|
125
127
|
});
|
|
126
|
-
const screen = new screen_manager_mjs_1.default(
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
128
|
+
const screen = new screen_manager_mjs_1.default(sessionRl);
|
|
129
|
+
let cancel = () => { };
|
|
130
|
+
const answer = new type_1.CancelablePromise((resolve, reject) => {
|
|
131
|
+
const onExit = () => {
|
|
132
|
+
try {
|
|
133
|
+
let len = hooksCleanup.length;
|
|
134
|
+
while (len--) {
|
|
135
|
+
cleanupHook(len);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
catch (err) {
|
|
139
|
+
reject(err);
|
|
140
|
+
}
|
|
141
|
+
if (context === null || context === void 0 ? void 0 : context.clearPromptOnDone) {
|
|
142
|
+
screen.clean();
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
screen.clearContent();
|
|
146
|
+
}
|
|
147
|
+
screen.done();
|
|
148
|
+
process.removeListener('SIGINT', onForceExit);
|
|
149
|
+
};
|
|
150
|
+
cancel = () => {
|
|
151
|
+
onExit();
|
|
152
|
+
reject(new Error('Prompt was canceled'));
|
|
153
|
+
};
|
|
154
|
+
let shouldHandleExit = true;
|
|
155
|
+
const onForceExit = () => {
|
|
156
|
+
if (shouldHandleExit) {
|
|
157
|
+
shouldHandleExit = false;
|
|
158
|
+
onExit();
|
|
159
|
+
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
160
|
+
}
|
|
161
|
+
};
|
|
162
|
+
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
163
|
+
process.on('SIGINT', onForceExit);
|
|
133
164
|
const done = (value) => {
|
|
134
165
|
// Delay execution to let time to the hookCleanup functions to registers.
|
|
135
166
|
setImmediate(() => {
|
|
136
|
-
|
|
137
|
-
let len = hooksCleanup.length;
|
|
138
|
-
while (len--) {
|
|
139
|
-
cleanupHook(len);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
catch (err) {
|
|
143
|
-
reject(err);
|
|
144
|
-
}
|
|
145
|
-
if (context === null || context === void 0 ? void 0 : context.clearPromptOnDone) {
|
|
146
|
-
screen.clean();
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
screen.clearContent();
|
|
150
|
-
}
|
|
151
|
-
screen.done();
|
|
152
|
-
// Reset hooks state
|
|
153
|
-
index = 0;
|
|
154
|
-
hooks.length = 0;
|
|
155
|
-
hooksEffect.length = 0;
|
|
156
|
-
hooksCleanup.length = 0;
|
|
157
|
-
sessionRl = undefined;
|
|
167
|
+
onExit();
|
|
158
168
|
// Finally we resolve our promise
|
|
159
169
|
resolve(value);
|
|
160
170
|
});
|
|
161
171
|
};
|
|
162
|
-
const workLoop = () => {
|
|
172
|
+
const workLoop = (resolvedConfig) => {
|
|
163
173
|
index = 0;
|
|
164
174
|
hooksEffect.length = 0;
|
|
165
|
-
handleChange = () => workLoop();
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
effect
|
|
175
|
+
handleChange = () => workLoop(resolvedConfig);
|
|
176
|
+
try {
|
|
177
|
+
const nextView = view(resolvedConfig, done);
|
|
178
|
+
for (const effect of hooksEffect) {
|
|
179
|
+
effect();
|
|
180
|
+
}
|
|
181
|
+
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
182
|
+
screen.render(content, bottomContent);
|
|
183
|
+
}
|
|
184
|
+
catch (err) {
|
|
185
|
+
reject(err);
|
|
169
186
|
}
|
|
170
|
-
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
171
|
-
screen.render(content, bottomContent);
|
|
172
187
|
};
|
|
173
|
-
|
|
188
|
+
// TODO: we should display a loader while we get the default options.
|
|
189
|
+
(0, options_mjs_1.getPromptConfig)(config).then(workLoop, reject);
|
|
174
190
|
});
|
|
175
|
-
|
|
191
|
+
answer.cancel = cancel;
|
|
192
|
+
return answer;
|
|
193
|
+
};
|
|
176
194
|
return prompt;
|
|
177
195
|
}
|
|
178
196
|
exports.createPrompt = createPrompt;
|
|
@@ -1,27 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
-
if (k2 === undefined) k2 = k;
|
|
4
|
-
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
-
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
-
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
-
}
|
|
8
|
-
Object.defineProperty(o, k2, desc);
|
|
9
|
-
}) : (function(o, m, k, k2) {
|
|
10
|
-
if (k2 === undefined) k2 = k;
|
|
11
|
-
o[k2] = m[k];
|
|
12
|
-
}));
|
|
13
|
-
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
-
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
-
}) : function(o, v) {
|
|
16
|
-
o["default"] = v;
|
|
17
|
-
});
|
|
18
|
-
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
-
if (mod && mod.__esModule) return mod;
|
|
20
|
-
var result = {};
|
|
21
|
-
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
-
__setModuleDefault(result, mod);
|
|
23
|
-
return result;
|
|
24
|
-
};
|
|
25
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
4
|
};
|
|
@@ -30,7 +7,6 @@ const cli_width_1 = __importDefault(require("cli-width"));
|
|
|
30
7
|
const strip_ansi_1 = __importDefault(require("strip-ansi"));
|
|
31
8
|
const string_width_1 = __importDefault(require("string-width"));
|
|
32
9
|
const ansi_escapes_1 = __importDefault(require("ansi-escapes"));
|
|
33
|
-
const util = __importStar(require('./readline.js'));
|
|
34
10
|
const utils_mjs_1 = require('./utils.js');
|
|
35
11
|
const height = (content) => content.split('\n').length;
|
|
36
12
|
const lastLine = (content) => { var _a; return (_a = content.split('\n').pop()) !== null && _a !== void 0 ? _a : ''; };
|
|
@@ -84,8 +60,7 @@ class ScreenManager {
|
|
|
84
60
|
if (rawPromptLine.length % width === 0) {
|
|
85
61
|
content += '\n';
|
|
86
62
|
}
|
|
87
|
-
|
|
88
|
-
this.rl.output.write(fullContent);
|
|
63
|
+
let output = content + (bottomContent ? '\n' + bottomContent : '');
|
|
89
64
|
/**
|
|
90
65
|
* Re-adjust the cursor at the correct position.
|
|
91
66
|
*/
|
|
@@ -94,29 +69,42 @@ class ScreenManager {
|
|
|
94
69
|
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - cursorPos.rows;
|
|
95
70
|
const bottomContentHeight = promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
|
|
96
71
|
// Return cursor to the input position (on top of the bottomContent)
|
|
97
|
-
|
|
72
|
+
if (bottomContentHeight > 0)
|
|
73
|
+
output += ansi_escapes_1.default.cursorUp(bottomContentHeight);
|
|
98
74
|
// Move cursor at the start of the line, then return to the initial left offset.
|
|
99
|
-
|
|
100
|
-
|
|
75
|
+
const backward = (0, string_width_1.default)(lastLine(output));
|
|
76
|
+
if (backward > 0)
|
|
77
|
+
output += ansi_escapes_1.default.cursorBackward(backward);
|
|
78
|
+
if (cursorPos.cols > 0)
|
|
79
|
+
output += ansi_escapes_1.default.cursorForward(cursorPos.cols);
|
|
101
80
|
/**
|
|
102
81
|
* Set up state for next re-rendering
|
|
103
82
|
*/
|
|
104
83
|
this.extraLinesUnderPrompt = bottomContentHeight;
|
|
105
|
-
this.height = height(
|
|
84
|
+
this.height = height(output);
|
|
85
|
+
this.rl.output.write(output);
|
|
106
86
|
this.rl.output.mute();
|
|
107
87
|
}
|
|
108
88
|
clean() {
|
|
109
89
|
this.rl.output.unmute();
|
|
110
|
-
|
|
111
|
-
|
|
90
|
+
this.rl.output.write([
|
|
91
|
+
this.extraLinesUnderPrompt > 0
|
|
92
|
+
? ansi_escapes_1.default.cursorDown(this.extraLinesUnderPrompt)
|
|
93
|
+
: '',
|
|
94
|
+
ansi_escapes_1.default.eraseLines(this.height),
|
|
95
|
+
].join(''));
|
|
112
96
|
this.extraLinesUnderPrompt = 0;
|
|
113
97
|
this.rl.output.mute();
|
|
114
98
|
}
|
|
115
99
|
clearContent() {
|
|
116
100
|
this.rl.output.unmute();
|
|
117
101
|
// Reset the cursor at the end of the previously displayed content
|
|
118
|
-
|
|
119
|
-
|
|
102
|
+
this.rl.output.write([
|
|
103
|
+
this.extraLinesUnderPrompt > 0
|
|
104
|
+
? ansi_escapes_1.default.cursorDown(this.extraLinesUnderPrompt)
|
|
105
|
+
: '',
|
|
106
|
+
'\n',
|
|
107
|
+
].join(''));
|
|
120
108
|
this.rl.output.mute();
|
|
121
109
|
}
|
|
122
110
|
done() {
|
package/dist/cjs/lib/utils.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
/// <reference types="node" />
|
|
3
3
|
import readline from 'node:readline';
|
|
4
|
-
import type
|
|
4
|
+
import { type Prompt } from '@inquirer/type';
|
|
5
5
|
import MuteStream from 'mute-stream';
|
|
6
6
|
export { usePrefix } from './lib/prefix.d.cts';
|
|
7
7
|
export * from './lib/key.d.cts';
|
package/dist/esm/index.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import readline from 'node:readline';
|
|
2
|
+
import { CancelablePromise } from '@inquirer/type';
|
|
2
3
|
import MuteStream from 'mute-stream';
|
|
3
4
|
import ScreenManager from './lib/screen-manager.mjs';
|
|
4
5
|
import { getPromptConfig } from './lib/options.mjs';
|
|
@@ -12,6 +13,14 @@ const hooksEffect = [];
|
|
|
12
13
|
let index = 0;
|
|
13
14
|
let handleChange = () => { };
|
|
14
15
|
let sessionRl;
|
|
16
|
+
const resetHookState = () => {
|
|
17
|
+
hooks.length = 0;
|
|
18
|
+
hooksCleanup.length = 0;
|
|
19
|
+
hooksEffect.length = 0;
|
|
20
|
+
index = 0;
|
|
21
|
+
handleChange = () => { };
|
|
22
|
+
sessionRl = undefined;
|
|
23
|
+
};
|
|
15
24
|
const cleanupHook = (index) => {
|
|
16
25
|
const cleanFn = hooksCleanup[index];
|
|
17
26
|
if (typeof cleanFn === 'function') {
|
|
@@ -77,66 +86,84 @@ export function useRef(val) {
|
|
|
77
86
|
return useState({ current: val })[0];
|
|
78
87
|
}
|
|
79
88
|
export function createPrompt(view) {
|
|
80
|
-
const prompt =
|
|
89
|
+
const prompt = (config, context) => {
|
|
90
|
+
// Set our state before starting the prompt.
|
|
91
|
+
resetHookState();
|
|
81
92
|
// Default `input` to stdin
|
|
82
93
|
const input = context?.input ?? process.stdin;
|
|
83
94
|
// Add mute capabilities to the output
|
|
84
95
|
const output = new MuteStream();
|
|
85
96
|
output.pipe(context?.output ?? process.stdout);
|
|
86
|
-
|
|
97
|
+
sessionRl = readline.createInterface({
|
|
87
98
|
terminal: true,
|
|
88
99
|
input,
|
|
89
100
|
output,
|
|
90
101
|
});
|
|
91
|
-
const screen = new ScreenManager(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
102
|
+
const screen = new ScreenManager(sessionRl);
|
|
103
|
+
let cancel = () => { };
|
|
104
|
+
const answer = new CancelablePromise((resolve, reject) => {
|
|
105
|
+
const onExit = () => {
|
|
106
|
+
try {
|
|
107
|
+
let len = hooksCleanup.length;
|
|
108
|
+
while (len--) {
|
|
109
|
+
cleanupHook(len);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
reject(err);
|
|
114
|
+
}
|
|
115
|
+
if (context?.clearPromptOnDone) {
|
|
116
|
+
screen.clean();
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
screen.clearContent();
|
|
120
|
+
}
|
|
121
|
+
screen.done();
|
|
122
|
+
process.removeListener('SIGINT', onForceExit);
|
|
123
|
+
};
|
|
124
|
+
cancel = () => {
|
|
125
|
+
onExit();
|
|
126
|
+
reject(new Error('Prompt was canceled'));
|
|
127
|
+
};
|
|
128
|
+
let shouldHandleExit = true;
|
|
129
|
+
const onForceExit = () => {
|
|
130
|
+
if (shouldHandleExit) {
|
|
131
|
+
shouldHandleExit = false;
|
|
132
|
+
onExit();
|
|
133
|
+
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
134
|
+
}
|
|
135
|
+
};
|
|
136
|
+
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
137
|
+
process.on('SIGINT', onForceExit);
|
|
98
138
|
const done = (value) => {
|
|
99
139
|
// Delay execution to let time to the hookCleanup functions to registers.
|
|
100
140
|
setImmediate(() => {
|
|
101
|
-
|
|
102
|
-
let len = hooksCleanup.length;
|
|
103
|
-
while (len--) {
|
|
104
|
-
cleanupHook(len);
|
|
105
|
-
}
|
|
106
|
-
}
|
|
107
|
-
catch (err) {
|
|
108
|
-
reject(err);
|
|
109
|
-
}
|
|
110
|
-
if (context?.clearPromptOnDone) {
|
|
111
|
-
screen.clean();
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
screen.clearContent();
|
|
115
|
-
}
|
|
116
|
-
screen.done();
|
|
117
|
-
// Reset hooks state
|
|
118
|
-
index = 0;
|
|
119
|
-
hooks.length = 0;
|
|
120
|
-
hooksEffect.length = 0;
|
|
121
|
-
hooksCleanup.length = 0;
|
|
122
|
-
sessionRl = undefined;
|
|
141
|
+
onExit();
|
|
123
142
|
// Finally we resolve our promise
|
|
124
143
|
resolve(value);
|
|
125
144
|
});
|
|
126
145
|
};
|
|
127
|
-
const workLoop = () => {
|
|
146
|
+
const workLoop = (resolvedConfig) => {
|
|
128
147
|
index = 0;
|
|
129
148
|
hooksEffect.length = 0;
|
|
130
|
-
handleChange = () => workLoop();
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
effect
|
|
149
|
+
handleChange = () => workLoop(resolvedConfig);
|
|
150
|
+
try {
|
|
151
|
+
const nextView = view(resolvedConfig, done);
|
|
152
|
+
for (const effect of hooksEffect) {
|
|
153
|
+
effect();
|
|
154
|
+
}
|
|
155
|
+
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
156
|
+
screen.render(content, bottomContent);
|
|
157
|
+
}
|
|
158
|
+
catch (err) {
|
|
159
|
+
reject(err);
|
|
134
160
|
}
|
|
135
|
-
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
136
|
-
screen.render(content, bottomContent);
|
|
137
161
|
};
|
|
138
|
-
|
|
162
|
+
// TODO: we should display a loader while we get the default options.
|
|
163
|
+
getPromptConfig(config).then(workLoop, reject);
|
|
139
164
|
});
|
|
165
|
+
answer.cancel = cancel;
|
|
166
|
+
return answer;
|
|
140
167
|
};
|
|
141
168
|
return prompt;
|
|
142
169
|
}
|
|
@@ -2,7 +2,6 @@ import cliWidth from 'cli-width';
|
|
|
2
2
|
import stripAnsi from 'strip-ansi';
|
|
3
3
|
import stringWidth from 'string-width';
|
|
4
4
|
import ansiEscapes from 'ansi-escapes';
|
|
5
|
-
import * as util from './readline.mjs';
|
|
6
5
|
import { breakLines } from './utils.mjs';
|
|
7
6
|
const height = (content) => content.split('\n').length;
|
|
8
7
|
const lastLine = (content) => content.split('\n').pop() ?? '';
|
|
@@ -42,8 +41,7 @@ export default class ScreenManager {
|
|
|
42
41
|
if (rawPromptLine.length % width === 0) {
|
|
43
42
|
content += '\n';
|
|
44
43
|
}
|
|
45
|
-
|
|
46
|
-
this.rl.output.write(fullContent);
|
|
44
|
+
let output = content + (bottomContent ? '\n' + bottomContent : '');
|
|
47
45
|
/**
|
|
48
46
|
* Re-adjust the cursor at the correct position.
|
|
49
47
|
*/
|
|
@@ -52,29 +50,42 @@ export default class ScreenManager {
|
|
|
52
50
|
const promptLineUpDiff = Math.floor(rawPromptLine.length / width) - cursorPos.rows;
|
|
53
51
|
const bottomContentHeight = promptLineUpDiff + (bottomContent ? height(bottomContent) : 0);
|
|
54
52
|
// Return cursor to the input position (on top of the bottomContent)
|
|
55
|
-
|
|
53
|
+
if (bottomContentHeight > 0)
|
|
54
|
+
output += ansiEscapes.cursorUp(bottomContentHeight);
|
|
56
55
|
// Move cursor at the start of the line, then return to the initial left offset.
|
|
57
|
-
|
|
58
|
-
|
|
56
|
+
const backward = stringWidth(lastLine(output));
|
|
57
|
+
if (backward > 0)
|
|
58
|
+
output += ansiEscapes.cursorBackward(backward);
|
|
59
|
+
if (cursorPos.cols > 0)
|
|
60
|
+
output += ansiEscapes.cursorForward(cursorPos.cols);
|
|
59
61
|
/**
|
|
60
62
|
* Set up state for next re-rendering
|
|
61
63
|
*/
|
|
62
64
|
this.extraLinesUnderPrompt = bottomContentHeight;
|
|
63
|
-
this.height = height(
|
|
65
|
+
this.height = height(output);
|
|
66
|
+
this.rl.output.write(output);
|
|
64
67
|
this.rl.output.mute();
|
|
65
68
|
}
|
|
66
69
|
clean() {
|
|
67
70
|
this.rl.output.unmute();
|
|
68
|
-
|
|
69
|
-
|
|
71
|
+
this.rl.output.write([
|
|
72
|
+
this.extraLinesUnderPrompt > 0
|
|
73
|
+
? ansiEscapes.cursorDown(this.extraLinesUnderPrompt)
|
|
74
|
+
: '',
|
|
75
|
+
ansiEscapes.eraseLines(this.height),
|
|
76
|
+
].join(''));
|
|
70
77
|
this.extraLinesUnderPrompt = 0;
|
|
71
78
|
this.rl.output.mute();
|
|
72
79
|
}
|
|
73
80
|
clearContent() {
|
|
74
81
|
this.rl.output.unmute();
|
|
75
82
|
// Reset the cursor at the end of the previously displayed content
|
|
76
|
-
|
|
77
|
-
|
|
83
|
+
this.rl.output.write([
|
|
84
|
+
this.extraLinesUnderPrompt > 0
|
|
85
|
+
? ansiEscapes.cursorDown(this.extraLinesUnderPrompt)
|
|
86
|
+
: '',
|
|
87
|
+
'\n',
|
|
88
|
+
].join(''));
|
|
78
89
|
this.rl.output.mute();
|
|
79
90
|
}
|
|
80
91
|
done() {
|
package/dist/esm/lib/utils.mjs
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" resolution-mode="require"/>
|
|
2
2
|
/// <reference types="node" resolution-mode="require"/>
|
|
3
3
|
import readline from 'node:readline';
|
|
4
|
-
import type
|
|
4
|
+
import { type Prompt } from '@inquirer/type';
|
|
5
5
|
import MuteStream from 'mute-stream';
|
|
6
6
|
export { usePrefix } from './lib/prefix.mjs';
|
|
7
7
|
export * from './lib/key.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/core",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=14.18.0"
|
|
6
6
|
},
|
|
@@ -57,7 +57,7 @@
|
|
|
57
57
|
"license": "MIT",
|
|
58
58
|
"homepage": "https://github.com/SBoudrias/Inquirer.js/blob/master/packages/core/README.md",
|
|
59
59
|
"dependencies": {
|
|
60
|
-
"@inquirer/type": "^1.0
|
|
60
|
+
"@inquirer/type": "^1.1.0",
|
|
61
61
|
"ansi-escapes": "^4.3.2",
|
|
62
62
|
"chalk": "^4.1.2",
|
|
63
63
|
"cli-spinners": "^2.8.0",
|
|
@@ -70,14 +70,14 @@
|
|
|
70
70
|
"wrap-ansi": "^6.0.1"
|
|
71
71
|
},
|
|
72
72
|
"devDependencies": {
|
|
73
|
-
"@inquirer/testing": "^
|
|
73
|
+
"@inquirer/testing": "^2.0.0"
|
|
74
74
|
},
|
|
75
75
|
"scripts": {
|
|
76
76
|
"tsc": "yarn run clean && yarn run tsc:esm && yarn run tsc:cjs",
|
|
77
77
|
"clean": "rm -rf dist",
|
|
78
|
-
"tsc:esm": "tsc -p ./tsconfig.
|
|
78
|
+
"tsc:esm": "tsc -p ./tsconfig.json",
|
|
79
79
|
"tsc:cjs": "tsc -p ./tsconfig.cjs.json && yarn run fix-ext",
|
|
80
|
-
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/
|
|
80
|
+
"fix-ext": "node --no-warnings=ExperimentalWarning --loader=ts-node/esm ../../tools/fix-ext.mts"
|
|
81
81
|
},
|
|
82
82
|
"publishConfig": {
|
|
83
83
|
"access": "public"
|
|
@@ -94,5 +94,5 @@
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
},
|
|
97
|
-
"gitHead": "
|
|
97
|
+
"gitHead": "86531864067faf1d6e76f1581c9aa72cb1b03753"
|
|
98
98
|
}
|
package/dist/cjs/lib/readline.js
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
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.clearLine = exports.down = exports.up = exports.right = exports.left = void 0;
|
|
7
|
-
const ansi_escapes_1 = __importDefault(require("ansi-escapes"));
|
|
8
|
-
/**
|
|
9
|
-
* Move cursor left by `x`
|
|
10
|
-
* @param {Readline} rl - Readline instance
|
|
11
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
12
|
-
*/
|
|
13
|
-
function left(rl, x) {
|
|
14
|
-
if (x > 0) {
|
|
15
|
-
rl.output.write(ansi_escapes_1.default.cursorBackward(x));
|
|
16
|
-
}
|
|
17
|
-
}
|
|
18
|
-
exports.left = left;
|
|
19
|
-
/**
|
|
20
|
-
* Move cursor right by `x`
|
|
21
|
-
* @param {Readline} rl - Readline instance
|
|
22
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
23
|
-
*/
|
|
24
|
-
function right(rl, x) {
|
|
25
|
-
if (x > 0) {
|
|
26
|
-
rl.output.write(ansi_escapes_1.default.cursorForward(x));
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
exports.right = right;
|
|
30
|
-
/**
|
|
31
|
-
* Move cursor up by `x`
|
|
32
|
-
* @param {Readline} rl - Readline instance
|
|
33
|
-
* @param {Number} x - How far to go up (default to 1)
|
|
34
|
-
*/
|
|
35
|
-
function up(rl, x) {
|
|
36
|
-
if (x > 0) {
|
|
37
|
-
rl.output.write(ansi_escapes_1.default.cursorUp(x));
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
exports.up = up;
|
|
41
|
-
/**
|
|
42
|
-
* Move cursor down by `x`
|
|
43
|
-
* @param {Readline} rl - Readline instance
|
|
44
|
-
* @param {Number} x - How far to go down (default to 1)
|
|
45
|
-
*/
|
|
46
|
-
function down(rl, x) {
|
|
47
|
-
if (x > 0) {
|
|
48
|
-
rl.output.write(ansi_escapes_1.default.cursorDown(x));
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
exports.down = down;
|
|
52
|
-
/**
|
|
53
|
-
* Clear current line
|
|
54
|
-
* @param {Readline} rl - Readline instance
|
|
55
|
-
* @param {Number} len - number of line to delete
|
|
56
|
-
*/
|
|
57
|
-
function clearLine(rl, len) {
|
|
58
|
-
rl.output.write(ansi_escapes_1.default.eraseLines(len));
|
|
59
|
-
}
|
|
60
|
-
exports.clearLine = clearLine;
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { InquirerReadline } from '../index.d.cts';
|
|
2
|
-
/**
|
|
3
|
-
* Move cursor left by `x`
|
|
4
|
-
* @param {Readline} rl - Readline instance
|
|
5
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
6
|
-
*/
|
|
7
|
-
export declare function left(rl: InquirerReadline, x: number): void;
|
|
8
|
-
/**
|
|
9
|
-
* Move cursor right by `x`
|
|
10
|
-
* @param {Readline} rl - Readline instance
|
|
11
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
12
|
-
*/
|
|
13
|
-
export declare function right(rl: InquirerReadline, x: number): void;
|
|
14
|
-
/**
|
|
15
|
-
* Move cursor up by `x`
|
|
16
|
-
* @param {Readline} rl - Readline instance
|
|
17
|
-
* @param {Number} x - How far to go up (default to 1)
|
|
18
|
-
*/
|
|
19
|
-
export declare function up(rl: InquirerReadline, x: number): void;
|
|
20
|
-
/**
|
|
21
|
-
* Move cursor down by `x`
|
|
22
|
-
* @param {Readline} rl - Readline instance
|
|
23
|
-
* @param {Number} x - How far to go down (default to 1)
|
|
24
|
-
*/
|
|
25
|
-
export declare function down(rl: InquirerReadline, x: number): void;
|
|
26
|
-
/**
|
|
27
|
-
* Clear current line
|
|
28
|
-
* @param {Readline} rl - Readline instance
|
|
29
|
-
* @param {Number} len - number of line to delete
|
|
30
|
-
*/
|
|
31
|
-
export declare function clearLine(rl: InquirerReadline, len: number): void;
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import ansiEscapes from 'ansi-escapes';
|
|
2
|
-
/**
|
|
3
|
-
* Move cursor left by `x`
|
|
4
|
-
* @param {Readline} rl - Readline instance
|
|
5
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
6
|
-
*/
|
|
7
|
-
export function left(rl, x) {
|
|
8
|
-
if (x > 0) {
|
|
9
|
-
rl.output.write(ansiEscapes.cursorBackward(x));
|
|
10
|
-
}
|
|
11
|
-
}
|
|
12
|
-
/**
|
|
13
|
-
* Move cursor right by `x`
|
|
14
|
-
* @param {Readline} rl - Readline instance
|
|
15
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
16
|
-
*/
|
|
17
|
-
export function right(rl, x) {
|
|
18
|
-
if (x > 0) {
|
|
19
|
-
rl.output.write(ansiEscapes.cursorForward(x));
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Move cursor up by `x`
|
|
24
|
-
* @param {Readline} rl - Readline instance
|
|
25
|
-
* @param {Number} x - How far to go up (default to 1)
|
|
26
|
-
*/
|
|
27
|
-
export function up(rl, x) {
|
|
28
|
-
if (x > 0) {
|
|
29
|
-
rl.output.write(ansiEscapes.cursorUp(x));
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Move cursor down by `x`
|
|
34
|
-
* @param {Readline} rl - Readline instance
|
|
35
|
-
* @param {Number} x - How far to go down (default to 1)
|
|
36
|
-
*/
|
|
37
|
-
export function down(rl, x) {
|
|
38
|
-
if (x > 0) {
|
|
39
|
-
rl.output.write(ansiEscapes.cursorDown(x));
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
/**
|
|
43
|
-
* Clear current line
|
|
44
|
-
* @param {Readline} rl - Readline instance
|
|
45
|
-
* @param {Number} len - number of line to delete
|
|
46
|
-
*/
|
|
47
|
-
export function clearLine(rl, len) {
|
|
48
|
-
rl.output.write(ansiEscapes.eraseLines(len));
|
|
49
|
-
}
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import type { InquirerReadline } from '../index.mjs';
|
|
2
|
-
/**
|
|
3
|
-
* Move cursor left by `x`
|
|
4
|
-
* @param {Readline} rl - Readline instance
|
|
5
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
6
|
-
*/
|
|
7
|
-
export declare function left(rl: InquirerReadline, x: number): void;
|
|
8
|
-
/**
|
|
9
|
-
* Move cursor right by `x`
|
|
10
|
-
* @param {Readline} rl - Readline instance
|
|
11
|
-
* @param {Number} x - How far to go left (default to 1)
|
|
12
|
-
*/
|
|
13
|
-
export declare function right(rl: InquirerReadline, x: number): void;
|
|
14
|
-
/**
|
|
15
|
-
* Move cursor up by `x`
|
|
16
|
-
* @param {Readline} rl - Readline instance
|
|
17
|
-
* @param {Number} x - How far to go up (default to 1)
|
|
18
|
-
*/
|
|
19
|
-
export declare function up(rl: InquirerReadline, x: number): void;
|
|
20
|
-
/**
|
|
21
|
-
* Move cursor down by `x`
|
|
22
|
-
* @param {Readline} rl - Readline instance
|
|
23
|
-
* @param {Number} x - How far to go down (default to 1)
|
|
24
|
-
*/
|
|
25
|
-
export declare function down(rl: InquirerReadline, x: number): void;
|
|
26
|
-
/**
|
|
27
|
-
* Clear current line
|
|
28
|
-
* @param {Readline} rl - Readline instance
|
|
29
|
-
* @param {Number} len - number of line to delete
|
|
30
|
-
*/
|
|
31
|
-
export declare function clearLine(rl: InquirerReadline, len: number): void;
|