@inquirer/core 1.3.0 → 2.1.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 +72 -50
- package/dist/cjs/lib/screen-manager.js +22 -34
- package/dist/cjs/types/index.d.cts +2 -2
- package/dist/esm/index.mjs +71 -40
- package/dist/esm/lib/screen-manager.mjs +22 -11
- package/dist/esm/types/index.d.mts +2 -2
- package/package.json +4 -4
- 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') {
|
|
@@ -70,6 +70,10 @@ function useState(defaultValue) {
|
|
|
70
70
|
}
|
|
71
71
|
exports.useState = useState;
|
|
72
72
|
function useEffect(cb, depArray) {
|
|
73
|
+
const rl = sessionRl;
|
|
74
|
+
if (!rl) {
|
|
75
|
+
throw new Error('useEffect must be used within a prompt');
|
|
76
|
+
}
|
|
73
77
|
const _idx = index;
|
|
74
78
|
index++;
|
|
75
79
|
const oldDeps = hooks[_idx];
|
|
@@ -80,7 +84,7 @@ function useEffect(cb, depArray) {
|
|
|
80
84
|
if (hasChanged) {
|
|
81
85
|
hooksEffect.push(() => {
|
|
82
86
|
cleanupHook(_idx);
|
|
83
|
-
const cleanFn = cb();
|
|
87
|
+
const cleanFn = cb(rl);
|
|
84
88
|
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
85
89
|
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
86
90
|
}
|
|
@@ -111,68 +115,86 @@ function useRef(val) {
|
|
|
111
115
|
}
|
|
112
116
|
exports.useRef = useRef;
|
|
113
117
|
function createPrompt(view) {
|
|
114
|
-
const prompt = (config, context) =>
|
|
118
|
+
const prompt = (config, context) => {
|
|
115
119
|
var _a, _b;
|
|
120
|
+
// Set our state before starting the prompt.
|
|
121
|
+
resetHookState();
|
|
116
122
|
// Default `input` to stdin
|
|
117
123
|
const input = (_a = context === null || context === void 0 ? void 0 : context.input) !== null && _a !== void 0 ? _a : process.stdin;
|
|
118
124
|
// Add mute capabilities to the output
|
|
119
125
|
const output = new mute_stream_1.default();
|
|
120
126
|
output.pipe((_b = context === null || context === void 0 ? void 0 : context.output) !== null && _b !== void 0 ? _b : process.stdout);
|
|
121
|
-
|
|
127
|
+
sessionRl = node_readline_1.default.createInterface({
|
|
122
128
|
terminal: true,
|
|
123
129
|
input,
|
|
124
130
|
output,
|
|
125
131
|
});
|
|
126
|
-
const screen = new screen_manager_mjs_1.default(
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
132
|
+
const screen = new screen_manager_mjs_1.default(sessionRl);
|
|
133
|
+
let cancel = () => { };
|
|
134
|
+
const answer = new type_1.CancelablePromise((resolve, reject) => {
|
|
135
|
+
const onExit = () => {
|
|
136
|
+
try {
|
|
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
|
+
process.removeListener('SIGINT', onForceExit);
|
|
153
|
+
};
|
|
154
|
+
cancel = () => {
|
|
155
|
+
onExit();
|
|
156
|
+
reject(new Error('Prompt was canceled'));
|
|
157
|
+
};
|
|
158
|
+
let shouldHandleExit = true;
|
|
159
|
+
const onForceExit = () => {
|
|
160
|
+
if (shouldHandleExit) {
|
|
161
|
+
shouldHandleExit = false;
|
|
162
|
+
onExit();
|
|
163
|
+
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
167
|
+
process.on('SIGINT', onForceExit);
|
|
133
168
|
const done = (value) => {
|
|
134
169
|
// Delay execution to let time to the hookCleanup functions to registers.
|
|
135
170
|
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;
|
|
171
|
+
onExit();
|
|
158
172
|
// Finally we resolve our promise
|
|
159
173
|
resolve(value);
|
|
160
174
|
});
|
|
161
175
|
};
|
|
162
|
-
const workLoop = () => {
|
|
176
|
+
const workLoop = (resolvedConfig) => {
|
|
163
177
|
index = 0;
|
|
164
178
|
hooksEffect.length = 0;
|
|
165
|
-
handleChange = () => workLoop();
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
effect
|
|
179
|
+
handleChange = () => workLoop(resolvedConfig);
|
|
180
|
+
try {
|
|
181
|
+
const nextView = view(resolvedConfig, done);
|
|
182
|
+
for (const effect of hooksEffect) {
|
|
183
|
+
effect();
|
|
184
|
+
}
|
|
185
|
+
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
186
|
+
screen.render(content, bottomContent);
|
|
187
|
+
}
|
|
188
|
+
catch (err) {
|
|
189
|
+
reject(err);
|
|
169
190
|
}
|
|
170
|
-
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
171
|
-
screen.render(content, bottomContent);
|
|
172
191
|
};
|
|
173
|
-
|
|
192
|
+
// TODO: we should display a loader while we get the default options.
|
|
193
|
+
(0, options_mjs_1.getPromptConfig)(config).then(workLoop, reject);
|
|
174
194
|
});
|
|
175
|
-
|
|
195
|
+
answer.cancel = cancel;
|
|
196
|
+
return answer;
|
|
197
|
+
};
|
|
176
198
|
return prompt;
|
|
177
199
|
}
|
|
178
200
|
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() {
|
|
@@ -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';
|
|
@@ -18,7 +18,7 @@ export type KeypressEvent = {
|
|
|
18
18
|
};
|
|
19
19
|
type NotFunction<T> = T extends Function ? never : T;
|
|
20
20
|
export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
|
|
21
|
-
export declare function useEffect(cb: () => void | (() => void), depArray: unknown[]): void;
|
|
21
|
+
export declare function useEffect(cb: (rl: InquirerReadline) => void | (() => void), depArray: unknown[]): void;
|
|
22
22
|
export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
|
|
23
23
|
export declare function useRef<Value>(val: Value): {
|
|
24
24
|
current: Value;
|
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') {
|
|
@@ -39,6 +48,10 @@ export function useState(defaultValue) {
|
|
|
39
48
|
];
|
|
40
49
|
}
|
|
41
50
|
export function useEffect(cb, depArray) {
|
|
51
|
+
const rl = sessionRl;
|
|
52
|
+
if (!rl) {
|
|
53
|
+
throw new Error('useEffect must be used within a prompt');
|
|
54
|
+
}
|
|
42
55
|
const _idx = index;
|
|
43
56
|
index++;
|
|
44
57
|
const oldDeps = hooks[_idx];
|
|
@@ -49,7 +62,7 @@ export function useEffect(cb, depArray) {
|
|
|
49
62
|
if (hasChanged) {
|
|
50
63
|
hooksEffect.push(() => {
|
|
51
64
|
cleanupHook(_idx);
|
|
52
|
-
const cleanFn = cb();
|
|
65
|
+
const cleanFn = cb(rl);
|
|
53
66
|
if (cleanFn != null && typeof cleanFn !== 'function') {
|
|
54
67
|
throw new Error('useEffect return value must be a cleanup function or nothing.');
|
|
55
68
|
}
|
|
@@ -77,66 +90,84 @@ export function useRef(val) {
|
|
|
77
90
|
return useState({ current: val })[0];
|
|
78
91
|
}
|
|
79
92
|
export function createPrompt(view) {
|
|
80
|
-
const prompt =
|
|
93
|
+
const prompt = (config, context) => {
|
|
94
|
+
// Set our state before starting the prompt.
|
|
95
|
+
resetHookState();
|
|
81
96
|
// Default `input` to stdin
|
|
82
97
|
const input = context?.input ?? process.stdin;
|
|
83
98
|
// Add mute capabilities to the output
|
|
84
99
|
const output = new MuteStream();
|
|
85
100
|
output.pipe(context?.output ?? process.stdout);
|
|
86
|
-
|
|
101
|
+
sessionRl = readline.createInterface({
|
|
87
102
|
terminal: true,
|
|
88
103
|
input,
|
|
89
104
|
output,
|
|
90
105
|
});
|
|
91
|
-
const screen = new ScreenManager(
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
106
|
+
const screen = new ScreenManager(sessionRl);
|
|
107
|
+
let cancel = () => { };
|
|
108
|
+
const answer = new CancelablePromise((resolve, reject) => {
|
|
109
|
+
const onExit = () => {
|
|
110
|
+
try {
|
|
111
|
+
let len = hooksCleanup.length;
|
|
112
|
+
while (len--) {
|
|
113
|
+
cleanupHook(len);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
catch (err) {
|
|
117
|
+
reject(err);
|
|
118
|
+
}
|
|
119
|
+
if (context?.clearPromptOnDone) {
|
|
120
|
+
screen.clean();
|
|
121
|
+
}
|
|
122
|
+
else {
|
|
123
|
+
screen.clearContent();
|
|
124
|
+
}
|
|
125
|
+
screen.done();
|
|
126
|
+
process.removeListener('SIGINT', onForceExit);
|
|
127
|
+
};
|
|
128
|
+
cancel = () => {
|
|
129
|
+
onExit();
|
|
130
|
+
reject(new Error('Prompt was canceled'));
|
|
131
|
+
};
|
|
132
|
+
let shouldHandleExit = true;
|
|
133
|
+
const onForceExit = () => {
|
|
134
|
+
if (shouldHandleExit) {
|
|
135
|
+
shouldHandleExit = false;
|
|
136
|
+
onExit();
|
|
137
|
+
reject(new Error('User force closed the prompt with CTRL+C'));
|
|
138
|
+
}
|
|
139
|
+
};
|
|
140
|
+
// Handle cleanup on force exit. Main reason is so we restore the cursor if a prompt hide it.
|
|
141
|
+
process.on('SIGINT', onForceExit);
|
|
98
142
|
const done = (value) => {
|
|
99
143
|
// Delay execution to let time to the hookCleanup functions to registers.
|
|
100
144
|
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;
|
|
145
|
+
onExit();
|
|
123
146
|
// Finally we resolve our promise
|
|
124
147
|
resolve(value);
|
|
125
148
|
});
|
|
126
149
|
};
|
|
127
|
-
const workLoop = () => {
|
|
150
|
+
const workLoop = (resolvedConfig) => {
|
|
128
151
|
index = 0;
|
|
129
152
|
hooksEffect.length = 0;
|
|
130
|
-
handleChange = () => workLoop();
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
effect
|
|
153
|
+
handleChange = () => workLoop(resolvedConfig);
|
|
154
|
+
try {
|
|
155
|
+
const nextView = view(resolvedConfig, done);
|
|
156
|
+
for (const effect of hooksEffect) {
|
|
157
|
+
effect();
|
|
158
|
+
}
|
|
159
|
+
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
160
|
+
screen.render(content, bottomContent);
|
|
161
|
+
}
|
|
162
|
+
catch (err) {
|
|
163
|
+
reject(err);
|
|
134
164
|
}
|
|
135
|
-
const [content, bottomContent] = typeof nextView === 'string' ? [nextView] : nextView;
|
|
136
|
-
screen.render(content, bottomContent);
|
|
137
165
|
};
|
|
138
|
-
|
|
166
|
+
// TODO: we should display a loader while we get the default options.
|
|
167
|
+
getPromptConfig(config).then(workLoop, reject);
|
|
139
168
|
});
|
|
169
|
+
answer.cancel = cancel;
|
|
170
|
+
return answer;
|
|
140
171
|
};
|
|
141
172
|
return prompt;
|
|
142
173
|
}
|
|
@@ -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() {
|
|
@@ -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';
|
|
@@ -18,7 +18,7 @@ export type KeypressEvent = {
|
|
|
18
18
|
};
|
|
19
19
|
type NotFunction<T> = T extends Function ? never : T;
|
|
20
20
|
export declare function useState<Value>(defaultValue: NotFunction<Value> | (() => Value)): [Value, (newValue: Value) => void];
|
|
21
|
-
export declare function useEffect(cb: () => void | (() => void), depArray: unknown[]): void;
|
|
21
|
+
export declare function useEffect(cb: (rl: InquirerReadline) => void | (() => void), depArray: unknown[]): void;
|
|
22
22
|
export declare function useKeypress(userHandler: (event: KeypressEvent, rl: InquirerReadline) => void): void;
|
|
23
23
|
export declare function useRef<Value>(val: Value): {
|
|
24
24
|
current: Value;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@inquirer/core",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "2.1.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,7 +70,7 @@
|
|
|
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",
|
|
@@ -94,5 +94,5 @@
|
|
|
94
94
|
}
|
|
95
95
|
}
|
|
96
96
|
},
|
|
97
|
-
"gitHead": "
|
|
97
|
+
"gitHead": "d48ecde923e5875de631f7b50f67cfaf00384947"
|
|
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;
|